Redis 8.12 reached general availability in Redis Open Source on 22 November 2026. The feature many developers have wanted for years is finally here: you can set an expiration on individual members of a Set, not just on the whole key. The release also adds a BLESS command to protect keys from eviction, compressed bitmaps and atomic sorted-set moves. This tutorial focuses on the new Set TTL commands with practical patterns, then covers eviction protection.

What’s new in Redis 8.12

From the Redis 8.12.0 release notes, the major changes compared with 8.10 include:

  • Set member expiration: SEXPIRE, SPEXPIRE, SEXPIREAT, SPEXPIREAT to set TTLs; STTL, SPTTL, SEXPIRETIME, SPEXPIRETIME to read them; and a command to remove a member’s TTL
  • BLESS to keep specified keys from being evicted
  • Compressed bitmaps, with redis-roaring integrated into core for lower memory use, plus a new BITRANGEINTARRAY command
  • A new B+ tree encoding that substantially reduces memory for large sorted sets
  • ZMOVE, BZMOVE and ZMOVEMEMBER for atomic sorted-set moves
  • Opt-in SQ8 compression for HNSW vector indexes, and several query engine improvements

One naming note: the release notes call the TTL-removal command SETPERSIST, but the command reference page is SPERSIST. This guide uses the documented name. Check COMMAND INFO on your server if in doubt.

Why per-member TTLs matter

Before 8.12, a Set’s members lived exactly as long as the key. Common workarounds:

  • A sorted set scored by expiry timestamp, plus a cron job running ZREMRANGEBYSCORE
  • One key per member with its own EXPIRE, losing cheap set operations
  • Application-side cleanup that sometimes didn’t run

With member TTLs, Redis removes expired members on its own.

SEXPIRE: set a TTL on members

From the SEXPIRE reference:

SEXPIRE key seconds [NX | XX | GT | LT] MEMBERS nummembers member [member ...]

Note the MEMBERS nummembers part: you give the count, then the names.

redis> SADD online:users alice bob carol
(integer) 3
redis> SEXPIRE online:users 60 MEMBERS 2 alice bob
1) (integer) 1
2) (integer) 1
redis> STTL online:users MEMBERS 3 alice bob carol
1) (integer) 60
2) (integer) 60
3) (integer) -1

The reply has one integer per member:

  • 1: TTL set or updated
  • 0: an NX/XX/GT/LT condition wasn’t met
  • -2: no such member or key
  • 2: the member was removed because the TTL was 0

STTL returns the remaining seconds, -1 for a member with no TTL and -2 for a missing member. SPEXPIRE and SPTTL do the same in milliseconds, and the *AT variants take Unix timestamps.

The condition flags

  • NX: only set a TTL if the member has none
  • XX: only if it already has one
  • GT / LT: only if the new TTL is greater / less than the current one. A member without a TTL counts as infinite.

Behaviour you must know

The docs spell out some rules that affect design:

  • SADD doesn’t reset a TTL. Re-adding an existing member leaves its expiration untouched. To refresh, call SEXPIRE again.
  • Only commands that remove a member clear its TTL, such as SREM. You can also make it persistent again with SPERSIST.
  • A TTL of 0 (or a past *AT timestamp) removes the member immediately. If that empties the set, Redis deletes the key.
  • Other set commands treat expired members as missing even before Redis reclaims them. SCARD is the exception: it can count expired members that haven’t been removed yet.

That last point matters. If you show an “online now” count, an exact figure may need SMEMBERS or SSCAN rather than SCARD.

Pattern 1: “Who’s online” with heartbeats

Each client pings every 30 seconds. Members disappear 90 seconds after their last heartbeat.

import redis

r = redis.Redis(decode_responses=True)
KEY = "online:users"

def heartbeat(user_id: str) -> None:
    pipe = r.pipeline()
    pipe.sadd(KEY, user_id)
    # SADD keeps an old TTL, so always refresh it explicitly
    pipe.execute_command("SEXPIRE", KEY, 90, "MEMBERS", 1, user_id)
    pipe.execute()

def is_online(user_id: str) -> bool:
    return bool(r.sismember(KEY, user_id))

The examples use execute_command, so they work with any redis-py version that can talk to a Redis 8.12 server. The command docs also list native client methods (for example sexpire in redis-py and sExpire in node-redis), so check your client version’s changelog before switching to them.

Pattern 2: Short-lived allow/deny lists

Temporarily block IPs after repeated failed logins, each with its own lifetime:

def block_ip(ip: str, seconds: int) -> None:
    r.sadd("blocked:ips", ip)
    # GT: never shorten an existing, longer block
    r.execute_command("SEXPIRE", "blocked:ips", seconds, "GT", "MEMBERS", 1, ip)

def is_blocked(ip: str) -> bool:
    return bool(r.sismember("blocked:ips", ip))

Note the subtle case. A member added without a TTL counts as infinite for GT, so if the IP was already a permanent member, GT won’t shorten it. That’s usually what you want for a deny list.

Pattern 3: One-time tokens

Store recently used nonces so replays are rejected, each kept for exactly five minutes:

SADD used:nonces n-8f2a
SEXPIRE used:nonces 300 NX MEMBERS 1 n-8f2a

NX makes sure a retried request doesn’t extend the window.

Protecting keys from eviction with BLESS

Under maxmemory pressure, Redis evicts keys according to your policy. Sometimes a few keys must never go, such as feature flags or a rate-limit config. 8.12 adds BLESS, a container command with SET, CLEAR, GET and SCAN subcommands:

redis> SET config:flags '{"newCheckout":true}'
OK
redis> BLESS SET config:flags NO-EVICT
(integer) 1

According to the BLESS SET reference:

  • The key must already exist, or you get an error.
  • NO-EVICT is currently the only flag.
  • A blessed key is skipped as an eviction candidate under every maxmemory-policy.
  • The flag persists across restarts and replicates, but it isn’t included in DUMP output, and RESTORE doesn’t set it.

Use it sparingly. Blessed keys still use memory, and the docs note that BLESS SET can itself fail with an out-of-memory error, while BLESS CLEAR always works.

Upgrading safely

  1. Upgrade a staging node first and confirm the version with INFO server.
  2. Check that your client library and any proxy pass through the new commands.
  3. If you replicate to older replicas, don’t use the new commands until every node runs 8.12.
  4. Note the compatibility table on each command page: Redis Software and Redis Cloud support is marked separately, so check before you rely on these commands in managed environments.

Takeaway

Redis 8.12 removes a long-standing workaround. Sets can now expire individual members with SEXPIRE and friends, which suits presence lists, temporary blocks and replay protection. Remember that SADD doesn’t refresh TTLs and SCARD may count expired members, and use BLESS SET ... NO-EVICT for the handful of keys that must survive memory pressure.