Hashes in Redis

7 questions found

What is a Redis hash, and how do you create one to store a simple object with multiple fields?

Beginner
A Redis hash lets you store several field value pairs under a single key, similar to a small dictionary or object, using the HSET command to set one or more fields at once, which is a natural way to represent something like a user profile with multiple attributes.
HSET user:100 name 'Sarah' email 'sarah@example.com' age 29
Real-world example A user management system stores a user's name, email, and age together under a single hash key, keeping all of that related profile information organized in one place rather than as separate top level keys.

Common follow-ups: Can you set multiple fields in a single HSET command?;What happens if you call HSET on a field that already exists?

Data Types;Redis Memory Optimization

How do you retrieve a single field or all fields from a Redis hash?

Beginner
You use HGET with a specific field name to retrieve just that one value, or HGETALL to retrieve every field and value pair stored in the hash at once, letting you either fetch a specific piece of information or the entire object depending on what your application needs.
HGET user:100 email
HGETALL user:100
Real-world example A profile page uses HGETALL to fetch a user's entire profile at once for display, while a notification system uses HGET to quickly check just the user's email field when sending a message.

Common follow-ups: Is HGETALL efficient for hashes with a very large number of fields?;What is the format of the data returned by HGETALL?

Data Types;Redis CLI & Basic Commands

How do you atomically increment a numeric field within a Redis hash?

Intermediate
You use the HINCRBY command, specifying the hash key, the field name, and the amount to increase it by, which atomically updates that specific field's numeric value, making it safe to use even when multiple clients are trying to update the same counter field simultaneously.
HINCRBY user:100 loginCount 1
HINCRBY product:55 stock -1
Real-world example An e-commerce platform atomically decreases a product's stock count field within its hash using HINCRBY whenever an order is placed, safely handling many simultaneous purchases without risking an incorrect stock count.

Common follow-ups: What happens if you try to increment a field that does not currently hold a numeric value?;Is there an equivalent command for incrementing by a floating point amount?

Rate Limiting with Redis;Redis CLI & Basic Commands

How do you check if a specific field exists within a hash, and how do you delete just one field without affecting the rest of the hash?

Intermediate
You use HEXISTS to check whether a specific field is present in a hash, returning a simple true or false style result, and HDEL to remove one or more specific fields from a hash, leaving the remaining fields completely untouched.
HEXISTS user:100 phoneNumber
HDEL user:100 temporaryFlag
Real-world example A user profile system checks whether an optional phone number field exists before displaying it, and removes a temporary flag field from a user's hash once it is no longer needed, without affecting any other stored profile data.

Common follow-ups: What happens if you call HDEL on the last remaining field in a hash?;Can you delete multiple fields at once with a single HDEL call?

Data Types;Redis CLI & Basic Commands

How would you decide between storing an object as a single JSON string versus as a native Redis hash, considering both memory usage and access patterns?

Advanced
You would generally prefer a native hash when your application frequently needs to read or update individual fields independently, since hashes let you avoid retrieving and rewriting the entire object for a small change, while a JSON string might be simpler when you almost always read or write the entire object together and do not need field level access.
-- Hash: efficient for updating a single field
HSET user:100 lastLogin '2026-09-07'

-- JSON string: requires reading, modifying, and rewriting the whole value
SET user:100 '{"name":"Sarah","lastLogin":"2026-09-07"}'
Real-world example A user session system switches from storing session data as a JSON string to a native hash, since it frequently needed to update just the last activity timestamp field without touching the rest of the session data.

Common follow-ups: What are the memory tradeoffs between these two approaches for very large objects?;Does RedisJSON change this decision for applications that need JSON specifically?

RedisJSON;Redis Memory Optimization

How does the internal listpack encoding for small hashes affect memory usage, and at what point does Redis switch to a different, less compact encoding?

Advanced
Redis stores small hashes using a highly compact listpack encoding by default, which uses significantly less memory than the alternative, but automatically switches to a full hash table encoding once the hash exceeds a configurable number of fields or a configurable maximum field value size, trading some memory efficiency for better performance on larger hashes.
CONFIG GET hash-max-listpack-entries
CONFIG GET hash-max-listpack-value
OBJECT ENCODING user:100
Real-world example A team storing millions of small user preference hashes carefully tunes the listpack thresholds to keep as many hashes as possible in the compact encoding, achieving significant memory savings across their entire dataset.

Common follow-ups: What are reasonable threshold values for a typical application's hash sizes?;What performance difference exists between listpack and hash table encoding?

Redis Memory Optimization;Data Types

How do you retrieve only the field names or only the values from a Redis hash, without needing both together?

Intermediate
You use HKEYS to retrieve just the list of field names currently stored in a hash, and HVALS to retrieve just the list of corresponding values, which can be useful when you only need one or the other rather than the complete set of field value pairs that HGETALL would return.
HKEYS user:100
HVALS user:100
Real-world example A data export tool uses HKEYS to first discover what fields exist across different user hashes with varying schemas, before deciding which specific fields to actually extract and include in its export.

Common follow-ups: Is there a performance difference between HKEYS, HVALS, and HGETALL?;How do you get just the count of fields in a hash without retrieving the data itself?

Data Types;Redis CLI & Basic Commands