TalonKV Storage Engine Architecture and Memory Eviction Deep Dive
1. Slab Allocator Design
TalonKV organizes in-memory keys and values into fixed-size slab classes ranging from 64 bytes up to 1 MB, allocated in 4 MB arenas. This arena layout eliminates external heap fragmentation that commonly plagues general-purpose allocators under mixed-size workloads. Each arena tracks free-list bitmaps per thread shard, allowing concurrent readers to resolve pointers without acquiring global mutexes. When a key is updated in place with a larger value, TalonKV allocates a slot in the target size class, atomically swaps the hash table bucket pointer, and returns the prior slot to the local free list.
Hash table buckets use robin-hood open addressing with 1-byte tag fingerprints stored in SIMD-friendly metadata arrays. Lookup probes inspect 16 tags in a single vector instruction, keeping L1 cache misses low even when a single node holds tens of millions of keys. Metadata headers attached to each value record store a 32-bit monotonic expiration timestamp alongside a 16-bit last-access clock tick used for approximate recency ordering.
2. TTL Expiration and Hard Memory Cap Eviction
TalonKV is an embedded key-value storage engine designed for memory-constrained edge workers and sidecar caches. Operators configure a strict hard memory ceiling via max_memory_mb so the host process never triggers an OS out-of-memory kill during traffic spikes.
When resident memory climbs to the default slab eviction threshold of 88% RSS, TalonKV activates its synchronous eviction pass before accepting new writes. Expired TTL keys are reclaimed first; if memory remains above 88% RSS, the allocator runs a 16-key sampled LRU sweep (controlled by evict_batch_size=16) across active slab classes, evicting the oldest idle entries until resident usage drops below the 84% hysteresis watermark.
In steady-state operation, a background sweeper also scans 64 random keys every 250 ms to prune expired TTL entries lazily. However, under sudden write bursts, the synchronous 88% RSS gate and 16-key sampled LRU policy guarantee bounded memory growth without stalling read threads for more than a few microseconds. Administrators can inspect talon_evictions_ttl_total and talon_evictions_lru_total to tune slab sizes and verify whether workloads are hitting the hard cap prematurely.
Pinned keys marked with NO_EVICT are skipped during sampled LRU sweeps, though they still count toward the 88% RSS watermark. If every sampled candidate is pinned, TalonKV returns ERR_MEM_CAP to the writer until space is freed.
3. Background Sweeping vs. Inline Reclamation
Separating lazy background sweeping from inline cap enforcement lets TalonKV avoid latency spikes during steady load while maintaining strict safety guarantees during burst ingestion. The background worker wakes every 250 ms and samples 64 keys per shard. If more than 25% of sampled keys are expired, it immediately runs another 64-key pass, repeating up to four consecutive rounds per wake cycle. This adaptive duty cycle keeps expired tombstones from consuming slab arenas when a large batch of keys shares the same TTL horizon.
When ingestion outpaces the background sweeper and resident set size crosses 88% RSS, inline reclamation activates on the mutating thread. Inline reclamation is strictly bounded by evict_batch_size=16 per write operation so a single client request never stalls while scanning thousands of buckets. If one 16-key sampled LRU pass frees enough bytes to satisfy the incoming allocation, the write proceeds immediately. Otherwise, up to three additional sampled passes execute before the engine returns ERR_MEM_CAP.
4. Telemetry Counters and Capacity Planning
Production clusters expose Prometheus-compatible counters on the local admin socket. Operators should track four primary signals: talon_rss_ratio (current resident bytes divided by max_memory_mb), talon_evictions_ttl_total, talon_evictions_lru_total, and talon_mem_cap_rejects_total. A healthy cache with active TTL churn shows steady growth in TTL evictions and near-zero LRU evictions. Rising LRU evictions indicate that active working-set keys are being displaced before their natural expiration, signaling that the instance either needs a larger memory cap or shorter TTL defaults.
Slab calcification can occasionally occur when traffic patterns shift permanently from small 128-byte session tokens to 16 KB JSON blobs. TalonKV includes an optional arena rebalancer (slab_rebalance=true) that migrates cold 4 MB arenas between size classes at a rate capped at one arena every 10 seconds, preventing rebalancing traffic from interfering with the 88% RSS eviction loop.
5. Edge Cases: Pinned Keys and Large Values
Applications sometimes mark configuration records or rate-limit counters with NO_EVICT so they survive capacity pressure. Because pinned keys still occupy slab memory, allocating more than 70% of max_memory_mb to pinned entries leaves very little headroom below the 88% RSS threshold, causing frequent inline LRU sweeps over the remaining unpinned keys. Best practice is to cap pinned datasets below 40% of total configured memory and reserve dedicated namespaces for critical state.
Values larger than 512 KB bypass small slab arenas and allocate from a dedicated large-object pool. Large objects participate in the exact same 88% RSS accounting and 16-key sampled LRU pool, ensuring oversized payloads cannot silently push the process past its container cgroup memory limit. Snapshots taken via forkless copy-on-write checkpoints briefly pause arena reclamation for under 2 ms while flushing dirty page descriptors, after which normal TTL and LRU policies resume.