Lists and hashes
Strings hold one value. Lists hold an ordered sequence of values, and hashes hold a set of named fields, like a small object. Together they cover queues, activity feeds, user profiles, shopping carts and much more.
Lists
A Redis list is a sequence of strings that you can push to and pop from at both ends, very fast. L commands work on the left (head) and R commands on the right (tail).
LRANGE key 0 -1 means "from the first element to the last". Negative indexes count from the end, so -1 is the last element.
Queue or stack?
| Pattern | Push with | Pop with |
|---|---|---|
| Queue (first in, first out) | RPUSH |
LPOP |
| Stack (last in, first out) | LPUSH |
LPOP |
Capped lists: "the 5 most recent"
LTRIM keeps only a range. Push, then trim, and the list can never grow beyond a fixed size, which is perfect for activity feeds and recent-items lists:
Other list commands
Lists are fast at the ends but slow in the middle:
LINDEXandLSETdeep inside a long list must walk to that position. For random access, use a hash or sorted set.
Blocking pops: a job queue with workers
A worker could poll a queue with LPOP every second, but that wastes time and CPU. BLPOP blocks until an item arrives, then returns it immediately.
To show this, the simulator lets you run commands from several clients: start a line with [name] to send it from a connection called name. Here a worker waits, and a web server (the producer) adds jobs:
The worker's reply appears tagged [worker] the moment the job is pushed. This is how many background job libraries (like BullMQ for Node.js and RQ for Python) work, on top of Redis.
Reliable queues with LMOVE
If a worker crashes after popping a job, the job is lost. LMOVE (or BLMOVE) moves the job atomically to a processing list instead; the worker removes it from there only when it's done:
If a worker dies, its jobs are still in processing for a cleanup process to retry. (For anything more serious, Redis Streams, covered later, have this built in.)
Hashes
A hash is a key containing field → value pairs: a natural fit for objects like a user profile or a product.
HSET returns the number of new fields added (updating an existing field returns 0 for that field).
Hash or JSON string?
You could also store an object as a JSON string with SET user:1001 '{"name":"Asha",…}'. Compare:
| Hash | JSON string | |
|---|---|---|
| Read one field | HGET (cheap) |
Fetch and parse everything |
| Update one field | HSET / HINCRBY, atomic |
Read, modify, write back (race conditions!) |
| Nested objects | No (flat fields only) | Yes |
Use a hash for flat objects whose fields change independently (counters, profiles, carts). Use a JSON string for nested data that you always read and write as a whole. (Redis 8 also includes a native JSON type with JSON.SET and JSON.GET for nested documents.)
A shopping cart
The whole cart expires a week after the last update, or whenever you choose.
Memory-efficient small hashes
Small hashes (up to 128 fields with short values, by default) use a compact listpack encoding:
That's why storing millions of small objects as hashes is very memory-efficient.
Practice
- Build a capped list
logins:ashathat keeps only the 3 most recent login times. - Model a product as a hash with
name,priceandstock, then atomically decreasestockby 2. - Using two clients, have
[worker]wait on aemailsqueue withBLPOP, and[app]push an email job.
1.Which pair of commands gives you a first-in, first-out queue?
2.What does LTRIM feed 0 99 do?
3.Why use a hash instead of a JSON string for a user's visit counter?