An ordered key-value store is a type of key-value database that maintains keys in a specific sorted order, typically lexicographical (byte-wise) or according to a user-defined comparator. This ordering property distinguishes it from hash-based key-value stores, which distribute keys randomly across partitions using a hash function and do not natively support efficient range queries or ordered iteration.
Core Characteristics
- Sorted Keys: Keys are stored in a data structure that preserves sort order (e.g., B-trees, B+ trees, or Log-Structured Merge-trees (LSM-trees)).
- Range Queries: Supports efficient retrieval of all key-value pairs within a specified key range (e.g.,
SELECT * WHERE key BETWEEN 'a' AND 'z'). - Ordered Iteration: Provides cursors or iterators to traverse keys in ascending or descending order.
- Predecessor/Successor Lookups: Allows finding the immediate previous or next key relative to a given key.
Common Data Structures
- B-trees / B+ trees: Traditional structure used in embedded databases (e.g., LMDB, Berkeley DB) and relational storage engines. Offers logarithmic time complexity for reads and writes with good cache locality for range scans.
- LSM-trees (Log-Structured Merge-trees): Optimized for high write throughput on solid-state drives (SSDs). Used in systems like RocksDB, LevelDB, Apache Cassandra (with specific compaction strategies), and TiKV. Writes are appended to a memory buffer (MemTable) and periodically flushed to sorted immutable files (SSTables) on disk, merged via compaction.
Prominent Implementations
- RocksDB / LevelDB: Embedded LSM-tree libraries widely used as storage engines for higher-level databases (e.g., TiKV, YugabyteDB, CockroachDB).
- LMDB (Lightning Memory-Mapped Database): Embedded B+ tree store utilizing memory-mapped files.
- FoundationDB: Distributed ordered key-value store providing ACID transactions across a cluster.
- etcd / Consul: Distributed key-value stores using Raft consensus, backed by BoltDB (B+ tree) or similar engines, used for service discovery and configuration.
- TiKV: Distributed transactional key-value store based on RocksDB and Raft.
- BadgerDB: Embedded LSM-tree store written in Go.
Comparison with Hash-Based Key-Value Stores
| Feature | Ordered Key-Value Store | Hash-Based Key-Value Store |
|---|---|---|
| Primary Access | Key lookup, Range scans, Ordered iteration | Key lookup (Point queries) |
| Typical Structure | B-tree, B+ tree, LSM-tree | Hash table, Hash map |
| Range Query Support | Native, efficient (O(log n + k)) | Inefficient (requires full scan or secondary indexing) |
| Write Performance | Varies (B-tree: random I/O; LSM: sequential I/O) | Generally high (O(1) average) |
| Use Cases | Databases, Indexes, Time-series, Leaders boards | Caching, Session stores, Lookup tables |
Typical Use Cases
- Database Storage Engines: Serving as the low-level storage layer for SQL and NoSQL databases (e.g., MySQL RocksDB engine, CockroachDB).
- Secondary Indexing: Maintaining pointers from index keys to primary keys.
- Time-Series Data: Storing metrics with timestamps as keys for efficient time-range scans.
- Distributed Coordination: Implementing distributed locks, leader election, and configuration management (e.g., etcd, ZooKeeper/ZooKeeper's ZNodes).
- Event Sourcing / Log Storage: Storing events sequentially by ID or timestamp.