Redis · Chapter 26 of 26
The interview
By the end of this chapter you have answered five timed questions, one idea each, from across the whole lab: paying out once, the top picks of a sliding hour, an exact sliding-window limit, a cache that never stampedes across app servers, and a count that stays exact through retries and a crash. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Tested
- 5 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 season went well, and GullyXI is hiring a second backend engineer. Farhan runs the interviews: "Short problems, ten minutes each. I am not checking that you remember every option of every command; the docs have them. I am checking that you know what Redis does inside: one command at a time, what a reply means, what a crash keeps." Rhea liked the round so much that it is now the last chapter of the lab.
Each question is one idea from an earlier chapter, with no hand-holding: the file has the goal, the Check has the edge cases. Below is a refresher of each idea, with what Farhan listens for. The answers are not here.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1 · interview question
Interview: idempotency.js, pay out once
Time yourself: 10 minutes. In idempotency.js, once(redis, key, fn, ttlSeconds) (ttlSeconds 86400 when left out) runs fn only once per key. The first call claims idem:<key> for ttlSeconds, runs fn, keeps its result there (the TTL stays about ttlSeconds) and returns it. A later call with the same key returns the kept result without running fn. A call while fn is still running throws an Error whose message has in progress in it. When fn throws, once forgets the key and rethrows the same error, so a retry runs fn again.
The Check runs five calls with one key at the same moment (fn must run once: one returns the result, four throw in progress), then reads the TTLs, and retries a payout that failed.
Farhan will ask: why does reading the key and then setting it pay twice, and which one command claims a key?
- Task 2 · interview question
Interview: trending.js, trending picks this hour
Time yourself: 10 minutes. In trending.js, recordPick(redis, playerId, now) records one pick at now (ms), and topPicks(redis, k, now) returns the k most picked players over the last 60 whole minutes, the current one included (the minute is now / 60000 rounded down), as [{ player, picks }] with picks a number, most first, equal counts in the order a reverse ZRANGE gives, and [] when there are none. Key names are yours, but every key left in Redis must expire within two hours (7200 seconds).
The Check records 90 minutes of picks, then asks at the end of minute 89 (minutes 30 to 89 count), at the start of minute 59 (minutes 0 to 59), and long after the last pick.
Farhan will ask: why a sorted set a minute, and what does that cost against a sorted set of every single pick?
- Task 3 · interview question
Interview: sliding-limit.js, a sliding window in one script
Time yourself: 10 minutes. In sliding-limit.js, allow(redis, fanId, now, limit, windowMs) (limit 5 and windowMs 60000 when left out) returns true when the fan has had fewer than limit allowed requests with a time after now - windowMs, and then counts this one; otherwise false, and a refused request does not count. The window is the sorted set limit:<fanId>, which must expire within windowMs.
Each call sends one script and nothing else (EVAL or EVALSHA; the Check records the commands your function sends). Five requests at one moment are allowed and the sixth is not; at now + 59999 still refused; at now + 60000 allowed. 20 requests at one moment from one fan allow exactly 5. A limit of 3 in 10 seconds works too.
Farhan will ask: why does chapter 11's window need a script to be exact, and what does the script cost Redis?
- Task 4 · interview question
Interview: never-stampede.js, a cache that never stampedes
Time yourself: 10 minutes. In never-stampede.js, getPage(redis, db, matchId) returns page:match:<matchId> parsed from JSON when it is there. On a miss, only the app server that takes the lock lock:page:match:<matchId> (a random token, a lease) builds the page with db.getMatchCentre, saves it as JSON with EX 30, and releases its own lock. The others wait for the page to appear and return it; a waiter that has waited 2 seconds builds the page itself. A match that does not exist returns null, saves nothing and leaves no lock.
The Check loads your file three times, as three app servers that share nothing in memory, each with its own client, and opens match 2001 ten times at once from each: one database call in all, the lock gone afterwards.
Farhan will ask: what happens to the waiters if the server holding the lock dies, and why does the lock need a lease?
- Task 5 · interview question
Interview: exact-counter.js, a count exact across a crash
Time yourself: 10 minutes. In exact-counter.js, countEntry(redis, contestId, entryId) counts an entry of a contest and returns the contest's count after it; the same entry sent again counts once. entryCount(redis, contestId) returns the count, 0 for a contest with none.
The Check runs Redis with appendonly yes and appendfsync always, counts entries 1 to 50 of contest 71 and sends 41 to 50 again, crashes Redis, starts another on the same folder, then counts 52 to 60 and sends 55 to 60 again: the count must be exact every time.
Farhan will ask: what would appendfsync everysec lose here, and why is INCR alone never exact with retries?
What you will learn
- How to work a timed question: say the plan, build the smallest thing that runs, then try the edges.
- Five ideas again, at interview depth: claiming a key (chapters 2, 3 and 19), sorted sets over time (7 and 11), scripts (18), locks across processes (10 and 19), and counts that survive (6, 8 and 21).
More in Tested
Build The interview 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.