Keydb Eng !!hot!!
KeyDB — Overview and Practical Guide
KeyDB is a high-performance, open-source in-memory database that is protocol-compatible with Redis but offers additional features, multi-threading, and performance optimizations aimed at lower-latency and higher-throughput use cases.
Conclusion
KeyDB is a masterclass in re-engineering a legacy codebase for modern hardware without breaking the external contract. By trading single-thread simplicity for sharded parallelism, optimistic locking, and fork-free persistence, it achieves throughput that Redis cannot match on multi-core systems.
The engineering decisions—from thread-affinity sockets to checkpointing without fork()—reveal a deep understanding of OS internals, memory allocators, and concurrent data structures. While not a panacea, KeyDB proves that the "single-threaded for simplicity" argument has an expiration date. For workloads that can embrace its sharded model, KeyDB unlocks the latent power of today’s 64-core servers. keydb eng
Key Takeaway: KeyDB does not make Redis obsolete; it fills the gap where Redis’s architectural constraints hit hardware limits. For engineers building at scale, understanding KeyDB’s internals provides a blueprint for how to threadify a stateful server—one shard at a time.
4.3. Transactions and Sub-commands
KeyDB maintains support for standard Redis transactions (MULTI/EXEC) but has evolved its sub-command structure for better performance and memory efficiency, KeyDB — Overview and Practical Guide KeyDB is
SHM (Shared Memory) Mode
KeyDB introduces a revolutionary mode: Shared Memory, which allows multiple KeyDB processes (or even different programs) to access the same dataset concurrently.
- Zero-Copy Replication: Instead of
fork()(which causes copy-on-write spikes on large datasets), replicas in SHM mode map the same memory region. A write by the primary is instantly visible to replicas (with appropriate memory barriers). - Persistence without Latency Spikes: Traditional Redis
bgsaveforks the process, doubling memory usage temporarily (even with COW). KeyDB can create a snapshot by simply locking a shard and copying its data into a separate SHM region—no fork, no massive memory spike.
6. Performance Characteristics: What the Benchmarks Show
Published benchmarks (KeyDB team, Snap Inc.) on a 40-core machine with memtier_benchmark: SHM (Shared Memory) Mode KeyDB introduces a revolutionary
| Operation | Redis 6.0 (single-thread) | KeyDB (16 threads) | Improvement | |-----------|----------------------------|--------------------|--------------| | SET (QPS) | 450k | 4.2M | 9.3x | | GET (QPS) | 520k | 6.1M | 11.7x | | 50/50 R/W | 480k | 5.8M | 12x | | 99th %ile latency | 1.2ms | 0.6ms | 2x |
Crucially, scaling is near-linear up to ~24 cores, then allocator contention and cache coherence traffic cause diminishing returns.
2.2. Memory Management
KeyDB optimizes memory usage through specific structures:
- TLSF Allocator: KeyDB utilizes the TLSF (Two-Level Segregated Fit) memory allocator, which is generally faster and reduces fragmentation compared to the default allocators used by standard Redis (jemalloc/glibc).
- Machine Capabilities: KeyDB is designed to handle datasets that exceed the capacity of a single machine via its Active Replication and Flash storage support, effectively moving beyond the limitations of RAM-only storage.