UPI Study › Courses › Introduction to Operating Systems › Free lessons › Lesson 3.6 Free with a free account
Transactions
Introduction to Operating Systems · Chapter 3: Concurrency, Synchronization, and Deadlock PreventionSection 3.6 · lesson 23 of 837 min · free with a free account
Free preview
Create a free account to keep watching
It takes a minute and costs nothing. Your free account unlocks all of Chapter 1 and 3 more lessons of Introduction to Operating Systems.
Lesson complete
Earn the 3 college credits for Introduction to Operating Systems
Enrolling adds a quiz after each lesson, the graded work and the official transcript. Recommended for credit by NCCRS.
Next lesson: Cooperating Processes →← DeadlocksNext lesson: Cooperating Processes →
A free account plays all of Chapter 1 and 3 more lessons of Introduction to Operating Systems. Create a free account →
In this lesson
How commit, rollback and logging make multi-step changes all or nothing.
- Explain the four ACID properties of a transaction
- Use commit and rollback to keep a two-step transfer all or nothing
- Describe how write-ahead logging lets recovery undo or redo changes
- Outline the two phases of two-phase commit
Free study notes
Get this lesson's notes by email
The key points and 4 practice questions with answers, in your inbox. One email, free.
One email. Unsubscribe in one click.
Transcript
Everything said in the video, section by section (990 words). Read along, or search it.
Opening Scene
Forty Dollars Lost Between Two Steps
Sam, who runs a small online bakery, moves forty dollars of the day's takings from the shop till into the bank account. That's two steps. Take forty dollars out of the till, then put forty dollars into the bank. Now imagine the power goes out between the two. The till is lighter, the bank is no richer, and forty dollars have vanished. A transaction is the promise that this can never happen: either both steps are done, or neither one is.
Definition
One Unit of Work
A transaction is a group of steps that the system treats as one unit of work. Banks, shops, airlines and file systems all depend on it. Book a flight, and the system must take your seat and charge your card. Doing only one of them would be a disaster. The steps of one transaction finish together, or the whole group is undone, as if it never began.
ACID Properties
Atomicity, Consistency, Isolation, Durability
Transaction systems promise four things, remembered as ACID. Atomicity: all the steps happen, or none do. Both the debit and the credit, or neither. Consistency: the data obeys its rules before and after. The total money stays the same, and no balance goes negative. Isolation: transactions running at the same time don't see each other's half-done work. A report never sees money in the air between two accounts. Durability: once committed, the result survives a crash. After done, a power cut can't undo the transfer. And atomic here doesn't mean fast. It means indivisible in outcome, however long it takes. In lesson 3.3, the same word meant one CPU step: same idea, bigger size.
Code Walkthrough
Commit and Rollback in SQLite
Two commands carry the whole idea. Commit says: I'm done, and all went well, so make it permanent. Rollback says: something went wrong, so undo everything since the transaction began. Python ships with a small database engine, SQLite, in its sqlite3 module, so we can try both without installing anything. This program keeps the shop's two accounts in a table, in cents. The transfer function debits one account, then credits the other. If the credit finds no account to change, it raises an error, and the except branch rolls back. If everything worked, it commits.
Program Output
A Typo Triggers a Rollback
It makes two transfers. The first is correct. The second has a typo in the receiving account's name: b, n, a, k. Run it, and read from the top. In the good transfer, forty dollars moved. The till fell from ten thousand cents to six thousand, the bank rose to fifty-four thousand, and COMMIT made it permanent. In the second transfer, the after-the-debit line shows the till at three thousand. For a moment, the money had left the till and gone nowhere. Then ROLLBACK ran, and the last line shows the till back at six thousand. The failed transfer left no trace. One honest note: this demo's database lives in memory, so nothing in it outlives the program. Real durability needs a log on real storage.
Write-Ahead Logging
Log the Change Before the Data
How can a system undo a half-finished change, or finish one after a crash, when a power cut wipes memory? It keeps a log. A log is a file on disk that's only ever added to at its end. It records every change. The rule is called write-ahead logging: the log entry for a change must reach the disk, before the change itself is written to the data. For each change, the log holds the old value, to undo it, and the new value, to redo it. And a commit record marks the end of a transaction.
File Systems
Journaling Planned Changes First
File systems use the same trick. A journaling file system writes its planned changes to a journal first. After a crash, it can finish or drop each planned change, instead of scanning the whole disk. Lesson 6.8 names real examples.
Distributed Commit
Two-Phase Commit Votes
What if the till and the bank live on different computers? Each one can commit its own part, but they must agree together. Two-phase commit uses a coordinator that runs a short vote. In phase one, the coordinator asks every participant: can you commit? Each one writes its plan to its own log, and answers yes or no. In phase two, if every answer was yes, the coordinator tells everyone to commit. If any answer was no, it tells everyone to roll back.
Quick Check
Redo a Committed Transaction
Quick check. The power fails. After the restart, the log on disk shows transaction T7. It has a begin, the till going from six thousand to three thousand, the bank going from fifty-four thousand to fifty-seven thousand, and a commit. But the bank's balance in the data itself still says fifty-four thousand. The crash hit before that write. Pause and decide: what does recovery do? Redo it, and set the bank to fifty-seven thousand. The commit record is in the log, so the transaction counts as done. Durability promises that a committed result survives a crash, and the log's new values let recovery finish the job. Undo is for a transaction with no commit record. Then recovery puts the old values back, as if the transaction never ran. And the data on disk can't win, because of the write-ahead rule: the log is always the more complete record.
Real Case
Payment and Order Together
Back to Sam. The shop's order program once took a payment, then crashed before saving the order. The customer paid for nothing. Ivy, who looks after the computers at a school, had the fix: put the payment and the order in one transaction. If the order fails to save, the payment rolls back too. Her one warning: keep a transaction short, with only the steps that must go together. Long transactions hold their locks for a long time, and make everyone else wait.
Key Takeaways
All or Nothing, Logged and Agreed
To recap. A transaction is all or nothing, and ACID names its four promises. Commit makes the steps permanent. Rollback undoes everything since the start. Write-ahead logging puts each change in the log before the data, so recovery can undo or redo. And two-phase commit gets several computers to agree: all yes, or everyone rolls back.
About Introduction to Operating Systems at UPI Study
Introduction to Operating Systems (Computer Science 354) is a self-paced online course of 83 video lessons in 10 chapters, from what an operating system does and process scheduling to concurrency, memory, devices, file systems, networks, the internet, security and virtual machines. It carries a NCCRS credit recommendation for 3 semester credits.
Every lesson transcript is free on pages like this one, and the videos are free with a free UPI Study account. Enrolling adds a practice quiz after each lesson, the graded work and final exam that earn the credit, lifetime access, and an official transcript on completion. UPI Study has helped over 130,000 students with college credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
Enroll now → Course details Download the syllabus All Computer Science courses Every course with PRO →
Compare: Linux vs Operating Systems
Why take it here
Why students take Introduction to Operating Systems with UPI Study
Where the credit goes
Universities that work with NCCRS
Introduction to Operating Systems carries a NCCRS credit recommendation. These universities work with NCCRS; some are UPI Study's signed partners. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
Thomas Edison State UniversitySigned partner
University of the PeopleSigned partner
Charter Oak State CollegeSigned partner
Strayer UniversitySigned partner
Columbia Southern UniversitySigned partner
Indiana TechSigned partner
Penn StateWorks with NCCRS
Southern New Hampshire UniversityWorks with NCCRS
SUNYWorks with NCCRS
+many more cooperating universitiesFind my college →Find my college → Not listed? Ask Athena in the corner of this page, or book a 15-minute call.
FAQ
Questions students ask us
About Transactions
What does ACID stand for in database transactions?
What is the difference between commit and rollback?
How does write-ahead logging work?
How does two-phase commit work in a distributed system?
About the Introduction to Operating Systems course
How many college credits is Introduction to Operating Systems?
How much does Introduction to Operating Systems cost and what is included?
Which universities accept UPI Study credits?
How long does Introduction to Operating Systems take?
How are the exams done?
How do my credits reach my college?
Ready for the full course? Earn 3 credits in Introduction to Operating Systems.
83 video lessons, a quiz after each one, and the graded work that earns the credit. Recommended for credit by NCCRS.
Updated
3 credits · NCCRSEnroll →