Skip to content

RED-211767 - Add BLESS — protect keys from eviction - #15649

Merged
TalBarYakar merged 101 commits into
redis:unstablefrom
TalBarYakar:tal.ba/feat/blessed_keys
Sep 15, 2026
Merged

TalBarYakar merged 101 commits into
redis:unstablefrom
TalBarYakar:tal.ba/feat/blessed_keys

Conversation

@TalBarYakar

@TalBarYakar TalBarYakar commented Aug 16, 2026 •

Copy link
Copy Markdown
Collaborator

Add BLESS — protect keys from eviction

Adds BLESS, a keyspace command family for marking specific keys as NO-EVICT under maxmemory. A blessed key is never chosen as an eviction victim. The protection is stored per-key and survives restart (RDB/AOF), replication, and slot migration.

Commands

All since 8.12.0, container BLESS.

Command Description Reply Flags
BLESS SET <key> NO-EVICT Turn the flag on 1 if the key's flag set changed, else 0 WRITE, DENYOOM, FAST
BLESS CLEAR <key> NO-EVICT Turn the flag off 1 / 0 WRITE, FAST
BLESS GET <key> The key's active flag names ([] if none) array; errors if the key is missing FAST
BLESS SCAN <cursor> NO-EVICT [COUNT <count>] Cursored scan of the current DB's blessed keys carrying the flag [cursor, [key ...]] —
  • The flag argument is mandatory — there is no default.
  • BLESS CLEAR is intentionally not DENYOOM, so you can always unbless to recover under memory pressure.
  • BLESS SCAN mirrors SCAN semantics (COUNT is a hint, buckets are emitted whole). Without COUNT the whole index is returned in one call with cursor 0; with COUNT it paginates.
  • INFO Stats → blessed_keys — instance-wide count of blessed keys (replaces the earlier BLESS COUNT idea).

Eviction

Blessed keys are never chosen as eviction victims, on all maxmemory policies (LRU / LFU / random, and the volatile-* variants).

When eviction can't free enough because blessed keys are what's holding the memory, used memory is allowed to overshoot up to 1.25× maxmemory before writes are rejected with OOM — that factor is the effective ceiling for a heavily-blessed keyspace.

BLESS SET is DENYOOM (can't dig the hole deeper under pressure); BLESS CLEAR is not, so unblessing is always available to recover.

Design rationale

  • Not expected to be used in high volumes. The feature targets a small set of protected keys, which is why both the in-memory index and the command interface are kept simple rather than optimized for scale.
  • More bless types are expected later. NO-EVICT is the first flag; the command grammar (BLESS SET/CLEAR/GET <key> <FLAG>, BLESS SCAN <cursor> <FLAG>) and the shared per-key ATTR bitmask are designed to take additional flags without an interface change.
  • It's server-management state, not key data. Blessing logically resides in server management, not in the key's value. This drives two choices: key-spec flags carry only the physical RW (not the logical UPDATE/ACCESS), and blessing is excluded from DUMP/RESTORE.
  • It is replicated and persisted. Blessing rides to replicas, persists to RDB/AOF, and is supported across atomic slot migration (ASM) — it's durable server state, just not part of the key payload.
  • READONLY deliberately not set on BLESS GET/BLESS SCAN: that flag opts a command into client-side-caching tracking, and blessing state isn't part of the key's value, so there's nothing for CSC to invalidate — leaving it off keeps client-side caching from having to deal with these commands.
  • WRITE set on BLESS SET/BLESS CLEAR: blessing is a state change that must originate on the primary, so the commands are rejected on cluster replicas.

Note

High Risk
Changes core maxmemory eviction, RDB format version, and key lifecycle paths (db, cluster ASM, lazyfree); misbehavior could cause unexpected OOM, lost protection, or index drift under migration.

Overview
Introduces BLESS (SET/CLEAR/GET/SCAN with NO-EVICT) so existing keys can be marked non-evictable under maxmemory. The flag lives in kvBits on each kvobj, with a per-DB blessed_keys index for scan, INFO blessed_keys, and MEMORY STATS overhead.

Eviction skips blessed keys on all policies; when sampling finds only blessed keys, writes may succeed with used memory up to ~1.25× maxmemory before OOM. BLESS SET is DENYOOM; BLESS CLEAR is not.

Durability and movement: RDB bumps to v16 with opcode RDB_OPCODE_KEY_NOEVICT; AOF/ASM emit BLESS SET … NO-EVICT after values via blessRewrite. DUMP/RESTORE still omit the flag; RESTORE REPLACE keeps the destination’s blessing. Rename/move/copy/SWAPDB/ASM slot trim wire the index and kvSpec (replacing KeyMetaSpec) including no_evict on load paths.

Reviewed by Cursor Bugbot for commit e9e57b6. Bugbot is set up for automated code reviews on this repo. Configure here.

Comment thread src/db.c Resolved
Comment thread src/evict.c Resolved
@fcostaoliveira

Copy link
Copy Markdown
Collaborator

This change touches performance-sensitive code paths. Adding the action:run-benchmark label will trigger the CE Performance suite so we can see the impact before merge.

Comment thread src/keyattr.c Resolved
Comment thread src/evict.c Resolved
Comment thread src/db.c Resolved
- evict.c: re-check blessNoEvict when selecting from the LRU/LFU/TTL pool;
  the pool persists across calls, so a key pooled while unblessed then
  marked NO-EVICT could be evicted.
- keyattr.c: MOVE reused attrKeep, leaving a ghost in the source DB's
  blessed_keys (BLESS COUNT/LIST over-reported). Use attrRename to untrack
  the source; COPY keeps attrKeep.
- tests: stale-pool and MOVE regression tests.
Comment thread src/evict.c Resolved
@TalBarYakar TalBarYakar changed the title Tal.ba/feat/blessed keys RED-211767 - Add BLESS command to protect keys from eviction Aug 17, 2026
@TalBarYakar TalBarYakar added the action:run-benchmark Triggers the benchmark suite for this Pull Request label Aug 17, 2026
Comment thread src/db.c Resolved
Comment thread src/db.c Resolved
Replace KeyMetaSpec with kvSpec carrying the NO-EVICT flag.
Apply the flag and update the blessed index in dbAddInternal()
and dbAddRDBLoad(), before creation callbacks.

Remove raw bit assignments from RENAME/MOVE and post-add
blessing calls from COPY, RESTORE, and RDB loading.
Comment thread src/server.h Resolved
Comment thread src/server.h Resolved
Comment thread src/rdb.h Resolved
Comment thread src/rdb.c Resolved
Comment thread src/rdb.c Resolved
Comment thread src/db.c Resolved
Comment thread src/db.c Resolved
Comment thread src/db.c Resolved
Comment thread src/db.c Resolved
Comment thread src/cluster.c Resolved
Comment thread src/server.h Resolved
- Use spec consistently in DB insertion, RDB loading, RENAME,
  MOVE, COPY, and RESTORE, including declarations and documentation.
- Remove redundant NO-EVICT initialization in dbAddRDBLoad.
- Label the keymeta fields within kvSpec.
Rename kms/keymeta to spec in metadata declarations and implementations,
and keyMetaEmpty to specEmpty in dbAdd and dbAddByLink.

No functional changes.
Comment thread src/db.c Resolved
Comment thread src/keymeta.c Resolved
Rely on object initialization and deletion to clear the bit.
Format the keyMetaOnCopy signature on one line.
@TalBarYakar
TalBarYakar merged commit b9800e0 into redis:unstable Sep 15, 2026
20 checks passed
@github-project-automation github-project-automation Bot moved this from Todo to Done in Redis 8.12 Sep 15, 2026
@sundb sundb removed the action:run-benchmark Triggers the benchmark suite for this Pull Request label Sep 18, 2026
gabsow pushed a commit that referenced this pull request Oct 22, 2026
…5996)

