Redis · Chapter 5 of 26

Lists

By the end of this chapter you can keep items in order in a Redis list: add them at either end with RPUSH and LPUSH, take them off with LPOP and RPOP, read any part with LRANGE and LINDEX, count them with LLEN, keep a list short with LTRIM, remove and move items with LREM and LMOVE, wait for an item with BLPOP, and use a list as a queue or a stack. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Redis from the command line
  • 4 tasks
  • Redis
  • In the all labs plan

The first 2 chapters of Redis are free. This one comes with the all labs plan.

Why you need this

Order matters all over GullyXI on match night. The ball-by-ball strip shows the balls of the over in the order they were bowled. The ticker on the Mega Contest shows the fans who joined most recently, newest first. And after every over the app has hundreds of fantasy teams to score again: those jobs must wait in line and be done oldest first, by whichever worker is free.

Strings and hashes have no order. You could keep a counter and keys like ball:2001:1, ball:2001:2, but then reading the last six balls means working out six key names, removing the oldest job means finding it, and two workers can grab the same job. Gurpreet has seen a points job run twice and a fan's score double. A Redis list is an ordered sequence of strings under one key, with commands that add and take at both ends in one step each.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    this-over.redis: an over, ball by ball, in order

    Keep Mumbai's sixth over (balls 5.1 to 5.6) as a list under match:2001:over:5, in the order the balls were bowled: 1, 6, 0, 0, 0, W. Add all six with one RPUSH command (Redis answers 6).

    Then read the whole list back with LRANGE match:2001:over:5 0 -1, and read the last ball with one LINDEX on match:2001:over:5 (it answers "W").

    The list must hold exactly those six items, first ball first. The Check runs your file in an empty Redis.

  2. Task 2

    points-queue.redis: a queue of jobs, served in order

    After an over, GullyXI scores fantasy teams one job at a time from a queue, the list queue:points. Add four jobs with one RPUSH, in this order: entry:21, entry:22, entry:23, entry:24.

    Then serve them from the front, oldest first: one worker takes one job with LPOP (it answers "entry:21"), a second worker takes the next two with one LPOP and a count of 2 (it answers entry:22 and entry:23), then LLEN queue:points answers 1, and one more LPOP answers "entry:24".

    Last, a worker waits for a new job with BLPOP queue:points and a timeout of 1 second (no more): nothing comes, so after a second it answers (nil). At the end queue:points no longer exists. The Check runs your file in an empty Redis.

  3. Task 3

    recent-joins.redis: the newest joins first

    The Mega Contest shows a ticker of the fans who joined most recently, newest first, in the list contest:71:recent. It holds four fans now, newest first: u1143, u1037, u1064, u1047.

    Three more fans join, in this order: u1179, then u1011, then u1169. Add all three to the front with one LPUSH command, so the newest, u1169, ends up first.

    Then read the ticker's five newest with LRANGE contest:71:recent 0 4, which answers u1169, u1011, u1179, u1143, u1037. Keep all seven fans in the list: remove nothing. The Check makes the four fans first; setup.redis does the same in your own Redis.

  4. Task 4 · interview question

    Interview: last-six.redis, a list that never grows

    The app shows the last six balls from the list match:2001:last6, which holds the six balls of over 17.1 to 17.6: 1, 0, 0, 0, 2, 1. The feed adds each new ball with RPUSH and trims the list to the last six. last-six.redis replays the next four balls (2, W, 0, 1), but it trims only once, at the end: the pushes answer 7, 8, 9 and 10, so a phone reading in between sees too many balls, and if the feed stopped before the trim the list would keep growing.

    Fix the file: keep one RPUSH per ball, in that order, and trim the list to its last six items with LTRIM after every push, so no RPUSH ever answers more than 7. At the end the list holds 2, 1, 2, W, 0, 1. The Check makes the six balls first; setup.redis does the same in your own Redis.

    Farhan will ask: why trim after every push instead of once at the end, and why is that trim cheap?

What you will learn

  • RPUSH and LPUSH: add items at the tail or the head.
  • LRANGE, LINDEX and LLEN: read a range, one item, or the length, with negative positions.
  • LPOP and RPOP: take items off either end, one or several.
  • LTRIM: keep only part of a list, for a list that never grows.
  • LREM and LMOVE: remove items by value, and move one between lists.
  • BLPOP: wait for an item, with a timeout; a list as a queue or a stack.

More in Redis from the command line

Build "Lists" yourself

GullyXI, a Pune fantasy cricket app, falls over on match night. You make it fast with Redis. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Redis are free. This one comes with the all labs plan.