A Map Inside a Key
A Redis hash stores a set of field value pairs under one key. Think of it as a small object: a user key might have fields name, email, and age. You read and write individual fields without touching the rest.
Key commands:
- HSET sets one or more fields.
- HGET reads one field.
- HGETALL returns every field and value.
- HINCRBY atomically adds to a numeric field.
Why Not Just a String of JSON
You could serialize an object to JSON and store it as a string, but then updating one field means reading the whole blob, parsing it, editing, and writing it back. A hash lets you update age alone with a single HSET, atomically and without moving the other fields over the wire.
Memory and Limits
For small hashes, Redis uses a compact encoding that is very memory efficient. As the hash grows past configured thresholds it switches to a full hash table. Hashes are flat: values are strings, so deeply nested objects need either flattening or multiple keys.
Use a hash whenever you have a record with several fields you read or write independently.
Key idea
A hash stores field value pairs under one key so you update individual object fields atomically instead of rewriting a whole serialized blob.