Skip to main content

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.

Dragonfly8,090,000OPS
Valkey1,490,0005.4× OPS
Redis1,370,0005.9× OPS
02.13M4.25M6.38M8.5M
ops/second
EngineThroughputp50p99p99.9Avg Latency
Dragonfly8.09M ops/s0.549 ms1.114 ms1.432 ms0.570 ms
Valkey1.49M ops/s2.754 ms8.701 ms9.487 ms3.211 ms
Redis1.37M ops/s3.408 ms6.658 ms7.939 ms3.478 ms

Methodology

HardwareServer: m7g.8xlarge (arm64) · Client: c6gn.8xlarge (arm64)
Client32 threads, 5 connections, pipeline 30
Dataset100M keys, 128B values, uniform key distribution
Duration60s (10s warmup), 1 trial
Measured2026-07-29

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 MGET to 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 nil values in the application logic to avoid unnecessary errors.

Common Mistakes​

  • Forgetting that MGET doesn't throw an error if keys don't exist, which might lead to unexpected nil values in the returned results.
  • Assuming MGET will throw an exception for expired or missing keys—it silently returns nil for those keys instead.
  • Not handling possible nil results 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.