Redis · Chapter 17 of 26
Pipelines, many commands, one round trip
By the end of this chapter you can say what a round trip costs and measure it, send many commands in one round trip with an ioredis pipeline, read its [err, value] results and handle the one that failed, and choose between a pipeline and MULTI. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Redis in production
- 3 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
Before the toss, GullyXI loads every fan's card into Redis, and the contest page reads the cards of the fans on screen. Gurpreet timed the loader on match night: it wrote one fan, waited for Redis to say OK, wrote the next, waited again. Redis was idle almost the whole time; the time went on waiting.
Every command you have sent so far, from redis-cli or with await redis.get(...), makes a round trip: the command travels to Redis, Redis does a few microseconds of work, the reply travels back, and only then does your code send the next command. On the same machine a trip is tens of microseconds; between two machines in a data centre it is more, and on match night every one of them is paid two lakh times over. The Redis commands you know cannot help: each one is its own trip. What is missing is a way to send many commands without waiting for each reply, and to get all the replies back together.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
load-fans.js: load 400 fans with one pipeline
Export loadFans(redis, fans). For every fan in the list (gullyxiData().fans, 400 of them), write the hash fan:<id> with four fields, the fan's own values: name, handle, city and joined. fan:u1001 becomes { name: 'Ananya Mishra', handle: 'ananya347', city: 'Nagpur', joined: '2025-02-12' }.
Send every write in one pipeline, run once: the Check counts the pipelines you make (exactly 1) and the hash writes sent one by one outside it (none). Return how many fans were written: the results that came back without an error, 400 for the full list.
- Task 2
fan-cards.js: read many, and handle the one that fails
Export readFanCards(redis, ids). Read the whole hash fan:<id> for every id, all in one pipeline (the Check counts exactly 1 pipeline and no hash reads sent one by one), and return { cards, missing, failed }:
cards is an object from each found fan's id to its hash as an object ({ name, handle, city, joined }); missing is the list of ids that have no fan:<id> key, in the order asked; failed is the list of ids whose read answered an error, in order. For no ids, return { cards: {}, missing: [], failed: [] }.
The Check loads the first 20 fans, turns fan:u1013 into a plain string (an old bug), and asks for u1001, u1013, u1999, u1002, u1888 and u1020: u1013 is failed, u1999 and u1888 are missing, and the other three are cards.
- Task 3 · interview question
Interview: pay-entry.js, pipeline or MULTI
payEntryFee(redis, fanId, contestId, feePaise) takes the fee out of the string wallet:<fanId> (paise), adds it to contest:<contestId>:prize_pool, and adds fanId to the set contest:<contestId>:fans. It returns the wallet after the fee, as a number. With wallet:u1010 at 50000 and contest:74:prize_pool at 170000, paying 9900 returns 40100 and leaves the pool at 179900.
It sends the three commands in a plain pipeline. The numbers come out right, but another client can run its commands between them: Venkat's audit once saw the fee in neither place. Fix it: keep one round trip, and make the three run together with nothing from another client in between, using MULTI and EXEC (the Check counts one MULTI and one EXEC for one payment, and no WATCH).
Farhan will ask: pipeline or MULTI, which one, and why?
What you will learn
- A round trip: what it costs, measured in the lab.
- MGET: the simplest batch, many keys in one command.
- pipeline() and exec(): many commands in one round trip.
- [err, value] pairs: one result per command, and an error that stops nothing.
- multi() or pipeline(): atomic, or only batched.
- enableAutoPipelining: ioredis batching for you, in a line.
More in Redis in production
Build "Pipelines, many commands, one round trip" 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.