Lessons / Rule 5

🧩 All or Nothing

Keep business operations atomic with transactions.

FreeData Integrity7 min read
Either it fully happens or it does not happen at all.

Moving money should never leave half a move.

If you move 10 coins from your pocket to a friend, it should not vanish from you without arriving to them. A transaction makes the whole move succeed or not happen.

💡 Key ideas

  • Do related changes together
  • If one part fails, undo everything
  • Never leave half-finished records that look done

🛠️ How to apply it

  • Wrap related writes in one transaction.
  • Never split a business step across two saves.

Reading level: 🐣 Simple

A business operation is one unit, not four database calls.

Identify the smallest business operation that must succeed or fail as one unit. Inside a transaction: validate, apply changes, commit; on unrecoverable error, roll back.

💡 Key ideas

  • Avoid unnecessary commits in the middle of a business transaction
  • Do not leave partially-created records that appear successful
  • Do not hold a transaction across external APIs, email, payments or queues

🛠️ How to apply it

  • Keep the transaction short: only the atomic database work
  • Hand off slow side effects to durable events/jobs with idempotent handlers

⚠️ Common pitfalls

  • Wrapping an email call or third-party API inside a database transaction

Reading level: 🚀 Standard

Transactions guarantee local atomicity — not distributed atomicity.

ACID gives you isolation and all-or-nothing within one database. Once a workflow spans systems, you need a pattern: transactional outbox, sagas, or compensation — plus idempotent, retry-safe steps.

💡 Key ideas

  • Model the boundary: what must be atomic, and what can be eventually consistent
  • Outbox pattern: commit business data + outbox event in one transaction, then publish
  • Compensation is not rollback — design the compensating action explicitly

🛠️ How to apply it

  • Emit side-effect intents inside the same transaction (outbox) for reliability
  • Ensure every downstream handler is idempotent so redelivery is safe

⚠️ Common pitfalls

  • Assuming a "transaction" spans your API call and a payment gateway
  • Long-held locks causing contention and timeouts under load

Reading level: 🧠 Deep

Try it yourself

All-or-Nothing Transfer — a live simulator for this principle.

Quick check

Community

Ask a question, share your own analogy, or help someone else get it.