CSE Roadmap

Core tier · about 3 hours · 7 parts

Part 5 of 7 · 2 min read

Group commit

Think of a bus that leaves only when a certain number of passengers are on board, or after a fixed time. The bus is more efficient than a taxi for each passenger, and a passenger may wait for it. The analogy stops in one place: the passenger only waits, while a record that is not yet synced is at risk of being lost.

In plain words: Group commit collects several records and makes them safe with one fsync. It trades a small risk for a large speed-up. If the program syncs every 100 records, then a crash can lose up to 99 records, which are up to 99 x 4 KiB = 396 KiB. If it syncs every 10, a crash can lose up to 9. The program can also sync by time, for example every 5 ms, which limits the loss in time and not in count.

A worked example. The table gives the measured rate from the first run, and the largest loss in a crash.

Policy Records per second Most records lost in a crash
fsync every record 7,262 0
fsync every 10 46,090 9
fsync every 100 84,963 99
sync only at the end 148,897 all of them

The target of 20,000 records per second is reached with a group of 10, and the loss is at most 9 records, which a log service may accept. A service that must never lose an acknowledged record, such as a payment system, must keep one fsync for each acknowledged request, or acknowledge a whole group together after one fsync. In that second design each request still waits for the sync of its group, so the request is safe when it is answered.

The key lines. The choice is not a technical one alone. The business decides how many records may be lost. The engineer then picks the cheapest policy that meets that limit and the speed goal.

Group commit

Group commit is the technique of making several operations durable with one synchronisation call, and acknowledging them together. It lowers the number of expensive synchronisations at the price of a short delay or of a small number of unsynced operations.

Example: a database that waits 2 ms to collect the commits of many clients and then calls fsync once for all of them.

Interview angle

A common question is "A service writes to disk and is slow. What do you do?" A good answer measures first (records per second, device writes, utilisation), finds that the program waits on fsync, proposes group commit, and states the durability trade-off in numbers.

Check yourself: 1. How many records can a crash lose with a sync every 100 records? 2. Which policy meets a target of 20,000 records per second? 3. How can a service combine group commit with acknowledging only safe records?