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

  1. MGET key [key ...]

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:

  1. dragonfly> SET key1 "value1"
  2. OK
  3. dragonfly> SET key2 "value2"
  4. OK
  5. dragonfly> SET key3 "value3"
  6. OK
  7. dragonfly> MGET key1 key2 key3
  8. 1) "value1"
  9. 2) "value2"
  10. 3) "value3"

When Some Keys Do Not Exist

If one or more keys do not exist, MGET will return nil for the missing keys:

  1. dragonfly> MGET key1 key_non_existent key3
  2. 1) "value1"
  3. 2) (nil)
  4. 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:

  1. dragonfly> SET session:user1 "data1"
  2. OK
  3. dragonfly> SET session:user2 "data2"
  4. OK
  5. dragonfly> SET session:user3 "data3"
  6. OK
  7. dragonfly> MGET session:user1 session:user2 session:user3
  8. 1) "data1"
  9. 2) "data2"
  10. 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:

  1. dragonfly> SET key_expiring "temp_value" EX 1 # Expires in 1 second
  2. OK
  3. dragonfly> SET key_persistent "persistent_value"
  4. OK
  5. dragonfly> MGET key_expiring key_persistent
  6. 1) "temp_value"
  7. 2) "persistent_value"
  8. # After 1 second
  9. dragonfly> MGET key_expiring key_persistent
  10. 1) (nil)
  11. 2) "persistent_value"

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.