Key Value Stores
Asked of: Software Engineer
Last updated

What's being tested
These designs test whether you can turn simple GET/PUT semantics into a reliable service under concurrency, node failures, increasing data volume, and latency requirements. The interviewer is probing your command of partitioning, replication, consistency, durability, and operational tradeoffs—not whether you can name a favorite database. LinkedIn cares because many backend systems depend on predictable low-latency reads, graceful degradation, and clear correctness guarantees when machines and networks fail.
Core knowledge
-
A key-value store exposes operations such as
GET,PUT,DELETE, and often conditional writes likeCompareAndSet. Define maximum value size, key format, availability target, throughput,p99latency, and whether missing keys, stale reads, or overwrites have explicit semantics. -
For a single process, a hash map provides expected lookup, insertion, and deletion, with space. Concurrent access requires a mutex, read-write lock, or sharded locks; never hold a global lock across slow disk I/O or callbacks.
-
Partitioning distributes keys across nodes. Simple
hash(key) mod Nis easy but remaps roughly most keys when changes; consistent hashing or rendezvous hashing limits movement, while virtual nodes improve balance when machines have unequal capacity. -
Replicate each partition across distinct failure domains using a replication factor , commonly three. A leader-based design routes writes through one replica; leaderless designs use quorum parameters , , and , typically requiring for read-write overlap, but this alone does not guarantee conflict resolution.
-
Consistency is a contract, not a binary feature. Strong linearizable reads and writes simplify clients but cost coordination; eventual consistency improves availability and latency. State whether reads may be stale, what ordering is guaranteed, and how concurrent writes resolve.
-
For quorum reads, read from replicas and write to replicas. If , at least one replica overlaps, but repair, hinted handoff, version vectors, timestamps, or deterministic last-write-wins rules are still needed to reconcile divergent values.
-
Durability requires more than acknowledging a write in memory. A write-ahead log (WAL), append-only log, or replicated commit must reach stable storage before acknowledgement if acknowledged data cannot be lost after process or machine failure; define the
fsyncpolicy explicitly. -
Storage engines trade write and read behavior. An in-memory hash map is fast but volatile; an LSM tree writes sequentially to a memtable and immutable SSTables, then uses compaction. Compaction consumes I/O and can cause write amplification, while Bloom filters reduce unnecessary SSTable reads.
-
A cache-aside layer can reduce backend load, but invalidation creates stale-data races. TTL metadata should use monotonic expiration handling, and expired entries should be removed lazily or by bounded background work; avoid allowing a cleanup thread to monopolize locks.
-
Rebalancing must preserve availability and avoid copying an entire dataset at once. Move ranges or virtual-node ownership incrementally, throttle transfer bandwidth, dual-read or dual-write during cutover when necessary, and verify checksums before deleting the old copy.
-
Failure handling should distinguish process crashes, disk failures, network partitions, slow nodes, and correlated zone outages. Use timeouts, bounded retries, exponential backoff with jitter, circuit breakers, hinted handoff, and idempotent request identifiers to prevent retry storms and duplicate effects.
-
Operational correctness needs observability: request rate, error rate, saturation,
p50/p99latency, replication lag, rejected writes, compaction debt, disk usage, hot partitions, and repair backlog. A health check should not report a node healthy merely because its process is alive.
Worked example: Design a Distributed Key-Value Store for a Small Company's Internal Use
In the first 30 seconds, a strong candidate would clarify expected keys and values, read/write ratio, data size, latency target, durability requirements, acceptable staleness, regions, and whether this is a general-purpose database or an internal configuration service. They might assume tens of millions of keys, three availability zones, replication factor three, and a requirement that acknowledged writes survive a single-node failure. The answer can be organized around the API and semantics, partitioning and request routing, replication and consistency, durable storage, and failure recovery/operations. For a small company, they should compare a centralized leader with replicas, a decentralized ring, and a hybrid design rather than prematurely building a globally distributed system. A sensible initial choice is a partitioned leader-per-shard architecture with consistent hashing, synchronous replication within the region, and asynchronous cross-region replication if disaster recovery is required. The explicit tradeoff is simplicity versus availability: a centralized or leader-based model is easier to operate and reason about, while decentralized quorum writes reduce dependence on one leader but make conflict repair and debugging harder. They should explain how membership changes trigger throttled range movement and how reads behave while a replica is unavailable. If they had more time, they would detail schema/value limits, rolling upgrades, backup restore testing, and load tests for hot keys and compaction.
A second angle: Design an in-memory key-value store using maps
The in-memory version shifts attention from cluster topology to API behavior, concurrency, and the path from volatile memory toward durability. Start with a hash map for expected operations, then discuss sharded locks or a concurrent map so unrelated keys do not block one another. TTLs introduce clock and cleanup concerns, while persistence introduces a WAL, snapshots, or both. The key framing question is whether the store is merely a process-local cache or must recover acknowledged writes after restart. If it must recover, the design needs explicit ordering between memory mutation, log append, flush, and acknowledgement.
Common pitfalls
Pitfall: Saying “use consistent hashing” without explaining replication, ownership, and rebalancing leaves the core availability problem unsolved. Describe virtual nodes, replica placement across zones, bounded data movement, and what happens while ownership changes.
Pitfall: Treating
$W+R>N$as a universal strong-consistency guarantee is analytically wrong. Quorums overlap, but stale replicas, concurrent writes, clock skew, and conflict resolution still require a defined protocol and read-repair or hinted handoff.
Pitfall: Listing
Kafka,Raft, LSM trees, and multi-region replication without prioritizing requirements sounds like technology name-dropping. State assumptions first, choose the simplest architecture that meets them, then identify the next bottleneck or failure mode you would investigate.
Connections
The interviewer may pivot to Raft leader election, gossip-based membership, consistent hashing, WAL recovery, LSM-tree compaction, or cache invalidation. Be prepared to connect consistency choices to CAP behavior, backpressure, observability, and idempotent API design.
Further reading
-
On Consistent Hashing: https://en.wikipedia.org/wiki/Consistent_hashing
-
On Partitioning Strategies: https://en.wikipedia.org/wiki/Hash_table#Partitioning
-
Introduction to Key-Value Stores: https://martinfowler.com/articles/rackspace-pg.html
-
Read about the CAP theorem: https://en.wikipedia.org/wiki/CAP_theorem
-
Overview of Operational Monitoring: https://www.datadoghq.com/blog/the-importance-of-operational-monitoring-and-observability/#monitoring-and-observability