Key-Value Databases: Redis, DynamoDB and When to Use Them

Key-value databases explained: the Dynamo idea, get/put/delete by key, why lookups are fast and scaling is easy, key design, and real uses at Dropbox, Snap and Disney+.

Beginner 4 min read · Updated

A key-value database is the simplest NoSQL store: it works like a giant hash map. You put a value under a key, get it back by key, and delete it by key. That's the whole interface. Many features need nothing more (sessions, caches, shopping carts, user preferences), and in return a key-value store gives very fast lookups and almost effortless horizontal scaling.

Why key-value databases exist

Amazon's engineers spelled out the pattern in the original Dynamo paper: many services on Amazon's platform (best-seller lists, shopping carts, customer preferences, session management, sales rank, the product catalog) only ever access their data by primary key. Running them on relational databases used those databases inefficiently and limited scale and availability. Dynamo offered a simple primary-key-only interface built for exactly those needs, and it inspired DynamoDB, Cassandra and Riak. See Dynamo-style key-value stores.

How key-value databases work

Each record has two parts, a unique key and a value, and the database offers three operations: get(key), put(key, value) and delete(key). No schemas, no relationships, no joins.

ownerget / put / deletekey = cart:u456hash(key) → partitionServer 1keys in hash range 0–33%Server 2keys in hash range 33–66%Server 3keys in hash range 66–100%
A key-value store hashes each key to the one partition that owns it. Every operation touches a single partition, so adding servers adds capacity linearly.
  • Primary-key lookups are fast. The database hashes the key to find its exact location: no table scans, no index traversals. In-memory stores answer in microseconds.
  • Horizontal scaling is straightforward. Partition keys by range or by hash and add servers for capacity; each server owns a subset of keys. Because no query ever spans partitions, the partitioning logic stays simple. See consistent hashing.
  • The API matches programming-language dictionaries. Developers already know get and put: no SQL to learn, no ORM, and the application's model lines up with the database.
import json
import boto3
import redis

# In-memory key-value: a session store with a 30-minute expiry
r = redis.Redis(host="localhost", port=6379, decode_responses=True)
r.set("session:abc123", json.dumps({"user_id": 456, "role": "member"}), ex=1800)
session = json.loads(r.get("session:abc123") or "null")
r.delete("session:abc123")                                  # logout

# Durable key-value: a DynamoDB table "file_metadata" keyed by "file_id"
table = boto3.resource("dynamodb", region_name="us-east-1").Table("file_metadata")
table.put_item(Item={"file_id": "f_789", "owner": "u456", "size": 52480, "path": "/docs/plan.pdf"})
item = table.get_item(Key={"file_id": "f_789"}).get("Item")
table.delete_item(Key={"file_id": "f_789"})

Designing keys

With lookup by key as the only query, the key is the design. Namespacing keys keeps different kinds of data apart and makes them predictable to build:

NeedKeyValue
Session for a logged-in usersession:{session_id}User ID, roles, expiry
Shopping cartcart:{user_id}List of SKUs and quantities
Rate-limit counterrate:{ip}:{minute}Request count, with a TTL
Cached web pagepage:{url}Rendered HTML
Feature flagflag:{name}On/off plus rollout percentage
Idempotency recordidem:{request_id}The stored response

Anything beyond lookup by key ("all carts containing SKU 9", "sessions older than a day") needs a second structure you maintain yourself, or a different kind of database. Some stores relax this: DynamoDB adds a sort key and secondary indexes, and Redis offers sorted sets and other data structures. See cache key design.

When to use a key-value database

  • High-performance reads and writes: reading and writing by key is about as fast as storage gets, so applications that need high-speed operations choose key-value stores.
  • Simple data structures: customer details, web pages keyed by URL, shopping-cart contents, product categories and e-commerce product details, where each entity is accessed independently with no relationships.
  • Basic query requirements: no complex queries and no updates that must span several records together; plain storage and retrieval.

Avoid them when you need ad-hoc queries, joins, or transactions across many records; use a relational database or a document database instead.

Thanks to their speed and simplicity, key-value stores rank high in popularity. The DB-Engines ranking of key-value stores in March 2023 (popularity score, not performance):

Redis172.45Amazon DynamoDB80.77Azure Cosmos DB36.1Memcached22.61Hazelcast8.63etcd8.6Aerospike6.54Ehcache6.03Google Cloud Bigtable5.44Apache Ignite5.29
DB-Engines key-value ranking, March 2023 (popularity scores). Redis and DynamoDB lead the category by a wide margin.

Further down the same list: Riak KV, ArangoDB, RocksDB, OrientDB, GemFire, Oracle NoSQL, LevelDB, Infinispan, Oracle Berkeley DB, InterSystems IRIS, LMDB, InterSystems Caché and Amazon SimpleDB.

RedisDynamoDB
Where data livesIn memory (optional disk persistence)Durable, replicated storage across availability zones
LatencyMicroseconds in-process; typically sub-millisecond over the networkSingle-digit milliseconds
ExtrasLists, sets, sorted sets, streams, TTLs, pub/sub. See RedisSort keys, secondary indexes, streams, global tables
Typical useCaching, sessions, counters, leaderboards, rate limitsPrimary store for key-based data at any scale, serverless apps

Real-world examples

  • Dropbox keeps file metadata in DynamoDB through its Alki system: each file's metadata maps to a unique file ID, relationships between files don't matter, and simple key lookups give the speed it needs. It saved millions in infrastructure costs compared with the previous on-premises storage.
  • Snapchat stores Stories in DynamoDB: a story ID maps to its content metadata, stories don't relate to each other, and their short-lived nature suits key-value simplicity. Snap handles over 10 million queries per second and cut median latency by 20%.
  • Disney+ uses DynamoDB for content metadata and user interactions: a video ID maps to its title, description and thumbnail URL, and each video loads independently. It ingests billions of customer actions a day for watchlists and personalized recommendations.

Where it’s used

Sessions and login state

session:{id} with a TTL in Redis: read on every request, so speed matters and relationships don't.

Shopping carts

Amazon's original Dynamo use case: always writable, keyed by user, merged on conflict.

Caching and rate limiting

Rendered pages, API responses and per-client counters with expiry. See caching and rate limiting.

Metadata at huge scale

File, story and video metadata keyed by ID (Dropbox, Snapchat, Disney+).

In an interview

  • Choose a key-value store when every access is by a known key; say what the key is.
  • Mention how it scales: hash partitioning (often consistent hashing) plus replication.
  • Say what you lose: no ad-hoc queries or joins, so any other access path needs its own index or table.
  • Distinguish an in-memory cache (Redis) from a durable primary store (DynamoDB).

Further reading