Make the right trade-off / CHAPTER 42
ACID and BASE database properties
Follow a transfer to separate all-or-nothing balance changes from a delayed activity display.
Builds on SQL versus NoSQL data stores, Functional and non-functional requirements.
- Explain all four ACID properties.
- Distinguish transactional consistency from replica consistency.
- Use BASE without claiming transactions are impossible.
01What ACID promises within a boundary
Transfer $40 from account A to B. Initially A has $100 and B has $20. A successful transfer should leave both at $60; it must not save only the debit or allow concurrent spending to violate the balance rule.
ACID describes four transaction properties:
- Atomicity: the required changes commit together or none commit. Saving only
A's debit is insufficient. - Consistency: correct transaction logic and database constraints preserve declared rules, such as equal debit/credit and no negative balance.
- Isolation: a defined rule controls how concurrent transactions interact. Some levels prevent more interference than others.
- Durability: once the database reports commit success, it preserves that commit through the failures covered by its configuration.
Name the records covered. A transaction in the account database does not automatically include an email provider, another unrelated store or every replica's visible read state.
02BASE describes a tendency, not a universal API
Follow the difference between a money record and its display feed:
- The account transaction updates balances and saves its outcome. These records must preserve the transfer rule together.
- A separate activity feed receives the committed event later. The balance can be correct while the feed still shows the old display.
- The feed catches up or is repaired. The product may permit that delay, but it does not permit an incomplete debit/credit commit.
This explains two meanings of consistency. ACID consistency concerns declared record rules. Replica consistency concerns which saved version a read can observe across copies.
BASE is commonly expanded as basically available, soft state and eventual consistency. It describes a tendency toward serving through some disruptions and letting copies converge, not one precise API:
- The feed may remain usable while updates are delayed.
- Its displayed state can change as old pending events arrive, even without a new user action; this is the idea behind soft state.
- Local ACID account transactions can coexist with that delayed feed.
Also name isolation and acknowledgment settings. Serializable isolation makes concurrent transactions behave like some one-at-a-time transaction order. A weaker transaction level may allow races. Durable commit settings determine which saved copies exist when success is returned; a log or replica count by itself does not promise survival of every machine, regional or operator failure.
03Worked example: transfer and activity feed
Call the customer's one transfer request T8 so a retry can identify it.
- Verify permission, amount and
T8. Look for an existing matching request result; reject changed content under the same request identity. - Protect the balance check and updates. A transaction can lock the account rows or detect conflicts, then calculate
A=$60 andB=$60. Their combined $120 remains unchanged. - Commit both balances,
T8's result and pending feed work together. A unique request-identity constraint prevents concurrentT8attempts creating two transfer outcomes. The pending event is an outbox record. A failure before commit cannot leave only the debit as a completed transfer. - Return success after commit. If the response is lost, a matching
T8retry returns the saved transfer outcome rather than moving another $40. - A relay sends the saved feed event later. It can retry, and the feed recognizes repeated events. The relay's network send is not part of the local balance transaction.
Now start two $80 transfers that both read A=$100. Independent prechecks followed by unconditional writes are unsafe: both could act on the same old value. For example, lock the account row while checking and deducting. The first $80 transfer leaves $20; the second then reads $20 and rejects an $80 spend. Checking $100 outside that protected change would not enforce this rule. A serializable transaction may abort when conflicts make a safe commit impossible; retry the whole safe transaction, not merely its last statement.
Do not send irreversible email or payment effects during an attempt that may abort and retry unless that external service can resolve repeats safely. Rolling back local records cannot unsend an accepted email. The outbox saves the obligation to send without pretending the provider joined the transaction.
ACID and BASE database properties — decision flow
Follow the numbered arrows
- Transfer request → Balance transactioncheck permission and the saved request ID
- Balance transaction → Account databasesave balances, result and event together
- Account database → Pending-event senderread committed events awaiting delivery
- Pending-event sender → Activity feedsend the event; delivery may repeat
Trace the diagram in words
- Check T8’s permission and request identity, then protect concurrent balance changes.
- Commit both balances, the request outcome and pending event together.
- A separate relay later sends the saved event to the activity feed, which may display it after a permitted delay.
04Name the guarantees instead of choosing a slogan
Choose guarantees for each operation:
- Balance movement: show the records and concurrent checks preserving the monetary rule.
- Activity display: state its permitted delay, saved update work, repeated-event handling and repair path.
- Commit success: name which independent durable copies must contain the write and which losses they cover.
- Database family: read actual transaction and read guarantees. Some nonrelational stores support ACID transactions; some relational deployments serve old replica reads.
- External side effects: identify the provider's repeat-recognition or status-query mechanism, or honestly state duplicate tolerance.
A slogan cannot answer these questions. Explain the before/after balances, one overlapping transfer, a lost response and a delayed feed. They show how strong local record correctness and weaker display freshness can coexist without weakening the transfer itself.
DRAW. CALCULATE. EXPLAIN.
Separate a purchase from its projections
20 minutes · guided practice
A purchase reduces stock from 3 to 2, creates one order and later sends email and increments a popularity view. Two copies of the purchase request may arrive. State transaction participants, retry identity and delayed-effect recovery.
Compare the worked solution
- Save stock reduction, order, original request result and pending outbox work together in a supported transaction. Otherwise describe an explicit reservation workflow preserving the same acceptance rule.
- Concurrent copies of the same request must resolve to one saved purchase result, not two stock decrements.
- Relay committed events later. A popularity updater recognizes repeated event IDs before repeating its count change.
- Email needs a provider-recognized send ID or a way to query the uncertain send. Without that, document possible duplicate receipts.
- A delayed popularity counter does not invalidate the committed purchase. A partial stock/order change can violate its correctness.
The checkboxes record your own assessment. The worked solution gives you a way to check the reasoning.
RETRIEVE BEFORE YOU REVEAL
Keep the important ideas.
Say the answer aloud, then turn the card over.
CARD 01What do the four ACID properties cover?
- Atomicity commits required changes together.
- Consistency preserves declared rules.
- Isolation controls concurrent interference.
- Durability preserves successful commits through specified failures.
Name the covered records; external services do not join automatically.
CARD 02Why distinguish two consistencies?
- ACID consistency preserves rules such as matching debit and credit.
- Replica consistency describes versions reads may observe.
- A delayed feed can be acceptable; a partially committed transfer is not.
CARD 03Why send email after the business transaction?
- A rollback cannot unsend accepted email.
- Save pending email work with the business change.
- Send it separately with provider repeat-recognition or explicit duplicate tolerance.
CHECK THE REASONING
Can you explain the choice?
Answer every question, then finish for an immediate score and an explanation for each answer. Aim for 80%, then revisit any gaps.
CLOSE THE LOOP
Explain it without the page.
Summarise the mechanism, one trade-off, and one failure mode. Then mark this chapter complete.