Proposal: Hash Field Expiration (HFE) #3432
PragmaTwice
started this conversation in
General
Replies: 1 comment 2 replies
-
|
Nice proposal.
For the HLEN, I'm wondering if it would be better to have an option like |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Motivation
Redis 7.4 introduced per-field expiration for hashes (
HEXPIRE,HPERSIST,HPTTL, etc.). This proposal describes how to implement HFE in Kvrocks on top of the existing hash encoding in RocksDB.Goals
Design
Metadata
We extend the existing hash metadata with four new fields (
mode,expsz,lower,upper):sizeexpsz == 0.mode0= legacy (compatible with old data),1= field expiration mode.expsz0.lower0.upper0.Migration rule:
mode=0(extra fields absent, metadata decoded as legacy).mode=1, expsz=0, lower=0, upper=0.Subkey Encoding
When
mode=1, each subkey value is prefixed with an 8-byte expiration timestamp (ms).0means the field is persistent.HLEN Algorithm
The first two branches are O(1). The last branch scans all subkeys, removes expired ones, and updates metadata.
HPERSIST / HEXPIRE Algorithms
lowerandupperare only tightened lazily — they may become stale. This is fine because they are hints for the fast path and get corrected duringSCAN_AND_REPAIR.HDELof a field with TTL should decrementexpszas well.Expired Field Cleanup
Passive repairing — foreground commands (e.g.
HLEN) checklower < now < upperand triggerSCAN_AND_REPAIRwhen needed.Compaction filter — drops expired subkeys by checking the 8-byte expire prefix, but does NOT update metadata. This avoids data races and keeps compaction fast. Metadata is corrected by the next passive repair.
Active repairing (optional, may not be in the first version) — a background thread with an expiration index in a separate CF:
Scans entries up to
nowand deletes corresponding fields. Trade-off:HEXPIRE/HSETEXneed an extra write to maintain this index.The encoding here is just a rough draft; we still need to consider slots and namespaces for this CF.
New Config Options
Example of HLEN fast-path & repair logic
To illustrate why actual slow scans are infrequent, consider a hash with two fields:
field1(expire=5) andfield2(expire=10).Initial metadata:
lower=5,upper=10,size=2,expsz=2.now=0or2:HLENdirectly returns2(Fast path:now < lower).now=6: TriggersSCAN_AND_REPAIR. It removesfield1, updates metadata tolower=10, upper=10, size=1, and returns1.now=7or9:HLENdirectly returns1(Fast path:now < lower).now=11: Directly deletes the key and returns0(Fast path:now > upperandexpsz == size).As shown, thanks to the lazy bounds (
lowerandupper),SCAN_AND_REPAIRis only triggered when absolutely necessary, minimizing full scans in most scenarios.HLEN spec
Beta Was this translation helpful? Give feedback.
All reactions