Skip to main content

Command Palette

Search for a command to run...

πŸ’³ Transactions Explained Like You're 5

Published
β€’2 min read
S

Building AI systems and writing about how they actually work. Master of AI @ University of Technology Sydney. Previously B.Tech CS with focus on IoT. I believe the best way to learn is to explain. That's why I'm documenting tech concepts with simple analogies (@sreekarreddy.com). AWS Certified β€’ Azure AI Certified β€’ Neo4j Professional β€’ Google Data Analytics When not coding: exploring Sydney, working on side projects, and teaching tech to anyone who'll listen.

A bank transfer that can't be half-done

Day 48 of 149

πŸ‘‰ Full deep-dive with code examples


The Bank Transfer

You transfer some money from savings to checking.

What if it crashed halfway?

  • Savings: -$100 βœ…
  • Checking: +$0 ❌
  • The transfer is now inconsistent 😱

Transactions prevent this!


All or Nothing

A transaction groups operations:

START TRANSACTION
  1. Take money from savings
  2. Add money to checking
COMMIT (make permanent)

If anything fails β†’ ROLLBACK (undo everything!)

Either BOTH happen or NEITHER happens.


The ACID Rules

LetterMeaningExample
AAtomicAll or nothing
CConsistentConstraints/invariants stay true
IIsolatedOthers don't see half-done state
DDurableOnce committed, it survives crashes

Real Example

BEGIN TRANSACTION;

UPDATE savings SET balance = balance - 100;
UPDATE checking SET balance = balance + 100;

COMMIT;  -- Make it permanent
-- or ROLLBACK; if something went wrong

In One Sentence

Transactions ensure multiple database operations either all succeed together or all fail together, avoiding partially-applied state.


πŸ”— Enjoying these? Follow for daily ELI5 explanations!

Making complex tech concepts simple, one day at a time.

More from this blog

esreekarreddy

132 posts