Difficulty: Beginner
How do you set a key to expire in 60 seconds in Redis, and how do you check or change its remaining time?
Use EXPIRE key 60 on an existing key, or set the value and the expiry together with SET key value EX 60. After 60 seconds Redis deletes the key automatically. You can check the remaining time with TTL key (seconds) or PTTL key (milliseconds).
| Command | What it does |
|---|---|
SET key value EX 60 | Set value and 60-second TTL atomically |
SET key value PX 60000 | Same, in milliseconds |
EXPIRE key 60 | Add a TTL to an existing key |
PEXPIRE key 60000 | Same, in milliseconds |
EXPIREAT key 1760300000 | Expire at a Unix timestamp in seconds |
PEXPIREAT key ... | Expire at a Unix timestamp in milliseconds |
GETEX key EX 60 | Read the value and reset its TTL (6.2+) |
PERSIST key | Remove the TTL, key lives forever |
Prefer SET ... EX when creating a cache entry. Running SET then EXPIRE as two commands leaves a gap where a crash could leave a key with no TTL at all.
TTL key returns the seconds remaining.-1 if the key exists but has no expiry.-2 if the key does not exist.EXPIRETIME key (7.0+) returns the absolute Unix timestamp at which the key will expire.SET key newvalue removes the existing expiry. Use SET key newvalue KEEPTTL (6.0+) to keep it.INCR, HSET, LPUSH, APPEND and similar commands keep the existing TTL.EXPIRE with a zero or negative value deletes the key immediately.EXPIRE accepts NX (only if no TTL), XX (only if a TTL exists), GT and LT (only if the new TTL is greater or less). EXPIRE session:9 1800 GT extends a session without ever shortening it.HEXPIRE, HTTL and HPERSIST.RENAME keeps the TTL with the renamed key.Redis does not run a timer per key. It uses two mechanisms:
hz, default 10), samples keys that have a TTL, and deletes expired ones. If many in the sample were expired, it repeats.So an expired key might sit in memory briefly, but no client will ever read it. Watch expired_keys in INFO stats to see how many keys expire.
SET otp:9876543210 482913 EX 300.EXPIRE ... GT.INCR a counter and set EXPIRE only when it was just created.redis-cli SET otp:9876543210 482913 EX 60 # value + 60s TTL atomically
redis-cli TTL otp:9876543210
redis-cli SET page:home '<html>'
redis-cli EXPIRE page:home 60 # add TTL to existing key
redis-cli PTTL page:home
redis-cli SET page:home '<html v2>' # plain SET clears TTL!
redis-cli TTL page:home
redis-cli SET page:home '<html v3>' EX 60
redis-cli SET page:home '<html v4>' KEEPTTL # keep remaining TTL
redis-cli TTL page:home
redis-cli TTL does:not:exist
TTL returns -1 for a key with no expiry and -2 for a missing key. KEEPTTL preserves the expiry when you overwrite a value.
import Redis from 'ioredis';
const redis = new Redis();
// Allow 100 requests per user per 60-second window
async function allow(userId) {
const key = `rl:${userId}:${Math.floor(Date.now() / 60000)}`;
const [[, count]] = await redis
.multi()
.incr(key)
.expire(key, 60, 'NX') // set TTL only on first hit (Redis 7+)
.exec();
return count <= 100;
}
INCR keeps any existing TTL, and EXPIRE with NX only sets it when none exists, so the window is not extended on every request.
EXPIRE, SET EX, TTL and PTTL, PERSIST, Lazy and Active Expiry