1. Gyanastra
  2. Database Systems
  3. Transactions and ACID
  4. Transaction Execution and CPU/IO Interleaving
Free lesson, no account needed 5 min read 2 practice questions after it

Database Systems · Transactions and ACID

Transaction Execution and CPU/IO Interleaving

Overview

A transaction, viewed from the process perspective, is not a single monolithic unit of work: it alternates between CPU parts and I/O parts.
Because database transactions are I/O-heavy, running them serially leaves the CPU idle most of the time; interleaving another transaction's CPU work into the I/O gaps keeps the CPU busy.

Key concepts

  • CPU parts: operations that need the CPU to compute something.
  • I/O parts: operations that need to read or write data from the disk, where the process may have to wait for the disk.
  • Transaction as a process: each transaction is run by a process, and that process alternates between the two kinds of parts, e.g. I/O → CPU → I/O → CPU → ...
  • CPU-bound part: an operation that requires the CPU to compute it.
  • I/O-bound part: an operation that must read or write data from the disk; the request is given to the disk and the process might have to wait.
  • Idle CPU during I/O: while the process executes I/O statements, the CPU is typically idle; while it executes compute statements, the CPU gets work.
  • I/O dominates in databases: most of the work in any transaction is accessing the database, so I/O-related work is more than CPU work.
  • Serial execution: running transactions one after the other (T1 fully, then T2 fully, ...) leaves the CPU with nothing to do during every I/O phase of the running transaction.
  • CPU efficiency rule: to increase the efficiency of a CPU you must keep it busy as long as possible — the same idea the operating system uses to manage processes.
Figure. T1 runs I O then CPU then I O then CPU. During each I O stretch the CPU is idle, so T2 computation is scheduled there; the CPU stays busy in all four steps.

How it works

  1. Transactions T1, T2, T3, ... up to N transactions arrive and are queued up; one process executes each transaction.
  2. Each transaction shows a two-part structure: operations requiring CPU computation and operations reading or writing data from disk.
  3. During the I/O statements the CPU is typically idle; during the compute statements the CPU gets work.
  4. In databases, I/O-related work is more than CPU work, because most of the work is accessing the database.
  5. If transactions are executed serially, the CPU is idle for most of the time — during every I/O phase of the running transaction it has nothing to do.
  6. To keep the CPU busy as long as possible, whenever one transaction is performing I/O, hand the CPU over to another process so that the other process can do its computing work.
  7. Transaction T1 does only I/O for some time, then some CPU work, then only I/O again, then CPU work again, and so on; the CPU is unused exactly during the "only I/O" stretches.
  8. If another transaction T2 has computation work to do, then whenever T1 is busy with I/O, schedule T2 (give the CPU to T2's work).

Worked examples

Interleaving T1 and T2 to keep the CPU busy

Pattern of T1: I/O phase, then CPU phase, then I/O phase, then CPU phase.

StepWhat T1 is doingState of the CPU if only T1 runsWhat the CPU can do instead
1I/O (disk read/write, process waits)IdleRun T2's computation work
2CPU workBusy with T1Busy with T1
3I/O (disk read/write, process waits)IdleRun T2's computation work
4CPU workBusy with T1Busy with T1

Result: by scheduling T2 during T1's I/O phases, the CPU is kept busy in steps 1 and 3 as well, instead of sitting idle.

Comparison

AspectExecuting transactions one after the other (serial)Scheduling another transaction during I/O
CPU during a transaction's I/OIdleGiven to another transaction's CPU work
CPU utilisationLow — CPU idle for most of the time, because database transactions are I/O-heavyHigher — CPU is kept busy as long as possible
ReasonThe single running process is waiting on the diskTwo transactions' CPU/I/O phases are overlapped

Revision summary

  • From the process perspective, a transaction consists of CPU parts and I/O parts in alternation.
  • I/O parts mean reading/writing data from disk; the process waits, so the CPU is idle during these parts.
  • In database transactions, I/O work is greater than CPU work, since most of the work is accessing the database.
  • Serial execution of transactions wastes the CPU: it stays idle most of the time.
  • Efficiency rule (same idea as OS process management): keep the CPU busy as long as possible.
  • Method: whenever one transaction is doing I/O, give the CPU to another transaction that has computation work (e.g., T1 doing I/O → run T2).
  • Transaction T1's shape is I/O → CPU → I/O → CPU ...; the CPU gaps are exactly the I/O stretches, which must be filled by another transaction.

Previous-year exam insights

  • Interleaving transactions to overlap one transaction's I/O with another's CPU work improves CPU utilisation and throughput but does not by itself guarantee serializability; correctness must be enforced separately by concurrency-control protocols (e.g., locking, timestamp ordering).
  • A schedule is serializable if its effect is equivalent to some serial execution of the same transactions; conflict serializability is checked with a precedence (serialization) graph, which must be acyclic.
  • In a conflict-serializable schedule, conflicting operations (read-write, write-read, write-write on the same data item by different transactions) must occur in the same order as in some serial schedule; read-read conflicts never matter.
  • The ACID properties are: Atomicity (all-or-nothing, ensured by recovery/logging), Consistency (preserves DB constraints), Isolation (concurrent execution equivalent to some serial order, ensured by concurrency control), Durability (committed effects survive failures, ensured by stable storage/logging).
  • A transaction's states are active, partially committed (after its last statement), committed (after commit record is durable), and aborted/failed; a rollback undoes its effects using the log.
  • CPU utilisation with interleaving is bounded by the number of concurrently runnable processes and by the fraction of time any transaction has CPU work available; if all transactions are simultaneously in I/O, the CPU still idles.
Pages 1–2 of 2