Transactions and concurrency
도입됨이 수업의 내용
| English | 의미 |
|---|---|
| transaction/trænˈsækʃn/ | 트랜잭션 |
| atomicity/ətəˈmɪsɪti/ | 원자성 |
| consistency/kənˈsɪstənsi/ | 일관성 |
| isolation/ˌaɪsəˈleɪʃn/ | 격리성 |
| durability/ˌdjʊərəˈbɪlɪti/ | 지속성 |
| Serializability/ˈsɪərɪəlaɪzəbɪlɪti/ | The result equals some order of running the transactions one at a time. |
| Two-phase locking/tuː feɪz ˈlɒkɪŋ/ | a transaction takes all its locks before it releases any, which guarantees serialisable schedules |
| Isolation levels/ˌaɪsəˈleɪʃn ˈlevlz/ | settings that decide how much concurrent transactions may see of each other's changes |
| dirty reads/ˈdɜːti riːdz/ | reading data written by a transaction that has not yet committed |
| write-ahead log/raɪt əˈhed lɒɡ/ | a log in which every change is recorded before it is applied to the database |
| group commit/ɡruːp kəˈmɪt/ | writing several transactions' log records to disk in one operation |
| multiversion concurrency control/ˌmʌltɪˈvɜːʃn kənˈkʌrənsi kənˈtrəʊl/ | keeping several versions of each row so that each transaction reads a consistent snapshot and readers never block writers |
| write skew/raɪt skjuː/ | an anomaly where two transactions each read consistent data and write different rows, together breaking a rule neither broke alone |
| waits-for graph/weɪts fɔː ɡræf/ | a graph with an edge from each transaction to the one whose lock it waits for; a cycle means deadlock |
All or nothing
- A transaction 事务 happens completely or not at all. ACID names its four promises: atomicity 原子性 (all or nothing), consistency 一致性 (the constraints stay true), isolation 隔离性 (running side by side looks like running one after another) and durability 持久性 (a commit survives a crash).

The A in ACID stands for —
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
All or nothing: the transaction commits completely or not at all.
The lost update, bank edition
- T1 reads 100, T2 reads 100, T1 writes 90, T2 writes 120. The withdrawal of 10 has vanished: the same race as in lesson 0703, now with money.
R1(A) R2(A) W2(A) W1(A)ends at 90, and T2's deposit is lost.R1(A) W1(A) R2(A) W2(A)ends at 110, the same as T1 then T2. Serializability 可串行化 means the result equals some order of running the transactions one at a time.
R1(A) R2(A) W2(A) W1(A) from 100 with -10/+20 ends —
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
T1 writes last from its stale read: 100 - 10 = 90, the deposit lost.
Locks and levels
- Two-phase locking 两阶段锁 (2PL): a transaction takes all the locks it needs before it releases any (a growing phase, then a shrinking phase). That alone guarantees the outcome equals a serial order.
- Strict 2PL, used by most engines, also holds write locks until commit. The price is deadlock: the detector stops one transaction, which then tries again.
- Isolation levels 隔离级别 name the problems you accept. A dirty read 脏读: T2 sees T1's 90 before T1 commits, and T1 then rolls back. A changed re-read: the same row reads differently twice. A phantom: a re-run query finds new rows.
| Level (can it happen?) | Dirty read | Changed re-read | Phantom |
|---|---|---|---|
| read uncommitted | yes | yes | yes |
| read committed | no | yes | yes |
| repeatable read | no | no | yes |
| serializable | no | no | no |
- Keep transactions short: locks held while a user types invite deadlock.
Reading another transaction uncommitted rollback requires fixing to at least —
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
Read-committed shows only committed data - the dirty read ends there.
Order the isolation levels from weakest to strongest.
난이도 3/5 · 200코인 + 오늘 첫 완료 시 작은 보너스 추가
Each level removes one more anomaly: dirty reads, then unstable re-reads, then phantoms. Stronger levels lock or check more, so they cost more concurrency.
Recovery: the write-ahead log
- The write-ahead log 预写日志 rule: the log record for a change reaches disk before the changed page does, and a transaction commits when its commit record is on disk. After a crash, the engine redoes committed work and undoes the rest.
- Log: T1 begins, T1 sets A from $100$ to $90$, T2 begins, T2 sets B from $50$ to $70$, T1 commits, crash. Recovery redoes T1 ($A = 90$) and undoes T2 ($B = 50$).
- A log flush takes about $5$ ms, so group commit 组提交 puts several transactions in one flush: $10$ per flush gives $2000$ commits a second instead of $200$.
Log: T1 sets A 100 to 90; T2 sets B 50 to 70; T2 commits; crash. After recovery —
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
T2 committed, so it is redone (B = 70); T1 did not, so it is undone (A = 100).
A log flush takes 5 ms and group commit puts 25 transactions in each flush. How many commits per second?
난이도 2/5 · 140코인 + 오늘 첫 완료 시 작은 보너스 추가
200 flushes a second x 25 = 5000 commits a second.
Snapshots instead of read locks
- Most engines use multiversion concurrency control 多版本并发控制 (MVCC): a write creates a new version, and each transaction reads the snapshot that existed when it began. Readers never block writers, and writers never block readers.
- Snapshot isolation still allows write skew 写偏斜. Two doctors on call each check that the other is on call, and both sign off. Each read was consistent, yet nobody is on call. Only serializable isolation stops this.
Snapshot isolation prevents every anomaly that serializable isolation does.
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
It allows write skew, as in the two doctors going off call.
Under MVCC, a long report query and an update to the same rows:
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
Each reads its own version; readers and writers do not block.
Worked example: break a deadlock cycle
- Under two-phase locking, T1 waits for T2, T2 waits for T3 and T3 waits for T1. The waits-for graph 等待图 has a cycle, so none of them can ever continue.
- Aborting any one transaction breaks the cycle: $\mathbf{1}$ abort is enough. The engine picks the cheapest victim, often the one that has done the least work, rolls it back and runs it again.
Waits-for cycle T1 → T2 → T3 → T1: fewest aborts that break it?
난이도 2/5 · 140코인 + 오늘 첫 완료 시 작은 보너스 추가
Removing any one node breaks the cycle.
Marks that slip away
- Trusting each statement on its own. Two transactions that read and then write can lose an update unless they are isolated.
- Releasing locks early. Two-phase locking holds them until commit; release them sooner and others see or overwrite half-done work.
- A transaction open during user input. Its locks stay held while the user thinks, which invites deadlock.
Recap
- ACID: atomicity, consistency, isolation, durability.
- Serializable means equal to some serial order; two-phase locking guarantees it, at the price of deadlocks.
- Isolation levels name the problems you accept, from dirty reads to phantoms.
어떤 문제가 있었나요? 사이트 팀만 이 내용을 볼 수 있습니다.