Difficulty: Beginner
How do you make a Redis key expire at a specific point in time (for example, midnight tonight) rather than after a relative number of seconds?
Use EXPIREAT key <unix-seconds> (or PEXPIREAT key <unix-milliseconds>) to give a key an absolute deadline. If you are writing the value at the same time, SET key value EXAT <unix-seconds> (Redis 6.2+) sets the value and the deadline in one atomic command. Redis deletes the key once the server clock passes that timestamp.
| Command | Unit | What it does |
|---|---|---|
EXPIREAT key ts | seconds | Set absolute expiry on an existing key |
PEXPIREAT key ts | milliseconds | Same with millisecond precision |
SET key val EXAT ts / PXAT ts | s / ms | Write value and absolute expiry together (6.2+) |
GETEX key EXAT ts | seconds | Read a value and set its absolute expiry (6.2+) |
EXPIRETIME key / PEXPIRETIME key | s / ms | Return the absolute expiry timestamp (7.0+) |
TTL key / PTTL key | s / ms | Return remaining time to live |
EXPIREAT returns 1 if the timeout was set and 0 if the key does not exist (or the condition below was not met). If the timestamp is already in the past, the key is deleted immediately and the command still returns 1.
EXPIRE, EXPIREAT and their millisecond variants accept one option:
NX: set expiry only if the key has noneXX: set expiry only if the key already has oneGT: set only if the new expiry is later than the current oneLT: set only if the new expiry is earlier than the current oneGT is handy when several code paths extend a deadline and you never want one of them to shorten it by accident.
EXPIREAT.EXPIRE and SETEX are written to replicas and the AOF as absolute PEXPIREAT, so a replayed AOF does not extend keys' lives.Date.now() in JavaScript returns milliseconds. Passing it to EXPIREAT sets an expiry thousands of years in the future. Divide by 1000 or use PEXPIREAT.SET on the key later clears the expiry unless you pass KEEPTTL (6.0+) or a new expiry.# Expire at 2026-12-31 18:30:00 UTC (midnight IST on 1 Jan 2027)
SET sale:price:sku123 49900
EXPIREAT sale:price:sku123 1798741800
# Set value and deadline in one command (Redis 6.2+)
SET sale:banner "Year end sale" EXAT 1798741800
# Inspect (EXPIRETIME needs Redis 7.0+)
EXPIRETIME sale:banner
TTL sale:banner
EXPIREAT works on an existing key, SET ... EXAT does both steps atomically. TTL output depends on when you run it.
import Redis from 'ioredis';
const redis = new Redis();
// Next midnight in Asia/Kolkata (UTC+5:30) as UNIX seconds
function nextIstMidnightSeconds() {
const IST_OFFSET_MS = 5.5 * 60 * 60 * 1000;
const nowIst = new Date(Date.now() + IST_OFFSET_MS);
nowIst.setUTCHours(24, 0, 0, 0); // midnight of the next IST day
return Math.floor((nowIst.getTime() - IST_OFFSET_MS) / 1000);
}
export async function countPayment(userId) {
const key = `quota:payments:${userId}`;
const [[, count]] = await redis
.multi()
.incr(key)
.expireat(key, nextIstMidnightSeconds(), 'NX') // NX flag needs Redis 7.0+
.exec();
return count;
}
MULTI keeps the increment and expiry together. Note the division by 1000: EXPIREAT takes seconds, while Date.now() gives milliseconds.
EXPIREAT, PEXPIREAT, UNIX timestamp, EXPIRETIME, SET EXAT