Ch 3 · Redis Lists · Redis

Topic 3 of 5 in Redis — Complete Tutorial (Beginner) — 4 lessons.

List Operations

Use Redis lists for queues, stacks, and ordered collections. A list keeps items in the order you add them, and you can push or pop at either end. That makes it ideal for a queue where jobs wait their turn. Look out for RPUSH and LPUSH for adding, LPOP and RPOP for removing, and BLPOP, which waits until an item arrives.

List Operations in Code

The code builds a tasks list, adding three jobs to the right and an urgent one to the left so it jumps the queue. Check the order LRANGE prints with 0 and -1, which means the whole list. Then see how LPOP and RPOP take from opposite ends, LLEN counts what remains, and LTRIM caps a log at its latest 100 entries.

Practice: List Operations

Here you build a simple task queue of your own. You will add several jobs, look at the whole queue without changing it, take one job off to process it, and stop the list growing past 100 items. A correct answer shows first-in, first-out behaviour: the job you added first is the first one you remove.

Solution & Challenge: List Operations

Compare your queue with the worked solution, which adds three named jobs, prints the queue, processes the oldest one and caps the list at 100 items. The challenge then turns it into a priority queue: urgent jobs jump to the front with LPUSH, normal ones join the back with RPUSH, and a worker waits for work using BLPOP and logs each task.

All topics in Redis Beginner

  1. Redis Hashes
  2. Redis Introduction and Setup
  3. Redis Lists
  4. Redis Sets
  5. Redis Strings and Numbers