MGET
Introduction
In Dragonfly, as well as in Redis and Valkey, the MGET command is used to retrieve the values of multiple keys in a single request.
It simplifies fetching values from several keys at once, making it highly efficient for batch retrieval.
If a key does not exist, MGET returns nil for that key instead of an error.
Syntax
MGET key [key ...]
- Time complexity: O(N) where N is the number of keys to retrieve.
- ACL categories: @read, @string, @fast
Parameter Explanations
key: The key(s) whose values you want to retrieve.- You can specify multiple keys, and their values will be retrieved in the order they were provided.
Return Values
The command returns an array of values corresponding to the list of keys.
If a key does not exist, nil is returned for that particular key.
Code Examples
Basic Example: Retrieving Multiple Keys
Retrieve values from multiple keys:
dragonfly$> SET key1 "value1"
OK
dragonfly$> SET key2 "value2"
OK
dragonfly$> SET key3 "value3"
OK
dragonfly$> MGET key1 key2 key3
1) "value1"
2) "value2"
3) "value3"
When Some Keys Do Not Exist
If one or more keys do not exist, MGET will return nil for the missing keys:
dragonfly$> MGET key1 key_non_existent key3
1) "value1"
2) (nil)
3) "value3"
Using MGET for Bulk Retrieval
In scenarios where you are managing session data for multiple users, you can use MGET to efficiently retrieve their session information in one operation:
dragonfly$> SET session:user1 "data1"
OK
dragonfly$> SET session:user2 "data2"
OK
dragonfly$> SET session:user3 "data3"
OK
dragonfly$> MGET session:user1 session:user2 session:user3
1) "data1"
2) "data2"
3) "data3"
Example with Keys that Expire
If some of the keys have expired or are nearing expiration, MGET will still retrieve available keys but return nil for those that no longer exist:
dragonfly$> SET key_expiring "temp_value" EX 1 # Expires in 1 second
OK
dragonfly$> SET key_persistent "persistent_value"
OK
dragonfly$> MGET key_expiring key_persistent
1) "temp_value"
2) "persistent_value"
# After 1 second
dragonfly$> MGET key_expiring key_persistent
1) (nil)
2) "persistent_value"
Benchmark
Sustained throughput for MGET on a single instance, measured against Redis and Valkey on identical hardware. Higher is better.
| Engine | Throughput | p50 | p99 | p99.9 | Avg Latency |
|---|---|---|---|---|---|
| Dragonfly | 8.09M ops/s | 0.549 ms | 1.114 ms | 1.432 ms | 0.570 ms |
| Valkey | 1.49M ops/s | 2.754 ms | 8.701 ms | 9.487 ms | 3.211 ms |
| Redis | 1.37M ops/s | 3.408 ms | 6.658 ms | 7.939 ms | 3.478 ms |
Methodology
| Hardware | Server: m7g.8xlarge (arm64) · Client: c6gn.8xlarge (arm64) |
| Client | 32 threads, 5 connections, pipeline 30 |
| Dataset | 100M keys, 128B values, uniform key distribution |
| Duration | 60s (10s warmup), 1 trial |
| Measured | 2026-07-28 |
Ran through dfly_bench until throughput stabilized; the reported figure is the median throughput sampled during the run. Harness and raw output: dragonflydb/benchmarking.
Best Practices
- Use
MGETto minimize network round trips when retrieving values from multiple keys at once. - Combine keys logically when possible to facilitate batch retrieval, which can significantly improve application performance.
- If some keys are expected to frequently expire, account for
nilvalues in the application logic to avoid unnecessary errors.
Common Mistakes
- Forgetting that
MGETdoesn't throw an error if keys don't exist, which might lead to unexpectednilvalues in the returned results. - Assuming
MGETwill throw an exception for expired or missing keys—it silently returnsnilfor those keys instead. - Not handling possible
nilresults when operating on keys subject to expiration.
FAQs
Does MGET return an error if one of the keys does not exist?
No, if one or more keys do not exist or have expired, MGET returns nil for those keys without throwing an error.
Can I use MGET with binary data?
Yes, MGET works with binary strings, just as it does with normal strings.
It can retrieve any data stored in a binary-safe manner.
Is there a limit to how many keys I can fetch using MGET?
There isn't a strict limit enforced by Dragonfly, Redis, or Valkey, though practical limitations such as memory or maximum payload size may apply based on available system resources.