← Building blocks

Key → value at any scale

Storage & data modellingfundamental

The access pattern is "get by id" and "put by id" at very high volume.

Say it like this

Every access is get or put by a single key at very high volume, so I use a key-value store that partitions by key automatically and give up ad-hoc queries, which this path never needs.

Basic solution

Use a key-value store that partitions by key automatically.

DynamoDBRedisRiaketcd (small, consistent)

Get and put by key, partitioned by the store

The store hashes the key to a partition so the application never routes. The second query is served by a secondary index the store maintains asynchronously.

How it works

  1. The client sends get(key) or put(key, value) to the store's endpoint or through its client library. There is no query language, only the key.
  2. The store hashes the key to a partition and routes the request to the nodes that own that partition; the client never chooses a node and never knows how many there are.
  3. A put writes the value to the owning replicas (a quorum of them, or a leader) and acknowledges once enough have it; a get reads from one or more replicas and returns the value.
  4. Partitions split automatically as data or throughput grows: the store moves key ranges between nodes in the background, and the application does not notice.
  5. Anything beyond get and put by key (a filter, a sort, a count) is either a secondary index the store maintains for you as a separate copy, or a scan over every partition that you should not run on the hot path.

What you are buying

  • Horizontal scaling by key with no sharding logic in the application.
  • Predictable single-digit-millisecond latency because every operation touches one partition.

Worked example

URL shortener mappings on DynamoDB

  • 6B links over 5 years × 500 B ≈ 3 TB; 40 writes/s average, 4,000 reads/s average, 40K reads/s at peak.
  • Table url_mapping: partition key alias, attributes long_url, user_id, created_at, expires_at. A redirect is GetItem(alias): one partition, ~2 ms.
  • Capacity: DynamoDB partitions serve about 3,000 reads/s each, so 40K/s at peak needs ~14 partitions; at 10 GB per partition the 3 TB is spread over ~300 partitions already, so throughput is not the constraint, size is.
  • Cost sketch at on-demand pricing: 4,000 reads/s × 86,400 ≈ 350M reads/day; with a 95% cache hit rate in front, 17M reads/day hit the table, which is what you pay for.
  • The one query the model does not give you is "list my links": add a global secondary index on user_id with created_at as sort key, written asynchronously by the store, eventually consistent, which is fine for a dashboard.

Variants and when each wins

Managed, disk-backed (DynamoDB, Cloud Bigtable in KV mode)

  • A durable store that partitions itself and charges per request. It wins for application data by key with no cluster to run; item sizes are capped and each partition has a fixed throughput, so hot keys throttle.

In-memory (Redis, Memcached)

  • Everything lives in RAM, so a get is sub-millisecond. It wins when latency or data structures matter and the data set fits in memory across the cluster; Redis adds lists, sets, sorted sets and persistence options, Memcached is a plain cache.

Embedded (RocksDB, LevelDB)

  • A library linked into your process, with no network hop at all. It wins as the engine under another database or for one node's local state.

Consistent coordination store (etcd, ZooKeeper, Consul)

Pick DynamoDB-style for durable application data by key; Redis when latency or data structures matter and the set fits in memory; etcd only for coordination metadata.

Failure modes

Hot key throttling

  • A few requests get throttled while the table as a whole is far below its limit. It happens because one key's partition exceeds its per-partition throughput even though the other partitions are idle. Fix it by caching the key in front of the store, or by spreading it under suffixed copies and reading a random one.

The second query

  • A feature needs "all items where status = pending" and the only option is a full scan. It happens because a key-value store does not index attributes unless you ask it to. Fix it by adding a secondary index keyed on that attribute, or by publishing changes to a search or analytics store.

Large values

  • Writes fail or slow down as items grow (DynamoDB caps an item at 400 KB). It happens because blobs or ever-growing lists are being stored under one key. Fix it by moving the bytes to object storage and keeping a pointer, and by splitting growing lists across several keys.

Stale read after write

  • A user updates a value and immediately reads the old one back. It happens because the read was eventually consistent and was served by a replica that had not received the write. Fix it by requesting a strongly consistent read on the read-after-write path only; it costs about double.

Memory exhaustion (in-memory stores)

  • Redis starts evicting keys or refusing writes. It happens because the data outgrew RAM or keys have no TTL and never leave. Fix it by setting maxmemory with an eviction policy, adding TTLs, and sharding across more nodes.

Interviewer follow-ups

Answer out loud before you open each one.

How do you query by something other than the key?

Either the store maintains a secondary index for that attribute, which is a second copy kept eventually consistent, or you keep a second table keyed the other way and write both. For rich filters I would feed changes into a search or analytics store rather than fight the key-value model.

When would you not use a key-value store?

When the product needs joins, transactions across several entities, or ad-hoc reporting, and the data fits a relational database with replicas. Most products do, and a key-value store is the wrong default; it is the right tool for one specific hot path by key.

How do you handle a hot key such as a viral link?

Put a cache in front so the store sees only misses, and if the key is still hot, replicate it under suffixed keys and read a random copy. Say that per-partition throughput is the limit you are working around.

What consistency do you get, and when does it matter?

Eventually consistent reads by default, with an option for strongly consistent reads at higher cost. It matters on the read-after-write path for the same user; for everyone else a few hundred milliseconds of lag is invisible.

Connections

Built on
Hash table
Pairs with
Cache the hot reads · Partition the data · Spread a hot key · Accept a short delay for scale · Expire data automatically
Pulls against
Default to a relational database · Full-text and fuzzy search
Used in
URL Shortener (TinyURL / bit.ly) · Video Streaming Platform (YouTube / Netflix)

The same parts on other clouds

The diagrams above name AWS services as one concrete example. Every part has a close equivalent elsewhere. In an interview, name the role first, such as “a managed Redis cache”, then the product you would pick.

AWSUsed here asGoogle CloudMicrosoft AzureOpen source and others
Amazon EC2 Auto ScalingRedirect serviceCompute Engine managed instance groupsVirtual Machine Scale SetsKubernetes Deployment with a HorizontalPodAutoscaler, Nomad
Amazon DynamoDBStore endpointFirestore, BigtableCosmos DBScyllaDB, Apache Cassandra