When not to use SQL
The last lesson in the track, and the one most likely to come up in a design round rather than a coding one.
The honest starting position: a relational database is the right default, and the interesting question is not “SQL or NoSQL” but “what specific property do I need that this does not give me”. An answer that names the property is a good answer. An answer that says “it does not scale” is the weakest version, because a single Postgres instance on modern hardware handles far more than most systems ever ask of it.
What you would be giving up
Worth being explicit, because these are the things that get rebuilt badly in application code afterwards:
- Joins. Combining data by key, computed where the data lives.
- Transactions across rows and tables. The all-or-nothing guarantee from transactions and isolation.
- Declared constraints. Foreign keys, uniqueness and checks enforced for every writer, from keys and constraints.
- Ad-hoc query. Asking a question nobody designed for, without a migration.
- A mature planner. Thirty years of work deciding how to execute your query, and a way to inspect it.
Give up the last one and every new question becomes an engineering task.
The cases where something else genuinely wins
A write rate one machine cannot take. Not “a lot of writes” — a rate that exceeds what a single primary can commit, after batching and after the one-statement-instead-of-read-modify-write fixes. Metrics ingestion, clickstream, device telemetry. Cassandra-style stores take linear write scaling in exchange for joins and transactions, which is a real trade and not a free upgrade.
Documents read and written whole. If the access pattern is always “fetch this entire object by id, mutate it, write it back”, and nothing queries across objects, a document store fits the shape and a relational schema is paying for flexibility nobody uses. Be honest about the “nothing queries across objects” half — it is the part that stops being true.
Key-value at cache latency. Session data, rate-limit counters, feature flags. Sub-millisecond, no durability requirement, one access pattern. Redis is not a database replacement here, it is a different tier.
Full-text search. Postgres has tsvector and it is genuinely good to a
point. Relevance ranking, fuzzy matching, faceting and synonyms across large
corpora are what a search index is for. The usual shape is the relational store
as the source of truth with a search index derived from it.
Time series at volume. Append-only, timestamp-ordered, queried by range and aggregated, old data downsampled or dropped. A purpose-built store does compression and retention that a general table does not. Though note TimescaleDB is this as Postgres, which is often the cheaper answer.
Graph traversal of unbounded depth. “Shortest path”, “all nodes within six hops” is recursive-CTE territory in SQL and gets slow with depth. A graph database makes traversal the primitive. Two-hop joins are not this case.
The cases where it is usually the wrong call
“It does not scale.” Ask what the actual numbers are. Most systems quoting this are nowhere near a single instance’s ceiling, and read replicas plus connection pooling plus the index work from Part 3 is a lot of headroom.
“Our data is unstructured.” Postgres has jsonb with indexing. You can have
schema-on-read for the genuinely variable part and constraints on the rest, in
the same table.
“Schema migrations are painful.” They are, and they are also how you find out that a change breaks something. The alternative is not “no migrations”, it is the same migration performed implicitly by application code that has to handle every historical shape forever.
“We might need to scale later.” The polyglot-persistence cost arrives immediately — two stores to operate, no joins between them, no transaction spanning them, two consistency models — and the benefit arrives only if the growth does.
The shape most real systems end up with
Relational as the source of truth, with purpose-built stores derived from it:
- Postgres for the transactional core
- a search index, populated from it
- a cache in front of hot reads
- a warehouse or column store for analytics
What makes that work is one system owning each fact, with the others as derived copies that can be rebuilt. The failure mode is two stores both claiming to be authoritative for the same data, which is the denormalisation problem from the previous lesson at system scale: if you cannot name the single mechanism keeping a copy true, it will drift.
What to say in an interview
Name the property, then the cost:
“I would start with Postgres. If the write rate turns out to exceed one primary — and I would want the number — I would look at what can move to an append-only store, knowing that costs me joins and cross-row transactions on that data. For search I would expect a separate index derived from Postgres rather than trying to do relevance ranking in SQL.”
That is a stronger answer than any list of databases, because it shows the trade is understood rather than the names memorised.
Try it
Nothing to run here. The rest of the track is in the SQL playground, and SQL vs NoSQL is the longer treatment of this lesson’s subject.
For the system-design side — replication, sharding keys, which service owns which table — the HLD fundamentals pick up where this stops.
Interview questions
1. When would you choose something other than a relational database? When a specific property is missing: a write rate beyond one primary, documents always read and written whole, cache-latency key-value access, relevance-ranked search, time series at volume, or unbounded graph traversal. Naming the property is the answer; “it does not scale” is not.
2. What do you give up by moving off a relational store? Joins, transactions spanning rows and tables, declared constraints enforced for every writer, ad-hoc query without a migration, and a mature query planner you can inspect.
3. Your data is semi-structured. Does that rule out Postgres?
No. jsonb with indexing covers the variable part while the stable part keeps
its columns and constraints, in one table and one transaction.
4. What is the cost of polyglot persistence? It is paid immediately and in full: two systems to operate and monitor, no joins or transactions across them, two consistency models, and a synchronisation mechanism that can drift. The benefit only arrives if the growth that motivated it does.
5. How do you keep a derived store consistent with the source of truth? One system owns each fact and the others are rebuildable copies, fed by a single named mechanism — change data capture, an outbox, or a scheduled rebuild. Two stores both claiming authority for the same data is the failure mode.
6. Would you use Postgres for full-text search?
To a point — tsvector handles straightforward matching well. For relevance
ranking, fuzzy matching, faceting and synonyms over a large corpus, a dedicated
index derived from Postgres is the usual shape.
That is the track
Fifteen lessons, from the order a SELECT runs in to where the relational model
stops. The practice bank is where you write the queries — each
problem runs against a seeded Postgres in your browser and is graded on the rows
you return.
The drill for this lesson is
Query a comma separated column,
which is the relational boundary from the other side: a list crammed into a
text column, and every query that then has to take it apart.