Redis · Chapter 7 of 26
Sorted sets
By the end of this chapter you can keep a board in a sorted set, where every member has a score and Redis keeps them in order, read the top of it and any player's place, add points as they arrive, ask by score instead of by place, and say exactly who comes first when two players have the same points. 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
The match centre shows a points board for match 2001: each player's fantasy points so far, most first. Fans refresh it after every ball, so on match night it is read thousands of times a second, and it changes whenever a player scores. Venkat needs it exact, because prizes are paid from it.
A set from chapter 6 has no order and no numbers. A string per player (chapter 1) can count with INCRBY, but to show the top three you would read every player's string and sort them in your code, on every refresh. A list (chapter 5) keeps an order, but only the order you pushed in: when a player scores you would have to find him and move him yourself. What the board needs is a value where each member carries a number, and Redis keeps the members sorted by it as the numbers change.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
top-three.redis: the top three, with their points
Put match 2001's fantasy points from points.json on a board, a sorted set under the key match:2001:points, with one ZADD: P123 7, P124 202, P111 25, P125 50, P115 25, P126 116, P110 25, P127 18 (each player is a member and his points are its score).
Then read the top three, most points first, with their points, in one ZRANGE on match:2001:points. It answers P124 202, P126 116, P125 50: six lines, each player followed by his points.
The Check runs your file in an empty Redis.
- Task 2
player-rank.redis: a player's place and points
The Check first makes the board match:2001:points with the eight players' points (setup.redis makes the same in your own Redis). Ask Redis three things about it: Yash Patil's (P125) place counting from the top with ZREVRANK (it answers 2, since places count from 0), his points with ZSCORE (it answers "50"), and how many players have at least 25 points, 25 included, with ZCOUNT (it answers 6).
The board must stay as it is.
- Task 3
six.redis: a six, and a new batter
On the board match:2001:points (the Check makes it first, with P127 on 18 and P124 on 202): Sai Menon (P127) hits a six, so add 12 points to his score with one ZINCRBY (it answers "30"). Aarav Shaikh (P128) walks in: put him on the board with 0 points with a ZADD that adds only members not yet on it (it answers 1). The scorer's app sends the same new-batter message for Dhruv Rathore (P124) by mistake: send that ZADD of P124 with 0 too, also adding only if missing (it answers 0, and his 202 stay).
Then ask how many players the board holds with ZCARD. It answers 9.
Both new-batter ZADDs must use the option that adds only members that are missing: a plain ZADD of P124 with 0 would wipe his points.
- Task 4 · interview question
Interview: ties.redis, three players on 25 points
On the board match:2001:points (the Check makes it first), P110, P111 and P115 all have exactly 25 points. Ask Redis for the players with exactly 25 points, lowest first, in one ZRANGE by score (not by place): it answers P110, P111, P115. Then ask for P115's place counting from the top with ZREVRANK (it answers 3), and P110's place counting from the top with ZREVRANK (it answers 5).
The board must stay as it is.
Farhan will ask: three players have the same score. Who is shown first on the top-first board, and why that one?
What you will learn
- ZADD and ZINCRBY: put members on the board with a score, and add to a score.
- ZSCORE, ZCARD, ZRANK and ZREVRANK: a member's score, the board's size, a place.
- ZRANGE: read by place or by score, with REV, BYSCORE, LIMIT and WITHSCORES.
- ZCOUNT, ZREM and ZREMRANGEBYRANK: count a score range, take members off.
- ZADD's NX, XX, GT and INCR: add or change only when you mean to.
- Equal scores: the order Redis gives them, and why.
More in Redis from the command line
Build "Sorted sets" 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.