Redis · Chapter 6 of 26
Sets
By the end of this chapter you can keep a group of fans in a set, where each fan is in it once however often they are added, ask whether a fan is in it, count it, and answer questions about two or more groups at once: who is in both, who is in either, and who is in one but not the other. 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
Tonight's opener has five contests, and Rhea wants three numbers on her screen before the toss: how many different fans have joined the Mega Contest, which fans play both the Mega Contest and the Pune Fans League (she wants to thank them), and which Mega Contest fans have joined nothing else tonight (she wants to show them the smaller contests).
A counter from chapter 1 cannot answer any of these. Fans tap Join twice on a slow network, and an INCR counts both taps. A list from chapter 5 keeps both taps too, and to find the fans in two contests you would read both lists and compare 240 names against 17 in your own code. What GullyXI needs is a value that holds each fan once, can say in one step whether a fan is in it, and can compare two groups inside Redis.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
mega-joined.redis: the Mega Contest's fans, each once
Keep the fans who join the Mega Contest in a set under the key contest:71:fans, which does not exist yet. Add each tap on Join with its own SADD of that one fan, in this order: u1047, u1064, u1047 again (a second tap on a slow network), u1037. So the file has four SADDs on contest:71:fans.
Then u1064 leaves: remove that fan from the set. Then ask how many fans the set holds (it answers 2), and, with one SMISMEMBER, whether u1047 and u1064 are in it, asked in that order (it answers 1, then 0). The set ends holding u1037 and u1047.
Do not DEL the key. The Check runs your file in an empty Redis.
- Task 2
both-leagues.redis: fans in both leagues
The Check first makes match 2001's five contests as sets, contest:71:fans to contest:75:fans, from entries.json (setup.redis makes the same in your own Redis). Rhea wants the fans who play both the Mega Contest (contest:71:fans, 240 fans) and the Pune Fans League (contest:74:fans, 17 fans).
Ask for the fans in both sets with one SINTER (any order of the two keys and of the reply). Then count them with one SINTERCARD on the same two keys. It answers 7.
Do not store the answer: the file must leave the five contest sets as they are and make no new key.
- Task 3
mega-only.redis: only the Mega Contest
With the same five contests (the Check makes them first), find the fans in contest:71:fans who are in none of contest:72:fans, contest:73:fans, contest:74:fans and contest:75:fans, and store them in one SDIFFSTORE as a new set under the key contest:71:only (Redis answers 126).
Then ask how many fans contest:71:only holds with SCARD. It answers 126.
The five contest sets must stay as they are.
- Task 4 · interview question
Interview: unique-fans.redis, unique fans counted once
Rhea's dashboard shows how many different fans opened the match centre tonight, counted in stats:2001:unique_fans with INCR on every open. unique-fans.redis replays five opens, u1101, u1306, u1101 (a refresh), u1253, u1306 (a refresh), and the count ends at 5, though only 3 fans opened it.
Fix the file: record each of the five opens, in the same order, with its own SADD of that fan's id into a set under the key stats:2001:fans_seen. Then ask how many fans the set holds with SCARD. It answers 3.
Use no INCR at all, and do not make stats:2001:unique_fans. The Check runs your file in an empty Redis.
Farhan will ask: why does the counter count u1101 twice, why can the set not, and what does SADD's reply tell the app about each open?
What you will learn
- SADD and SREM: add and remove members; only new members count.
- SCARD, SISMEMBER and SMISMEMBER: how many, and is this fan in it.
- SMEMBERS: every member, in no order, and why not on a huge set.
- SINTER, SUNION and SDIFF: in both, in either, in one but not the others.
- The STORE forms and SINTERCARD: keep a result as a new set, or just count it.
- SRANDMEMBER and SPOP: a random member, for a lucky draw.
More in Redis from the command line
Build "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.