Transactions, Explained | Introduction to Operating Systems

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

← 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 →

Earn 3 college credits for Introduction to Operating SystemsRecommended for credit by NCCRS · Computer Science 354 · self-paced · one payment, lifetime access

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.

Compare: Linux vs Operating Systems

Why take it here

Why students take Introduction to Operating Systems with UPI Study

What happens under every app you runIntroduction to Operating Systems explains the software that runs a computer: processes and scheduling, threads and synchronization, deadlock, memory and paging, devices and storage, file systems, networking, security and virtualization. It suits computer science and information technology degrees.
Credit at a fraction of the priceThe same 3 credits can cost $750 or more at a US college. Here it is one payment, self-paced, with lifetime access.
Finish your degree soonerTransfer credits reduce the courses you still owe, so you can graduate earlier or lighten a heavy semester.
Strengthen an applicationA completed college-level course on an official transcript shows admissions and scholarship committees you can do the work.
Study on your own scheduleNo start dates, no deadlines. Watch the lessons, practice, and take the final when you are ready.
130,000+students helped with college creditsACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

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.

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?
ACID stands for atomicity, consistency, isolation and durability. Atomicity means every step of a transaction happens or none does. Consistency means the data obeys its rules before and after, such as no negative balances. Isolation means transactions running at the same time do not see each other's half-finished work. Durability means a committed result survives a crash or power failure. Atomic here means indivisible in outcome, not fast.
What is the difference between commit and rollback?
Commit makes every change since the transaction began permanent, while rollback undoes all of them. In Python's sqlite3 module, a transfer function can debit one account, credit another, and commit if both steps succeed. If the credit fails, for example because the receiving account name has a typo, the except branch rolls back, the debit disappears too, and the failed transfer leaves no trace.
How does write-ahead logging work?
Write-ahead logging requires the log record for a change to reach disk before the change itself is written to the data. The log is append-only and stores each change's old value, used to undo it, and new value, used to redo it, plus a commit record at the end of each transaction. After a crash, recovery redoes transactions that have a commit record and undoes those that do not.
How does two-phase commit work in a distributed system?
Two-phase commit lets several computers agree to commit or roll back together. In phase one, a coordinator asks every participant whether it can commit, and each participant writes its plan to its own log before answering yes or no. In phase two, the coordinator tells everyone to commit if every answer was yes, or tells everyone to roll back if any answer was no.

About the Introduction to Operating Systems course

How many college credits is Introduction to Operating Systems?
Introduction to Operating Systems (Computer Science 354) carries a NCCRS credit recommendation for 3 semester credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
How much does Introduction to Operating Systems cost and what is included?
$250, paid once, with lifetime access: all 83 video lessons, a practice quiz after each, the graded work and final exam, and an official transcript when you finish. Students who want several courses choose UPI Study PRO instead.
Which universities accept UPI Study credits?
UPI Study has signed partner agreements with universities such as Thomas Edison State University, University of the People and Charter Oak State College, and works with universities such as Penn State, SNHU and SUNY. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities. Use "Find my college" on this page to check yours.
How long does Introduction to Operating Systems take?
It is self-paced with no deadlines. The 83 lessons total about 10 hours of video; most students add reading and practice time and finish in a few weeks to a few months, on their own schedule.
How are the exams done?
Quizzes and assignments are online in the course. The final exam is taken online with proctoring; a person on the UPI Study team checks assignments, usually within two days.
How do my credits reach my college?
When the course shows 100% complete, request the official transcript from your dashboard. It is sent to the college you name, and you can send it to more than one. Check your college first with "Find my college" on this page, or ask Athena.

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

Introduction to Operating Systems
3 credits · NCCRS
Enroll →