RAM is paid per key, not per byte
Every live key consumes a fixed-size keydir entry (~44.5 B + key length) regardless of value size, so RAM scales with key count alone — not with how much data you store.
Reads were cheap because the keydir was in RAM. The bill for that line is paid in keys, not bytes — and most workloads don't realise which side of that asymmetry they're on until they OOM.
Scene 04
RAM is paid per key, not per byte
- Watch
- Try it
- Predict
- Capture
Default workload: 1M keys, 32 B keys, 4 KB values. Watch the RAM bar settle around 76 MB while the disk bar climbs to ~4 GB — same key count, two very different bars. Both fit under the 256 MB RAM budget line.
Highlighted lines are the ones running in the diagram right now.
struct KeydirEntry {file_id: u32 # which data file holds the valuevalue_sz: u32 # bytes to preadvalue_pos: u64 # offset within file_idtstamp: u32 # for tombstone / merge ordering# 4 + 4 + 8 + 4 = 20 B payload# + ~24.5 B Erlang term + HashMap bookkeeping# → 44.5 B static + len(key) per entry}
def estimateKeydirRam(keyCount, avgKeyLen):static_overhead = 44.5 # Riak capacity calculator figurebytes_per_entry = static_overhead + avgKeyLen# value_size DOES NOT appear — the keydir only stores# a pointer to the value, never the value itself.return keyCount * bytes_per_entry
def verdict(keyCount, avgKeyLen, ramBudget):needed = estimateKeydirRam(keyCount, avgKeyLen)if needed > ramBudget:return OOM # use an LSM insteadif needed > 0.75 * ramBudget:return TIGHT # one traffic spike from OOMreturn FITS
Where this sits in Build a Bitcask-style KV store
Scene 04 of 9, in the Limits act — RAM is paid per key; merge gives back what's dead.. Every live key consumes a fixed-size keydir entry (~44.5 B + key length). RAM scales with key count alone — fat values are free, tiny tags OOM.
Up next. RAM tracks live keys. Disk tracks every write you've ever made — including the overwritten ones. Without something to reclaim, disk grows forever.
All 9 scenes in Build a Bitcask-style KV store · Every curriculum