Choosing a data store

Choose from the access pattern, not the feature list — and why relational is the default that answers questions you have not thought of yet.

11 min read🏗️ System Design Fundamentals

"Which database?" is the decision with the longest half-life in a design. Everything else can be changed in an afternoon; the store holds the data, and moving it is a migration project.

So it is worth choosing from the access pattern rather than from a list of features.

Start from the questions, not the technology

Before naming anything, answer four things about the data:

  1. What are the reads? By primary key? By a range? By arbitrary combinations of fields? Full-text?
  2. What is the write pattern? Steady, bursty, append-only, heavy updates?
  3. What consistency does the business need — not what is nice, what would be wrong to get wrong? Money and stock levels are different from view counts.
  4. How big does it get, and how fast? The estimation lesson's arithmetic, applied here.

Answer those and the shortlist is usually one or two things. Start from "should we use Mongo?" and you have started at the answer.

The shortlist

StoreIt is good atIt is bad at
Relational (Postgres, MySQL)related data, ad-hoc queries, transactions, constraintshorizontal write scale past one primary
Document (Mongo)one aggregate read whole, flexible shapejoins, and consistency across documents
Key-value (Redis, DynamoDB)one known key, very fastanything that is not a key lookup
Wide-column (Cassandra)huge write throughput, time seriesanything you did not design the partition key for
Search (Elasticsearch)text, relevance, facetingbeing the system of record
Object store (S3)large blobs, cheap and durableanything you need to query
Graph (Neo4j)many-hop relationship querieseverything else

The default is boring and it is usually right

Start with a relational database. Not from conservatism — from the fact that it is the only one on that list that does not require you to know your queries in advance.

A relational schema answers questions you have not thought of yet. Every other store on that list is fast because it made an assumption: Cassandra is fast if you query by the partition key you chose a year ago; Redis is fast if you know the key. When the product changes and the question changes, the relational database runs a slower query and the others cannot answer at all.

And the scale argument is weaker than it sounds. A single modern Postgres handles tens of thousands of writes a second and terabytes of data. The honest question is not "will it scale" but "what is our actual number, and what fraction of that is it" — and for most applications being designed in an interview or a startup, the answer is a small fraction.

Polyglot, and its price

Real systems use more than one, and that is fine when each is doing what it is good at:

  • Postgres as the system of record.
  • Redis as a cache, holding nothing that is not derivable.
  • Elasticsearch for search, fed from Postgres.
  • S3 for files, with the metadata row in Postgres.

The pattern in each: one store owns the truth, and the others are derived. That keeps the failure modes simple — a derived store can be rebuilt; if two stores both own truth, you have a distributed consistency problem you have to solve forever.

The price is real and worth naming: every additional store is another thing to operate, back up, monitor, upgrade and be paged for, and another consistency lag to explain to somebody. "We added Elasticsearch" also means "search results can be a few seconds behind" and somebody will report that as a bug.

The specific choices that come up

Redis for cache, and for what else? Its data structures make it a reasonable queue, rate limiter and leaderboard. It is not a database: by default it is not durable, and treating it as a store means accepting that a restart can lose data.

When a document store genuinely wins. When the aggregate is read and written whole, has a shape that varies, and is not joined to anything. A product catalogue with per-category attributes is the honest example. An orders system with customers and line items is not — that is relational data in a document store, and you will write the joins in application code.

When Cassandra genuinely wins. Write volume beyond what one primary can take, with queries known in advance and modelled as partitions. Time-series and event logs are the fit. The cost is severe and worth stating: you design the tables from the queries, and a new query often means a new table and a backfill.

Search is not a system of record. Elasticsearch loses documents under some failure modes and its consistency model does not aim at durability. It indexes the truth; it does not hold it.

Under the hood: the storage engine underneath the label, and why it decides the read/write trade-off

"Relational" and "wide-column" describe a data model, but the actual read/write cost of a store is set one layer down, by its storage engine — and most engines fall into one of two families whose trade-off is the real reason Cassandra is fast at writes and Postgres is fast at ad-hoc reads.

