Part 4 of 6 · 3 min read
One object or many
Which guarantees a store gives for a single record, and when several records must change together.
Think of a single page of a ledger. Writing one line on it is easy to do safely. Updating two pages that must agree (a sales page and a totals page) needs care, since a visitor might open the book between the two writes.
The book separates two cases.
Single-object operations. Even a single record needs protection. If a 20 KB JSON document is being written and the connection drops after 10 KB, does the database keep the broken half? If the power fails while overwriting the old value, are old and new spliced? If another client reads during the write, does it see a partial value? Storage engines almost always give atomicity and isolation for a single object on one node. Atomicity uses a log for crash recovery, and isolation uses a lock on each object.
Some stores also offer special single-object operations:
- Atomic increment, which removes the read-modify-write cycle that caused the lost counter update.
- Compare-and-set, which writes only if the value has not changed since it was read.
Compare-and-set
Compare-and-set is an operation that writes a new value only if the current value still equals the one the client expects.
Example: set seats_left to 9 only if it is still 10. If another client changed it to 9 meanwhile, the operation fails and the client tries again.
The book warns that such operations have been marketed as "lightweight transactions" or even "ACID", which is misleading. A transaction usually means grouping several operations on several objects into one unit.
Multi-object transactions. Many cases need several records changed together. The book's list:
- Foreign keys. A row in one table refers to a row in another. Inserting records that refer to each other needs both to be right at once.
- Denormalised documents. In a document store without joins, copies of the same fact live in several documents. Updating all copies at once needs a transaction to keep them in step.
- Secondary indexes. An index is another object. Without isolation, a record could appear in one index and not in another.
The book's example: an email app stores a count of unread messages per user for speed. A new email must insert the message and add 1 to the counter. Without isolation, user 2 may see the new message in the list but a counter still at 0. Without atomicity, if the counter update fails, the message exists but the counter does not match.
Many distributed stores dropped multi-object transactions because they are hard to implement across partitions and can hurt availability and performance. The book says nothing prevents transactions in a distributed database in principle (module 9 returns to it). Key-value stores may offer a multi-put that updates several keys at once, but that does not mean it has transaction meaning: it can succeed for some keys and fail for others, leaving a partial update.
A worked example. A shop writes an order row, 3 order line rows, and reduces stock for 3 products: 7 writes. Without a transaction, a crash after 4 writes leaves an order with 1 line and wrong stock. With a transaction, a crash after 4 writes means all 4 are undone, and the client retries the whole order.
| Single-object operation | Multi-object transaction | |
|---|---|---|
| Scope | One record | Several records, tables or indexes |
| Typical support | Almost every store | Relational databases, some others |
| Protects against | Partial writes, torn reads, lost increments | Partial updates across records, out-of-sync copies |
| Cost | Low | Higher, especially across partitions |
Check yourself:
- Why is compare-and-set not a full transaction?
- Give one reason a secondary index makes multi-object atomicity matter.