Learn
Lesson · Caching and Hit Rates · Where caches fail

Write Policies

Write-through sends every write to the backend and keeps nothing at risk. Write-back holds writes in the cache and sends the distinct keys once per flush, which is a coalescing ratio on any trace with repeats. What it buys is fewer backend writes; what it costs is a loss window, and both come off the same trace.

write-through backend writes = every write write-back backend writes = distinct keys per flush window trace A B A C A B A D A B, flushed every 5 writes 10 writes → 6 distinct ratio 5/3 1 000 → 600 writes/s largest dirty set 3 keys × 4 kB = 12.00 kB at risk over 5.0 ms
Two policies, one trace, and the price of the better ratio

Core ideas

What this lesson establishes

One rule each, and then a number that only exists for the second of them: what is lost if the process dies.

1

Write-through sends every write

The write goes to the backend and to the cache, and it is not acknowledged until the backend has it. Backend write rate equals the application write rate exactly. Nothing is ever only in the cache, so a crash loses nothing and the loss window is zero.

2

Write-back sends the distinct keys per flush

The write goes to the cache and is acknowledged; the cache flushes dirty entries to the backend periodically. Repeated writes to the same key inside one flush window collapse into one backend write, so the backend sees the distinct keys per window. On A B A C A B A D A B flushed every five writes, that is 6 backend writes for 10 application writes.

3

The ratio is bought with a loss window

Whatever is dirty when the process dies is gone. The right measure is the largest dirty set a flush interval ever holds, not the average: an average loss window is not what a crash takes. On that trace it is 3 keys, which at 4 kB each is 12.00 kB, held for the 5.0 ms a flush interval lasts at 1 000 writes a second.

The material

Two policies, the coalescing ratio, and what is at risk

The rules, the counting, and why the trace decides whether write-back is worth anything at all.

Definition · Write-through and write-back

Under write-through, every write is applied to the cache and to the backend before it is acknowledged. Under write-back, a write is applied to the cache, marked dirty and acknowledged; dirty entries are written to the backend on a periodic flush.

The coalescing ratio of a trace under write-back is the number of writes divided by the number of distinct keys written, counted per flush window. It is 1 when every write in a window is to a different key, and it is the window length when they are all to the same key.

Both policies leave the cache correct. The difference is entirely about the backend: how many writes it receives, how quickly it learns about them, and what happens to the ones it has not learned about yet.

Counting the ratio off a trace

Take the write trace A B A C A B A D A B and flush every five writes. The first window is A B A C A: five writes, three distinct keys, so two writes to A are coalesced away. The second is B A D A B: five writes, three distinct keys again. Ten writes, six backend writes, a ratio of 5/3.

trace: A B A C A | B A D A B flush every 5 writes window 1 A B A C A 5 writes → 3 distinct {A, B, C} window 2 B A D A B 5 writes → 3 distinct {A, B, D} write-through 10 backend writes at 1 000 w/s → 1 000 w/s write-back 6 backend writes at 1 000 w/s → 600 w/s ratio 10/6 = 5/3 largest dirty set: 3 keys × 4 kB = 12.00 kB, held for 5.0 ms

Widen the flush window to ten writes and the whole trace becomes one window: four distinct keys, a ratio of 5/2, and the backend sees 400 writes a second instead of 600. The dirty set grows to 4 keys and 16.00 kB, held for 10.0 ms. That is the trade in its entirety, and both ends of it moved together.

What the trace decides

The ratio is a property of the workload, not of the policy. A trace in which every write is to a different key inside its window — A B C D E F G H I J — coalesces nothing at all: write-back sends ten writes, exactly as write-through does, and still carries the loss window. A trace of one key repeated ten times coalesces to one per window and buys a ratio of five.

So “write-back is faster” is not a general claim. It is a claim about a workload with repeated writes to the same keys inside a flush interval, and if you do not have that, write-back is a loss window with no compensating gain. Run your own trace before choosing.

What is actually at risk

The bytes at risk are the largest dirty set times the object size, and the exposure lasts a flush interval — w/λ_w seconds, for a window of w writes at λ_w writes a second. At 1 000 writes a second and a five-write window that is 5.0 ms, which sounds negligible until you multiply it by the number of processes you run and the number of times a year each of them dies.

The honest statement of the trade is arithmetic on both numbers at once: a flush window k times wider improves the ratio by at most k and grows both the dirty set and the exposure time. Whether the trade is acceptable is a durability question, and Availability and Failure is where losing things gets priced.

