Courses System design · Part 5 · Lesson 7
SQL or NoSQL, and CAP in Plain Words
In interviews you often hear: “I use NoSQL here because it scales.” That sentence says almost nothing. This lesson helps you make the choice with a reason.
A good default: the relational database
PostgreSQL and MySQL keep data in tables with defined columns, and give things that are hard to find elsewhere:
- Transactions: several changes happen together, or none of them (what the write-ahead log makes possible).
- Constraints: unique values, foreign keys, “this column is never empty”. The database stops wrong data.
- Flexible queries: joins, grouping, filters on any column with an index. You do not need to know every question in advance.
With the right indexes, a cache and a few read replicas, a relational database handles thousands of writes and tens of thousands of reads a second. Most products never pass that line. So unless you have a clear reason for something else, start with SQL.
Four NoSQL families
NoSQL is not one thing. It is several kinds of database, each very good at one job, each giving something up in return.
| Family | Example | Good for | What it gives up |
|---|---|---|---|
| Key-value | Redis, DynamoDB | Very fast reads and writes by key: sessions, caches, counters | Queries on anything but the key |
| Document | MongoDB | Records whose shape differs from one to the next and are read together: product catalogs, profiles | Joins, and sometimes transactions across documents |
| Wide-column | Cassandra, ScyllaDB | Huge write volumes spread over data centers: event logs, messages, sensor data | Flexible queries; you design each table for one query |
| Graph | Neo4j | Relationships many steps deep: friends of friends, recommendations, fraud detection | Easy horizontal scaling |
A practical rule: look at the main queries. If they are almost always “everything about key X” and writes are very heavy, key-value or wide-column fits. If queries vary and correctness is vital (money, stock), relational.
Many real systems use both: orders and payments in PostgreSQL, sessions and counters in Redis, events in a log.
The CAP theorem
The CAP theorem is about a system that replicates data over several servers. Three properties:
- C, Consistency: every read sees the latest write, as if there were only one copy.
- A, Availability: every request to a healthy server gets an answer.
- P, Partition tolerance: the system keeps working when the network between servers is cut.
The famous line “pick two of three” is misleading. Network cuts happen; they are not a choice. So the theorem really says:
While the network is cut, you must choose between C and A.
An example: two servers in two cities, and the cable between them is cut. A user in the first city changes their balance. A user in the second city reads the balance. The second server has two options:
- Not answer (“unavailable for now”) until the link is back: consistent, but not available. That is CP.
- Answer with what it has, which may be old: available, but not consistent. That is AP.
You have seen this in the labs. In replication, when only one node of three is on, the system refuses writes (CP). In quorums with W = 1 and R = 1, the system always answers, but sometimes with an old value (AP).
When the network is fine
Most of the time the network is fine. Then the choice is between consistency and latency: waiting for several replicas to confirm is more correct, but slower. This extension is called PACELC: in a Partition, choose A or C; Else, choose Latency or Consistency.
A choice per kind of data
The whole system does not need one choice:
- Account balances, warehouse stock, seat bookings: correctness matters most. CP, or transactions in a relational database.
- Like counts, news feeds, “last seen”: a little staleness is fine. AP and speed.
In an interview
- Do not say “NoSQL scales”; say which family, for which query, and what you give up.
- State CAP precisely: the choice between C and A is only during a network split.
- Decide separately for each kind of data, and give the reason with a user example.
Summary
- Start with a relational database unless you have a clear reason.
- Each NoSQL family does one job very well and gives something up.
- CAP: during a network split, either answer correctly or always answer; you cannot do both.