Advertisement
Intermediate Redis, Data Types

What is a Redis hash, and why is it often a more efficient choice than storing separate keys for each field of an object?

Short answer

A hash stores multiple field value pairs under a single key, similar to a small object or dictionary, and is often more memory efficient than storing each field as its own separate top level key, since Redis can store small hashes very compactly, and it also lets you retrieve or…

Save Redis mock interview
Advertisement
AModel answer
A hash stores multiple field value pairs under a single key, similar to a small object or dictionary, and is often more memory efficient than storing each field as its own separate top level key, since Redis can store small hashes very compactly, and it also lets you retrieve or update specific fields without touching the rest of the object.
Example

HSET user:123 name 'Alice' email 'alice@example.com' age 28 HGET user:123 email

Real-world example
A user profile system stores a user's name, email, and age together in a single hash, rather than as three separate top level keys, saving memory and making it easy to fetch the entire profile with one command.
Likely follow-ups
  • At what point does storing many fields in a hash become less efficient than separate keys?
  • Can you set an expiration time on an individual field within a hash?
Related topics