Redis · Chapter 3 of 26
Keys that expire
By the end of this chapter you can give a key a time to live with EXPIRE or with SET's EX and PX, read what is left with TTL and PTTL (and say what -1 and -2 mean), remove or move an expiry with PERSIST and EXPIREAT, keep a TTL with KEEPTTL when a value changes, use EXPIRE's NX and GT, and explain how Redis gets rid of expired keys. 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
Fans log in to GullyXI with a six-digit code sent by SMS, a one-time password (OTP). A code must stop working after five minutes: one that works forever is a door left open. In chapter 1, lab page saved the match centre in Redis, but the copy stayed there for good. On match night the page changes with every ball, and a copy that never goes away keeps showing fans the toss long after the first wicket.
With only chapter 1 and 2's commands, someone would have to come back and DEL each code and each page at the right moment: a job that runs every few seconds, looks at every key and decides. It would miss keys when it fell behind, and the two lakh codes sent before a big match would pile up in memory. Gurpreet would rather Redis did it. It can: every key can carry a time to live (TTL), and Redis deletes the key by itself when the time runs out.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
otp.redis: a login code that expires
Fan u1001 asked for a login code (an OTP). Store it as a string: the key otp:u1001 holds 482913. Then give the key a time to live of 300 seconds with EXPIRE, as a second command after the SET.
End with TTL otp:u1001, which answers 300 (the Check accepts 295 to 300).
The Check runs your file in an empty Redis.
- Task 2
cache-page.redis: cache the page for 30 seconds in one command
Cache a small match centre page for 30 seconds. The key page:match:2001 holds exactly this JSON text: {"match":2001,"status":"live"}
Store it and give it its 30 second TTL in one SET command: no EXPIRE or PEXPIRE.
End with PTTL page:match:2001, which answers the milliseconds left, a little under 30000 (the Check accepts 25000 to 30000).
- Task 3
keep-ttl.redis: change the score, keep its TTL
The live score of match 2001 is cached under score:match:2001 with a 60 second TTL; it holds MUM 124/2 (10.0). The next over ends, so change it to MUM 137/2 (11.0), all of it, spaces included.
The key must keep the TTL it already has: use the SET option that keeps the time to live, and no EXPIRE (do not give it a new one).
End with TTL score:match:2001, which answers a number of seconds (the Check accepts 50 to 60), not -1. The Check makes the key with its 60 seconds first; setup.redis does the same in your own Redis.
- Task 4 · interview question
Interview: ttl-codes.redis, a resent code that never expires
Fan u1005, Meera Joshi, got a login code two minutes ago: otp:u1005 has 120 seconds left. She asked for a new one, 579380, and the app wrote it with a plain SET. ttl-codes.redis replays that, then asks for three TTLs. Its first TTL answers -1: the new code would never expire.
Fix the file: store 579380 in otp:u1005 with a fresh 300 second TTL (the Check accepts 295 to 300). Keeping the old 120 seconds is wrong: a new code gets its full five minutes.
Keep the three TTLs, in this order: TTL otp:u1005 answers about 300, TTL fan:u1005:name answers -1 (a key with no TTL) and TTL otp:u1006 answers -2 (a key that does not exist). The Check makes otp:u1005 and fan:u1005:name first; setup.redis does the same in your own Redis.
Farhan will ask: what do -2, -1 and a positive number from TTL each mean, and why did the resent code never expire?
What you will learn
- EXPIRE, TTL and PTTL: give a key seconds to live, and read what is left.
- SET with EX and PX: store a value and its TTL in one command.
- PERSIST and EXPIREAT: take a TTL away, or expire at a set moment.
- KEEPTTL: change a value without losing its TTL.
- EXPIRE's NX and GT: change a TTL only in some cases.
- How Redis expires keys: on access, and a few at a time in the background.
More in Redis from the command line
Build "Keys that expire" 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.