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:
- The shape of the data (
shape): connected tables (relational), documents whose content varies (document), a network of relationships (graph), data points ordered by time (timeseries), or key and value pairs (keyvalue). - The query pattern (
queryPattern): combining tables (joins), fetching one item by key (lookup), walking relationships several steps deep (traversal), reading one time range (range-scan), or summarising lots of data (aggregate). - Does it need ACID transactions? (
needsAcidTransactions): several changes must happen together or not at all. For example: save the order and take the seat out of stock. - How many writes a second (
writesPerSec): 50 a second and 50,000 a second need different machinery.
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
- Rule 1 has two ways in: the shape is
timeseries, or a range-scan with more than 10,000 writes a second. The brackets around&&make those two conditions one package. ||means "or": one true is enough.&&means "and": both have to be true.- Each
returnleaves the function at once, so the rules after it are never checked. return 'KV'at the very bottom is the default (the fallback result), used when no rule matches.- The returned text must be exact:
'TimeSeries'with a capital T and S, not'timeseries'.
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:
- PostgreSQL = the source of truth (the place where the original data lives): users, orders, payments, seat stock. Everything that has to be right lives here, with ACID transactions.
- Redis = cache and sessions: login sessions, ticket-war queue tokens, data read thousands of times a second. If it's lost, its content can be rebuilt.
- Elasticsearch = concert search with fuzzy matching (matching that tolerates typos): "dewa sembilan belas" still finds "Dewa 19". Its content is copied from PostgreSQL through events (messages saying that some data changed) or CDC (change data capture, reading changes from the database's log).
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
- PostgreSQL plus Redis is enough for most apps. Every new database means new backups, monitoring (watching that it works), upgrades and skills on the team.
- Redis is not where the main data lives. Its default settings only save a snapshot (a copy of all its data) to disk now and then. The append-only file (AOF, a file that records every write) is off. A sudden restart loses everything written since the last snapshot.
- Elasticsearch is not a source of truth. A new document can only be found by a search after a refresh, which happens every 1 second by default. Elasticsearch also has no transactions across documents.
- The CAP Theorem lesson covers why a distributed system has to choose between staying consistent and staying available when the network splits. How SQL and NoSQL (databases that are not relational) differ in general is in the SQL vs NoSQL lesson (Database track).
⚠️ Picks that miss, in the exercise and in real life
- Choosing by trend. Picking "NoSQL is faster" for data full of relationships ends with duplicated data everywhere and JOINs written in app code.
- Getting the order wrong in the exercise. The graph rule has to be checked before the SQL rule, and the time-series rule first of all. Swap them, and a graph that needs ACID becomes
SQL. - The wrong threshold. The condition is
writesPerSec > 10000, which means more than 10,000, and it only counts together withrange-scan. A range-scan with 500 writes a second must end at the default,KV. - Forgetting the default. Without
return 'KV'at the end, the function returnsundefined(no value).
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.