Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

RD
Redis
0/10 completed
Beginner
What is Redis?Strings, numbers and keysExpiry and TTL
Intermediate
Lists and hashesSets and sorted setsCaching patterns
Advanced
Rate limiters, locks and transactionsPub/Sub and Streams
Pro
Memory, eviction and persistenceReplication, Sentinel and Cluster
Tracks / Redis / Intermediate

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).

redis-cli (simulator) · editable

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:

redis-cli (simulator) · editable

Other list commands

redis-cli (simulator) · editable

Lists are fast at the ends but slow in the middle: LINDEX and LSET deep 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:

redis-cli (simulator) · editable

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:

redis-cli (simulator) · editable

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.

redis-cli (simulator) · editable

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

redis-cli (simulator) · editable

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:

redis-cli (simulator) · editable

That's why storing millions of small objects as hashes is very memory-efficient.

Practice

  1. Build a capped list logins:asha that keeps only the 3 most recent login times.
  2. Model a product as a hash with name, price and stock, then atomically decrease stock by 2.
  3. Using two clients, have [worker] wait on a emails queue with BLPOP, and [app] push an email job.
redis-cli (simulator) · editable
✎ Check your understanding

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?

← Expiry and TTL