Interactive

Write policies

The coalescing ratio, and the loss window it is bought with

Write-through against write-backevery write, or the distinct keys per flush — and what that risks
a writea repeat, coalesced awaya flushbytes at risk

Method

Choosing a write policy from a trace

Count the distinct keys per window first. If the ratio is one, there is nothing to discuss.

1

Take a real write trace and a flush interval

A sequence of written keys in order, and how many writes a flush covers. The flush interval is usually configured in time; convert it with the write rate, since w = λ_w · flush seconds.

2

Count distinct keys per window

Not distinct keys overall: per window. A key written in two different windows is two backend writes. This is the only counting in the lesson and it is where the ratio comes from.

3

Compute the ratio and the backend write rate

writes ÷ distinct is the ratio, and λ_w × (distinct/writes) is what the backend actually receives. On this trace, 1 000 writes a second becomes 600.

4

Compute the bytes at risk, using the largest dirty set

Peak, not mean: a crash takes whatever was dirty at that instant, and the instant is not chosen kindly. Multiply by the object size and state the exposure as bytes over a flush interval.

Ratios, rates and loss windows

Score 0 / 0

A B A C A B A D A B, flushed every five writes

Ten writes, two windows, and then the same trace at a wider flush so that both ends of the trade move where you can see them.

trace: A B A C A B A D A B 1 000 writes/s, 4 kB objects flush every 5 writes: window 1 A B A C A 5 writes, distinct {A,B,C} = 3 window 2 B A D A B 5 writes, distinct {A,B,D} = 3 10 writes, total = 6 write-through 10 backend writes 1 000 writes/s write-back 6 backend writes 600 writes/s coalescing ratio 10/6 = 5/3 at risk peak dirty set 3 keys × 4 kB = 12.00 kB over 5.0 ms flush every 10 writes (one window): distinct {A,B,C,D} = 4 ratio 10/4 = 5/2 400 writes/s at risk 4 keys × 4 kB = 16.00 kB over 10.0 ms

Doubling the flush window improved the ratio from 5/3 to 5/2 — a further 200 writes a second off the backend — and took the exposure from 12.00 kB over 5.0 ms to 16.00 kB over 10.0 ms. Both numbers moved, and a proposal that mentions only the first is not a proposal.

For a faded rehearsal, run A B C D E F G H I J at a flush of five. The supplied first move is to count the distinct keys in the first window before computing anything; do that, then state the ratio, the backend write rate and the bytes at risk, and say in one sentence what write-back has bought on this trace.

The lab marks every repeated write inside a window as coalesced away and counts the rest, so you can see which writes the ratio is made of. Try the one-hot-key trace and the all-distinct trace before deciding what you believe about write-back.

Common mistakes

1
Believing write-back is freeIt costs a loss window: whatever is dirty when the process dies is gone. On the trace above that is 12.00 kB over 5.0 ms — small, until it is multiplied by a fleet. A proposal for write-back that does not name the peak dirty set and the exposure time has priced only the half it likes.
2
Counting distinct keys over the whole traceThe coalescing is per flush window. A B A C A B A D A B has four distinct keys overall and six backend writes at a five-write flush, because a key written in two windows is written to the backend twice. Counting globally overstates the ratio by whatever the flush interval would have been.
3
Assuming a coalescing ratio the workload does not haveOn A B C D E F G H I J the ratio is 1: write-back sends every write, exactly as write-through does, and still carries the loss window. The ratio is a property of the trace, so measure it on yours before choosing the policy that depends on it.

Check your understanding

✓
Finish when you would not accept a write-back proposal without a peak dirty set in it.

You should be able to count distinct keys per flush window off a trace, convert that into a coalescing ratio and a backend write rate, compute the bytes at risk from the peak dirty set and the exposure from the flush interval, and say what a wider flush does to all four numbers.

Note: The next two lessons are about caches that are not one cache. The first is a second level behind the first, where the rate you measure is not the rate you want: “Multi-level Caches” shows why the two miss rates multiply rather than add.

Kept in this browser only, for you. Nothing is sent anywhere and nothing checks it. Carry it to another device.

Feedback and recommendations

Something to change about this lesson? Write it down, tick it off when it is handled, and export the list when you want to act on it. Kept in this browser only: nothing is sent anywhere, and nobody is notified.

No recommendations yet for this lesson.