Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Redis · Module

MGET

MGET atomically retrieves the string values of one or more keys in a single command. Missing keys and keys holding non-string values return nil in their corresponding result positions.

How it works

MGET atomically reads the string values for one or more keys and returns them as an array aligned with the requested key order. A missing key or a key containing a non-string value contributes nil, so the command does not fail for those cases.

Syntax

MGET key [key ...]

Parameters

NameTypeDescription
key [key ...]keyOne or more keys whose string values should be retrieved.

Returns

An array of values corresponding to the specified keys; missing keys and keys that do not hold strings return nil.

Key points

  • Use MGET to retrieve multiple Redis string values in one atomic command.
  • The response contains one result for every supplied key.
  • Missing keys and non-string keys do not abort the complete multi-key read.
  • Redis classifies MGET as a read operation for string data and marks it fast.
  • Execution cost grows linearly with the number of requested keys.

Common mistakes

  • A missing key and a key with a non-string data type both produce nil, which can make those cases indistinguishable from the returned value alone.
  • Cluster configuration affects multi-key behavior; verify the documented clustering rules before assuming every requested key is handled identically.

Example: Read several string keys

redis-cli SET user:1 Alice
redis-cli SET user:2 Bob
redis-cli MGET user:1 user:2 user:3

The command returns the values in key order; the missing key produces nil.

When to use it

Batch cache reads

Read several cached string fields in one request.

Lower read latency

Fetch a group of independent values while reducing client-server round trips.

Aligned multi-key results

Preserve the association between requested keys and returned values by using the response positions.

Interview questions

QWhat does MGET do, and what does it return?

MGET accepts one or more keys and returns an array containing the corresponding values. Results preserve the input-key order, and missing or non-string keys contribute nil.

Follow-up: What is its time complexity? It is O(N), where N is the number of keys retrieved.

QWhy use MGET instead of issuing several GET commands?

MGET is atomic as a command: Redis returns the string values for all requested keys in one operation. It is useful when several independent string values must be read with fewer client-server round trips.

Follow-up: How would you reduce latency further for many unrelated requests? Compare MGET with pipelining, while considering the key distribution and the deployment's clustering behavior.

QWhat happens when one requested key is missing or has the wrong type?

MGET is intended for string values. If a requested key is absent or does not contain a string, its position in the returned array is nil rather than causing the whole command to fail.

Follow-up: How should an application distinguish an absent key from a non-string key? Use an additional type-aware check such as TYPE when that distinction matters, because MGET itself reports nil for both cases.

  • GET
  • MSET
  • SET
  • Redis Strings
  • multi-key operations

Source: MGET | Docs - Redis. Checked 2026-10-16.