Choosing Your Database — System Design

Choose a database from four things, not from a trend: the shape of the data, the query pattern (how the data is fetched), the need for ACID transactions…

Many teams pick a database the way people pick a phone: whatever everyone is talking about. But the question isn't "which database is the coolest", it's "what is the data like, and how do you fetch it". Things at home are also stored by how you use them. Important letters that refer to each other go in a filing cabinet with labelled folders. Money you take out dozens of times a day stays in your wallet. Photos are sorted by date in an album. In this picture, the filing cabinet is like a relational database (tables connected to each other). The wallet is like a key-value database (one value taken quickly by one name). The photo album is like a time-series database (data sorted by time).

So choosing a database means matching the shape of the data and how it is read and written with a database built for that pattern.

Four questions before you choose

For each feature, answer these four first. In the exercise, they become the properties of the workload object, a description of how a feature uses its data:

Five database families

This table shows five database families, example programs, what each is strong at, and the TicketTune feature that fits. A record means one item of data, for example one concert.

Family Examples Strong at TicketTune feature
Relational (SQL) PostgreSQL, MySQL JOINs, ACID transactions, free-form queries Orders & payments
Document MongoDB content that varies per record, read whole by id Concert details
Key-value Redis fetching one value by key, very fast Login sessions & queue
Graph Neo4j walking relationships several steps deep "Your friends are going"
Time-series TimescaleDB, InfluxDB, Prometheus many data points a second, read by time range Per-second server metrics

There are also search engines such as Elasticsearch, for text search that tolerates typos. Elasticsearch almost always holds a copy of data from another database, not the original.

One more family appears in the CAP Theorem and chat app lessons: wide-column, for example Cassandra and ScyllaDB. This family is strong for very many writes that are then read by a partition key (a key that groups the data). An example is chat history, which is read one conversation at a time. pickDatabase in the exercise does not use this family.

Document or relational?

A document database fits better when the structure varies per record and the queries almost always fetch by key or by a field inside the document, not complex JOINs. A field is one entry inside a document, for example the concert's date. Concert details fit a document database. Concert A has a line-up and a rundown (the order of the show), concert B has venue rules and a seat map, and the concert page always reads one whole document.

If the data is connected (users have orders, orders have seats) and needs transactions, stay relational. PostgreSQL also has JSONB columns for the free-form parts, so often you don't need a second database.

Document databases have transactions too: MongoDB supports multi-document ACID transactions (one transaction that changes several documents) since version 4.0 (4.2 for sharded clusters). So the needsAcidTransactions rule in the exercise is a simplification. Relational stays the first choice here because the data is connected, and JOINs and foreign keys (columns that point to a row in another table) are already there.

pickDatabase: rules in order

The exercise turns all of that into rules checked from the top. As soon as one rule matches, its result is returned right away:

function pickDatabase(workload) {
  if (workload.shape === 'timeseries' || (workload.queryPattern === 'range-scan' && workload.writesPerSec > 10000)) {
    return 'TimeSeries';
  }
  if (workload.shape === 'graph' || workload.queryPattern === 'traversal') {
    return 'Graph';
  }
  if (workload.needsAcidTransactions === true || workload.queryPattern === 'joins') {
    return 'SQL';
  }
  if (workload.shape === 'document') {
    return 'Document';
  }
  return 'KV';
}

console.log(pickDatabase({ shape: 'relational', writesPerSec: 50, needsAcidTransactions: true, queryPattern: 'joins' })); // SQL

This table runs five TicketTune features through pickDatabase. They are exactly the five console.log lines in the exercise:

Feature shape writesPerSec ACID queryPattern Matching rule Result
Orders & payments relational 50 true joins 3 SQL
"Your friends are going" graph 10 false traversal 2 Graph
Per-second server metrics timeseries 50,000 false range-scan 1 TimeSeries
Concert details document 200 false lookup 4 Document
Login sessions & queue keyvalue 1,000 false lookup default KV

Rule order: the first match wins

Before you go on, take a guess: does pickDatabase({ shape: 'keyvalue', writesPerSec: 50000, needsAcidTransactions: false, queryPattern: 'range-scan' }) return 'KV', 'TimeSeries' or 'SQL'?

The shape is indeed keyvalue, but a range-scan with 50,000 writes a second gets in through rule 1's second way. The result is 'TimeSeries'. The exercise also checks two more cases:

Workload Why Result
keyvalue, 500 writes/s, range-scan 500 is not more than 10,000, so rule 1 does not match; no other rule matches KV
graph, 5 writes/s, ACID, joins rule 2 (graph) and rule 3 (ACID, joins) both match; rule 2 is checked first Graph

So the order of the rules is part of the answer. Swap rules 2 and 3, and the second case turns into SQL.

Polyglot persistence: one source of truth

Real systems rarely use just one database. Using several, each for its own need, is called polyglot persistence. Here is a common set, and the one TicketTune uses:

What matters: there is only one source of truth. The other databases hold copies that can be thrown away and rebuilt.

📌 Boring databases are good

⚠️ Picks that miss, in the exercise and in real life

TL;DR: Choose a database from four things, not from a trend: the shape of the data, the query pattern (how the data is fetched), the need for ACID transactions (several changes happen together or not at all) and the writes per second. Relational (PostgreSQL) for connected data, with JOINs (combining tables) and transactions. Document (MongoDB) when the content varies per record (one item of data, for example one concert) and is always read by key. Key-value (Redis) for fetching one value very fast. Graph (Neo4j) for walking relationships. Time-series for time-ordered data written in huge volume. The exercise's pickDatabase function checks the rules from the top, and the first match wins. The rules, in order: time-series (or a range-scan, reading a time range, above 10,000 writes a second), graph (or traversal, walking relationships), SQL (ACID or joins), document, then the default 'KV'. Real systems use several databases (polyglot persistence) with one source of truth, the place where the original data lives: PostgreSQL for orders and users. Redis is used for cache and sessions, Elasticsearch for search.

⚡ Start from the shape of the data

Meaning: Choose a database from four things, not from a trend: the shape of the data, the query pattern (how the data is fetched), the need for ACID transactions (several changes happen together or not at all) and the writes per second. Relational for connected data that needs JOINs (combining tables), key-value for fetching one value by key very fast, graph for walking relationships, time-series for data ordered by time.

⚡ Document or relational?

Guess first: a document database stores each item of data as one document, and documents may have different content. Concert A has a line-up and a rundown, concert B has venue rules and a seat map. The page always reads one whole document. Document or relational?

Document. The structure varies per record (per concert), and the queries almost always go by key, not complex JOINs.

Meaning: If the data is connected (users have orders, orders have seats) and needs transactions, stay relational.

⚡ The first matching rule wins

Write:

if (workload.shape === 'graph' || workload.queryPattern === 'traversal') {
  return 'Graph';
}
if (workload.needsAcidTransactions === true || workload.queryPattern === 'joins') {
  return 'SQL';
}

Meaning: These are two rules from the exercise's pickDatabase function, which picks a database for a workload (a description of how a feature uses its data). || means "or". The rules are checked from the top, and each return leaves the function at once. A graph workload that also needs ACID and joins matches both of these rules, but the result is 'Graph' because the graph rule is checked first.

⚡ One source of truth

Meaning: Real systems use several databases (polyglot persistence): PostgreSQL for orders and users, Redis for cache and sessions, Elasticsearch for search. Only one is the source of truth, the place where the original data lives: here, PostgreSQL. The rest are copies that can be thrown away and rebuilt.

What you will learn