seegongsik
Saved words
Data

Transactions: write at once without tangling

Say you send a friend ten dollars. You take ten from your balance and add ten to your friend's. Both must happen to be correct. But what if the take goes through and the power dies right before the add? Your money is gone and your friend got nothing. To prevent such accidents, treating several edits as one bundle is a transaction.

01

A transfer is a take and an add, a pair

Earlier we learned a database
is a warehouse that stores info safely.
But sometimes it is not one thing to change,
you must change two together.
A money transfer is exactly that.
You take money from your balance
and add the same to the receiver's balance.
These two are a pair you cannot split.
If only one happens, an accident strikes at once.

my balance30
friend balance10
sum of both40
Press the two buttons separately

Tap the take and the add separately. If only one is pressed, money vanishes or appears from nowhere.

Press only the take and your money is gone.
Press only the add and money appears from nowhere.
Neither one is right.
The total sum of money has changed.
That short gap between take and add,
right there is the dangerous stretch.
If something cuts into that gap
the ledger goes out of balance.
So we need a way to bind the two.

02

All happen, or none do

The fix is surprisingly simple.
Bundle the take and the add into one.
This bundle is called a transaction.
Every edit inside the bundle is tentative.
Only when all finish does it apply for real, all at once,
and if even one slips, all are cancelled.
So nothing is ever left half done.
All happen, or none do.
The middle state is shown to no one.

one bundle (transaction)
stage take
stage add
my balance30
friend balance10
Stage the two edits into the bundle

Run it as one bundle. It commits only when both edits are ready, and cancelling drops both.

Both commit together, so the sum fits exactly.
Press cancel and both vanish cleanly.
Either way the total stays the same.
Merely by treating it as a bundle
that dangerous gap is gone.
Handled this way, all or nothing,
the ledger can never go out of balance.
But what if, midway through the work inside the bundle,
the power really does die?

03

Even cut off, roll the whole thing back

We said edits in the bundle are tentative.
That tentativeness is the very safeguard.
It does not forget the state before the work began,
it writes it down on the side.
So even if the power dies midway,
when it comes back it can tell:
this bundle never finished.
Then, rather than leave half done,
it rolls the whole thing back to the noted earlier state.

state before start (noted aside)30 / 10
my balance30
friend balance10
Advance one step, then cause a cutoff

Advance one step, then tap the cutoff. Instead of leaving half done, it rolls back fully to the start.

Cut off, yet the ledger is unharmed.
Instead of a messy half left behind
it went cleanly back to before the start.
Rolling the whole thing back is called a rollback.
Since failure leaves no trace,
you can simply try again from the start.
Thanks to that, you can entrust the work in peace.
But when several people try to change
the same balance at once, what happens then?

04

Even arriving at once, handled in turn

We saw something like this in the operating system.
When two touch the same resource at once,
the result tangles, a race condition.
A balance is just the same.
Two people read the balance at once
and each writes back their own changed value,
so the later writer overwrites the earlier result.
One whole change just vanishes.
So we make them be handled one at a time, in turn.

person X (+5)+5
person Y (+8)+8
same balance100
Sent at once, one overwrites the other

Send two people at once. Left alone one overwrites and it tangles, but held in turn both apply.

Left alone, one side's change vanished.
Handled in turn, both applied just fine.
Even arriving at once, they queue up a moment,
and the next is taken only after one finishes.
Just by setting an order, overwriting is gone.
Added to the bundling and rollback from before,
now writing at once does not tangle.
When these three come together, data becomes
dependable enough to trust and entrust.

05

Let's wrap up

Gathered on one line, it is this.
Bundle edits that must change in pairs into one.
All happen, or none do.
If something slips midway, instead of leaving half
it rolls the whole thing back to before the start. That is rollback.
When several try to change the same place at once,
it queues them in turn and blocks overwriting.
All or nothing, roll back on a slip,
and no tangling at once. That is a transaction.

Tap the four key points in order
Tap the points below in order from the top

Tap the key points in order to review. (transfer is a pair -> all or nothing -> roll back on a slip -> no tangling at once)

Now you know a dependable promise
for changing data safely.
The dizzying gap where money vanished,
the accident of half left when power died,
the chaos of tangled simultaneous edits,
all untangled by one idea: a single bundle.
The more a system is shared by many,
the more precious this promise is.
Next too, let's keep exploring together
the wisdom of handling data.

In one lineA transaction treats several edits as one bundle so that all happen or none do. When a take and an add are a pair, like a transfer, only when both finish does it really apply. If the power dies or something goes wrong midway, it does not leave half done but rolls the whole thing back to the earlier state. This is called a rollback. And when several people try to change the same balance at once, it handles them one at a time so they do not overwrite each other. The tangle of all-at-once becomes an orderly line. In short: all or nothing, roll back on a slip, and no tangling even at once. These three are the transaction.
Data
Was this helpful? Support seegongsik