SQL vs NoSQL
Before this, it helps to know: What is System Design?.
Read a little, play a little. No scary maths, and no rush.
Two families, different promises
SQL (Postgres, MySQL): data in neat tables with relationships, plus ACID transactions — all-or-nothing guarantees. Transfer $100 from A to B and the money can never exist in both places or neither.
NoSQL is everything else, each giving something up for speed or scale: documents (MongoDB — JSON-ish blobs), key-value (Redis — one key, one value, blazing), wide-column (Cassandra — write floods), graph (Neo4j — things and their connections).
Match the tool to the shape
- Orders, payments, bookings → SQL. You need joins ("all orders with their items") and transactions. This is the default — most apps live here happily for years.
- Sessions, shopping carts, counters, leaderboards → key-value (Redis). Look up by one key in microseconds; nobody joins a session.
- Product catalogs, CMS pages, event logs → documents. Shape varies per item and you mostly fetch whole blobs.
- Likes, metrics, chat history at massive scale → wide-column. Optimized for write avalanches.
The beginner rule (say this in interviews)
SQL first, add a cache second, reach for a special database only when proven. New engineers love proposing five databases; seniors ask "what breaks if this is just Postgres plus Redis?" Nine times out of ten, nothing breaks — and one database is enormously easier to back up, secure, and reason about.
A worked example: an online store
- Users, orders, and payments live in Postgres. Placing an order must create the order, reserve stock, and record the payment together or not at all, which is exactly what a transaction guarantees.
- Shopping carts live in Redis, keyed by session, with a TTL so abandoned carts clean themselves up. Losing one is mildly annoying, not a financial error.
- The product catalog has items with very different attributes: a shirt has sizes, a laptop has RAM. Postgres can still hold it, with the common fields as normal columns and the varying ones in a JSON column. One database covers both shapes, and the team only adds a document store if that genuinely stops working.
What "NoSQL scales better" really means
Many NoSQL databases are built to spread data across many machines automatically, partitioned by key, so adding machines adds capacity. The price is what you give up: joins across partitions, and transactions that touch many rows at once. Relational databases scale a long way too, through indexes, read replicas, caching, and larger machines. Splitting one across machines (sharding) is possible but takes more deliberate work. So the honest version is: NoSQL makes certain kinds of horizontal growth easier by removing features you may need.
Rigid or flexible schema?
A SQL database enforces the schema when data is written: a column defined as a number rejects text. Document stores are often called "schemaless," but the schema has not disappeared; it has moved into your application code, which must cope with every shape ever written. Changing the data's shape still needs a migration plan either way.
Remember this
- Relations + money → SQL. Single-key speed → key-value. Varying shapes → documents. Write floods → wide-column.
- "Postgres first" is a defensible answer to almost any database question.
Check your understanding
5 questions · first-try answers earn the most XP
My notes
Saved in this browser. Highlight a line above and save it, or write it in your own words.
Nothing saved yet. Your highlights will live here.
References
Finished reading?
Ticking it here also ticks the chapter in the sidebar, the section count and your streak — it is all one number.
Try a design drill
Related chapters
Spotted a mistake or want a topic covered? Report an issue