Sub topic 8
Choose storage that fits the access pattern
This chapter assumes you have already listed the requirements, estimated capacity, and sketched an API and data model. You should also know the basics of relational and key-value databases. Here you pick the storage that fits how the data is read and written.
Start from the access patterns
Pick storage by how the data is used, not by which database is popular. Your API sketch already lists the operations. For each one, write down:
- The lookup key: by item ID, by owner, by time range, or by free text.
- Whether it is a read or a write, and how often.
- The consistency it needs: must a reader see the latest write?
- Whether it touches one record or joins several.
Consider this API sketch:
POST /v1/items
Content-Type: application/json
{"owner_id": "user:42", "body": "hello"}
GET /v1/items/{item_id}
GET /v1/users/{owner_id}/items?limit=20&cursor=abc123It shows two read patterns. One is a point lookup by item_id. The other is a range scan of one owner's items in order. Neither needs a join across tables, and both are keyed on a single value. That favors a store partitioned by key.
Compare the common options
| Store type | Fits best | Weak at | Example |
|---|---|---|---|
| Relational (SQL) | Joins, transactions, flexible queries | Horizontal write scaling | PostgreSQL, MySQL |
| Key-value or wide-column | Point lookups and range scans by key at high volume | Ad hoc queries, joins | DynamoDB, Cassandra |
| Document | Self-contained records with varying fields | Cross-document transactions | MongoDB |
| Object storage | Large blobs such as images and video | Low-latency small reads and updates | Amazon S3 |
| Search index | Full-text and faceted queries | Being the source of truth | Elasticsearch |
| In-memory cache | Hot data with very low latency | Durability and capacity | Redis, Memcached |
Use your estimates to narrow the choice
Suppose your estimate gave about 579 average write QPS, about 11,574 average read QPS, and about 9.1 TB of new data per year. A single well-tuned relational database can handle those request rates. Storage is the larger concern. At 9.1 TB per year, one machine fills up within a few years, so you will need partitioning at some point.
The same numbers might point elsewhere. If writes were 50,000 QPS, a key-value store built to partition by key would be the safer start. Always connect the choice back to a number you estimated.
Common mistakes
- Choosing by trend. "We need NoSQL for scale" is not a reason. Name the access pattern that a relational database cannot serve.
- Using one store for everything. Real designs often combine stores. For example, you might keep the source of truth in a database, images in object storage, and hot reads in a cache.
- Ignoring consistency. Many distributed stores replicate asynchronously, so a read right after a write can return old data. Decide whether your requirements allow that.
- Forgetting the partition key. A key such as
owner_idthat sends one very active user's traffic to a single shard creates a hot spot. Check the key against your read and write patterns.
Say your reasoning aloud
A short justification works well: "Reads are point lookups and per-owner range scans, with no joins. Volume is about 12,000 read QPS and 9 TB per year. I would use a key-value store partitioned by owner_id, with a cache in front for hot items." That covers the pattern, the numbers, the choice, and the risk to check.
Exercise
A service stores user profile photos and also needs to list, for each user, their 20 most recent photo records with captions. Which stores would you choose, and why?
Show answer
Use object storage for the photo files, because they are large blobs that need cheap, durable storage. Use a key-value or relational database for the photo metadata (owner, caption, timestamp, object URL), keyed by owner and ordered by time, so the "20 most recent" query is a single range scan. The database holds a pointer to the blob rather than the blob itself.