Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Preview

AESP task 3: ACID: keeping changes safe

This is the first page of the lesson. Read it and have a go at the activities. The rest is in the full lesson.

55 minutes

Money that vanishes

Sam sends £20 to Priya. The system takes £20 from Sam. Then the power fails.

Priya never got the money. Sam has lost it. Four simple promises, called ACID, stop this from ever happening.

Key words

WordWhat it means
Transactiona group of changes that must all happen or none happen
Commitmake the changes of a transaction permanent
Rollbackundo the changes of a transaction
Atomicall or nothing: the group can not be left half done
Consistentthe data always follows its rules
Isolatedtwo users at once do not spoil each other's changes
Durableonce saved, a change survives a crash

The cells students fill in are left blank in this preview.

By the end of this lesson

Note

I can explain each ACID promise, show it with a transaction and say when a design needs it.

Starter

Have a go

Starter

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Your answers are marked here and are not kept. A college account keeps every student’s marks.

This is practice

Important

The real task comes from the exam board. This lesson uses an invented organisation and its own made-up data. Your teacher tells you how the real tasks are set and what you may bring.

Worked example: ACID with a money transfer

LetterPromiseIn the transfer
A: AtomicAll or nothingBoth the take and the give happen, or neither does
C: ConsistentThe rules always holdA balance never goes below zero
I: IsolatedUsers do not spoil each otherTwo transfers at once do not mix up the balances
D: DurableSaved means savedAfter the done message, a crash does not lose the transfer

The cells students fill in are left blank in this preview.

Run it: a transfer that completes

Code: sql
BEGIN;
UPDATE account SET balance_pence = balance_pence - 2000 WHERE account_id = 1;
UPDATE account SET balance_pence = balance_pence + 2000 WHERE account_id = 2;
COMMIT;
SELECT owner, balance_pence FROM account ORDER BY account_id;

Run it: a transfer that is undone

Code: sql
BEGIN;
UPDATE account SET balance_pence = balance_pence - 2000 WHERE account_id = 1;
ROLLBACK;
SELECT owner, balance_pence FROM account ORDER BY account_id;

What those runs show

In the first run both changes were wrapped in one transaction and committed. Sam went from 5000 pence to 3000, and Priya from 2000 to 4000.

In the second, the first change was undone. Both balances are as they were. That is atomic: no half done state is kept.

The lesson goes on

6 more parts of this lesson are in the full curriculum, with the tasks, the final check and the labs. A college account gives your students all of it.