A B-tree engine (Postgres, MySQL's InnoDB, most relational databases) keeps data on disk sorted, in place, in a tree of fixed-size pages. A write finds the correct page — often already true, in-memory — and modifies it directly. A read walks the tree from the root and lands on the one page holding the value, typically in a handful of page reads no matter how large the table is. The cost is on the write side: modifying a page in place, especially one not already cached in memory, can mean a random disk read followed by a random disk write — and if the change also has to propagate (a page split, an index update), several more. This is exactly why "tens of thousands of writes a second" is the honest ceiling this lesson names for a single Postgres primary: each write is doing real, randomly-located disk I/O, not just appending bytes.

An LSM-tree engine (Cassandra, RocksDB, and what many key-value and wide-column stores use underneath) never modifies data in place. A write goes to an in-memory buffer (a memtable) and an append-only log for durability — both sequential operations, which is why LSM-tree writes are so much cheaper: sequential disk I/O is far faster than random I/O, on spinning disks dramatically so and on SSDs still measurably so. When the memtable fills, it is flushed to disk as a new sorted, immutable file (an SSTable); a background process periodically compacts several SSTables into fewer, larger ones, merging entries and discarding overwritten or deleted values. The cost moves to the read side: a value might exist in the memtable, or in any of several SSTables on disk, so a read may have to check multiple locations — mitigated by bloom filters (a compact structure that can quickly say "definitely not in this file", avoiding most unnecessary disk checks) and by compaction keeping the number of files manageable, but a read in an LSM-tree engine is structurally more work than a B-tree's single tree-walk.

This is the real content behind "Cassandra means designing tables from queries." It is not an arbitrary API restriction — it follows from the write-optimised structure: because a read has to check multiple SSTables and cannot cheaply do an ad-hoc scan across them the way a B-tree can walk an index for any query matching its sort order, the engine only performs well when reads are funnelled through the exact partition key the data was written under. Change the query and you are asking the storage engine to do work its structure was not built for — which is why a new access pattern in Cassandra usually means a new table populated by a backfill, not a new query against the existing one.

Try it yourself

Predict which engine handles a workload better, from the workload alone

Two workloads: (1) a time-series metrics table, millions of writes a minute, almost always queried by "give me the last hour for this one metric key", rarely updated once written; (2) an accounts table, moderate write volume, queried constantly by many different, unpredictable combinations of fields (by email, by signup date range, by status, by a join to orders). Using only the B-tree/LSM-tree trade-off above, reason about which family fits which workload, and why forcing the fit the other way around would hurt.

Why the same "just use a database" instinct fails workload 1

Workload 1 is close to the textbook LSM-tree case: extremely high write volume, an append-heavy pattern (metrics are written once and rarely updated), and reads that go through one known access pattern (metric key, recent time range) — exactly the shape an engine optimized for sequential writes and a single well-chosen partition key handles well, and exactly where a B-tree engine's random-write cost per data point becomes the bottleneck at high enough volume. Workload 2 is the textbook B-tree case: moderate writes, but reads that need to filter and join on fields nobody could fully predict in advance — the flexibility a B-tree's general-purpose indexing gives you (index a few columns, and the query planner can still use them in combinations, or fall back to a slower scan for anything else) is precisely what an LSM-tree/wide-column store cannot offer, because reads outside the store's chosen partition key are exactly its weak point. Forcing workload 1 onto a B-tree store risks hitting a write-throughput ceiling as data volume grows; forcing workload 2 onto an LSM-tree/wide-column store means either designing a table per query pattern up front (this lesson's Cassandra cost) or accepting that most of the "unpredictable combination" queries simply cannot be served efficiently at all.

Misconceptions

  • "NoSQL is fast and SQL is slow." The label on the data model (relational, document, wide-column) is not what sets the read/write trade-off — the storage engine underneath is, and it is entirely possible for a relational database to use an LSM-tree engine or a "NoSQL" store to use a B-tree; the trade-off this lesson describes is about the engine, not the marketing category.
  • "A write-optimised store is strictly better for write-heavy workloads, so more writes should mean 'go LSM-tree'." Write volume is only half the decision — the other half is whether the reads can live inside a single, chosen partition key. A write-heavy workload with unpredictable read patterns is still poorly served by an LSM-tree/wide-column store, because the cost that engine avoids on writes it collects back on any read outside its optimised path.

Sources

  • Martin Kleppmann, Designing Data-Intensive Applications — chapter 3 (Storage and Retrieval) covers B-trees and LSM-trees, SSTables, memtables and compaction in detail.
  • Avinash Lakshman and Prashant Malik, "Cassandra: A Decentralized Structured Storage System" (2010) — the original Cassandra paper.
  • PostgreSQL documentation, "Database Physical Storage" — B-tree indexes and page-based storage.
  • Facebook Engineering, "RocksDB: A Persistent Key-Value Store" documentation — a widely used LSM-tree engine and its compaction strategies.
Progress is saved on this device and to your account when signed in.