6.3 Group Commit: Many Commits, One fsync

If fsync is expensive, the number of calls must be reduced. But skipping fsync at each commit breaks durability. The method that satisfies both is group commit.

The principle is simple. If several transactions request commit at nearly the same time, their logs are gathered, fsync is done just once, and all of them are told that the commit is complete. As seen in Section 5.2, the log accumulates sequentially at the end of the file, so the logs of ten transactions form one contiguous range on disk. fsync is a request to "record everything up to here on the persistent medium," so the cost is almost the same whether one record or ten records are flushed.

Figure 6-2. Group commit timeline

sequenceDiagram
  participant T1 as T1
  participant T2 as T2
  participant T3 as T3
  participant T4 as T4
  participant T5 as T5
  participant DB as Log buffer
  participant D as Disk
  T1->>DB: Commit request
  DB->>D: fsync 1 starts, T1 log
  T2->>DB: Commit request, only adds log and waits
  T3->>DB: Commit request, only adds log and waits
  T4->>DB: Commit request, only adds log and waits
  T5->>DB: Commit request, only adds log and waits
  D-->>DB: fsync 1 complete
  DB-->>T1: Commit complete
  DB->>D: fsync 2 starts, T2-T5 logs as one range
  D-->>DB: fsync 2 complete
  DB-->>T2: Commit complete
  DB-->>T3: Commit complete
  DB-->>T4: Commit complete
  DB-->>T5: Commit complete

Five transactions, T1 to T5, request commit within a few milliseconds of each other. Time flows from top to bottom.

  1. T1 requests commit first, and the database starts the first fsync with T1's log.
  2. During the hundreds of microseconds the first fsync takes, T2 to T5 request commit one after another. They do not start a new fsync; they add their logs to the log buffer and wait.
  3. When the first fsync finishes, T1 is told that the commit is complete.
  4. The database bundles the logs of T2 to T5 that accumulated in the meantime into one contiguous range and runs the second fsync once.
  5. When the second fsync finishes, all four transactions T2 to T5 are told at once that their commits are complete.

Five commits were handled with two fsync calls. By the time each transaction receives its response, its log is already on disk, so durability is preserved.

In this structure, the more transactions request commit at the same time, the more commits are bundled into a single fsync, so the cost per commit actually goes down. On an idle server there is one fsync per commit, but on a busy server one fsync handles dozens of commits. The response time of an individual transaction may increase slightly (because it has to wait for the preceding fsync), but the throughput of the whole server far exceeds the physical limit of fsync.

Group commit works by default in all four DBMSs, and users rarely need to turn it on or off. Still, when you encounter "concurrency went up but the cost per commit actually dropped," or "in a load test, raising the connection count increased throughput more than proportionally," knowing that group commit lies behind it lets you interpret the results correctly.