8. SQL vs NoSQL
1
The picture to keep in your head2
8.1 What are tables, rows and keys?3
8.2 What does ACID mean?4
8.3 What is normalisation, and what are joins?5
8.4 What kinds of NoSQL databases are there?6
8.5 What's the difference between schema-on-write and schema-on-read?7
8.6 Why does NoSQL often duplicate data and avoid joins?8
8.7 How do you choose between SQL and NoSQL?9
Memory card and where the analogy breaks10
Practice: five levels11
Interview questions12
Solutions13
Sources14
Code lab8. SQL vs NoSQL
8.2 What does ACID mean?
3 min read+10 XP
In the office: The accountant has four rules. A sale is recorded completely or not at all. The books always balance. Two clerks writing at once don't scribble over each other. And once written in ink, it survives even if the lights go out.
In the system: ACID is the set of promises a database makes about transactions: groups of changes treated as one unit.
| Letter | Means | Bookstore example |
|---|---|---|
| Atomicity | All the changes in a transaction happen, or none do | Save the order and reduce the stock, or neither |
| Consistency | Every transaction leaves the data obeying its rules | Stock can never go below zero; every order points at a real user |
| Isolation | Transactions running at the same time don't see each other's half-done work | Two people buying the last copy don't both succeed (Chapter 17) |
| Durability | Once committed, the change survives a crash | The confirmed order is still there after a power cut |
In code, a transaction looks like this:
sql
Note the C in ACID isn't the same "consistency" as in CAP (Chapter 16). Here it means "the database's rules still hold".
Memory code: "All or nothing, rules always hold, no peeking, written in ink." Atomic, Consistent, Isolated, Durable.