Test-only change to `tests/unit/bless.tcl`. It covers the eviction
policies and paths that the BLESS suite added in #15649 didn't exercise
yet. No server code changes.

## What's added

| Area | Before | After |
| --- | --- | --- |
| Blessed key survives an eviction flood | 6 of 9 evicting policies |
All 9 (adds `volatile-lfu`, `volatile-lrm`, `allkeys-lrm`) |
| All-blessed bounded overshoot (tolerated within 1.25x `maxmemory`, OOM
past it, `BLESS CLEAR` relieves it) | `allkeys-random` only, plus a
separate `allkeys-lru` case | All 9 evicting policies; keys get a TTL
under `volatile-*` |
| Time-sliced eviction | Not covered | `lazyfree-lazy-eviction yes` with
`maxmemory-eviction-tenacity 0` |
| No overshoot when no blessed key is an eviction candidate | Not
covered | `noeviction`, and `volatile-lru` with blessed keys that have
no TTL: the plain `maxmemory` limit applies |

Most of the additions wrap existing tests in `foreach` loops rather than
copying them. Per review, the loops sit at column 0 so the wrapped tests
keep their original indentation (+51 / −9). A few existing test names
gain a `($policy)` or `(lazyfree-lazy-eviction ...)` suffix.

## Note on the "no overshoot" case

The eviction docs say the overshoot tolerance "applies uniformly across
every `maxmemory-policy` value". In the code, the tolerance only applies
when eviction sampled keys and found that all of them were blessed
(`blessed_blocked`). With `noeviction`, or under `volatile-*` when the
blessed keys have no TTL, nothing is sampled, so writes get a plain OOM.
The new test pins the current code behavior. If that's the intended
behavior, the docs wording could be narrowed.

## Testing

`./runtest --single unit/bless` on `unstable` @ 5e332ca (macOS arm64):
all 48 cases pass, plus the memory-leak checks.

Found while working on RED-213286 (BLESS OSS test coverage).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> **Low Risk**
> Changes are limited to unit tests in `bless.tcl` with no production
code paths modified.
> 
> **Overview**
> **Test-only** expansion of `tests/unit/bless.tcl` maxmemory/BLESS
eviction scenarios—no server changes.
> 
> The memory-pressure case now runs for both `lazyfree-lazy-eviction`
**no** and **yes** (with `maxmemory-eviction-tenacity 0` on the lazy
path), and several tests call **`config resetstat`** so `evicted_keys`
assertions are not polluted by earlier cases.
> 
> **All-blessed 1.25× overshoot** behavior (tolerate within factor, OOM
beyond, recover via `BLESS CLEAR`) is parameterized across all nine
evicting `maxmemory-policy` values, with TTLs on keys when using
`volatile-*`. New cases assert **no** overshoot tolerance when nothing
blessed is an eviction candidate (`noeviction`, and `volatile-lru` with
blessed keys that have no TTL).
> 
> TTL protection and pool-branch eviction floods add **`volatile-lfu`**,
**`volatile-lrm`**, and **`allkeys-lrm`**; existing single-policy tests
are mostly folded into `foreach` loops with policy names in test titles.
> 
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
c3654b2. Bugbot is set up for automated
code reviews on this repo. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-notes indication that this issue needs to be mentioned in the release notes state:needs-doc-pr requires a PR to redis-doc repository

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

7 participants