What is taught wrongly

When the stream takes it back

Count-Min's estimate is never below the truth. That is a theorem about a stream where every update adds — and allow deletions that can take a count below zero and it comes back under on 93% of queries, with nothing in the number to say so.

The property a Count-Min sketch is built around is that its answer is never too small. Not usually, not with high probability — never. It is a consequence of arithmetic rather than of chance: every cell is a sum of counts, all of them non-negative, and the cell a key lands in contains that key’s own count plus a non-negative pile of other keys’. The minimum over rows is the smallest such pile, and it is still a pile.

Here is the same structure on a stream of 60,000 updates over 2,048 keys where half the updates are decrements.

Signed error on 200 keys, in the general turnstile modelEach mark is one key's estimate minus its exact count, so a mark left of the line is an estimate BELOW the truth. The stream is 60,000 updates over 2,048 keys, insertions and deletions, counts may go negative. Count-Min 4×64 holds 8,192 bits and came back under on 184 of 200 keys, worst under -161 and worst over 13; Count-Sketch 4×64 holds 8,192 bits and came back under on 99 of 200 keys, worst under -54 and worst over 39. Count-Min's one-sidedness is a consequence of every cell being a sum of non-negative counts; it survives deletions exactly as long as no count goes below zero, which is a condition on the stream that the structure cannot check and its bound never mentions.Count-Min 4×64exact184 under16 overrms 42Count-Sketch 4×64-174-8708717499 under101 overrms 13general turnstile · insertions and deletions, counts may go negative · counts exact8,192 bits each
Fig. 1 Every mark is a key’s estimate minus its true count, and a mark left of the line is an estimate below the truth. Count-Min is on the wrong side for 184 of these 200 keys and by as much as 161 counts. Nothing about the arithmetic changed; the stream did.

Three models, and only one of them is usually named

The streaming literature has names for these and they are worth learning, because almost every published accuracy figure belongs to one of them and says which about as often as a benchmark says which machine.

Cash register. Every update adds. The frequency vector only ever grows. This is the model of every measurement in the previous phase of this field and of every one in the three essays before this.

Strict turnstile. Updates may be negative, but no key’s count is ever allowed to go below zero. A count of items currently in a set, or of sessions currently open, is in this model: things are removed only after having been added.

General turnstile. Updates may be negative and a count may end up negative. Streams of differences live here — the change in a metric between two feeds, a set of adjustments applied in whatever order they arrived, a reconciliation between two replicas.

The distinction between the second and the third is the one this essay turns on, and it is the one most often skipped. Count-Min’s one-sidedness holds in the strict turnstile exactly as it does in the cash register, and fails in the general one.

Signed error on 200 keys, in the strict turnstile modelEach mark is one key's estimate minus its exact count, so a mark left of the line is an estimate BELOW the truth. The stream is 60,000 updates over 2,048 keys, insertions and deletions, no count below zero. Count-Min 4×64 holds 8,192 bits and came back under on 0 of 200 keys, worst under 0 and worst over 100; Count-Sketch 4×64 holds 8,192 bits and came back under on 108 of 200 keys, worst under -184 and worst over 1,754. Count-Min's one-sidedness is a consequence of every cell being a sum of non-negative counts; it survives deletions exactly as long as no count goes below zero, which is a condition on the stream that the structure cannot check and its bound never mentions.Count-Min 4×64exact0 under200 overrms 15Count-Sketch 4×64-1,894-94709471,894108 under92 overrms 179strict turnstile · insertions and deletions, no count below zero · counts exact8,192 bits each
Fig. 2 The strict turnstile: 60,000 updates of which 30% are deletions, each drawn from the keys currently positive so that nothing ever goes below zero. Count-Min is above the truth on every one of the 200 keys, and its theorem covers this model without modification.

Why the proof survives one and not the other

The proof is three lines and each line is worth setting out, because which line breaks is the whole content.

A cell holds y:h(y)=cfy\sum_{y : h(y) = c} f_y. The queried key xx is one of those yy, so the cell is fx+yx,h(y)=cfyf_x + \sum_{y \neq x, h(y)=c} f_y. If every fy0f_y \ge 0 then that second term is at least zero, so the cell is at least fxf_x, so the minimum over rows is at least fxf_x.

The condition is on the frequency vector at query time, not on the updates. A strict turnstile stream can subtract as much as it likes; every fyf_y is still non-negative when the query arrives, the second term is still a sum of non-negative numbers, and the estimate is still an upper bound. A general turnstile stream can leave some fyf_y negative, the second term can be negative, and the guarantee is gone.

The structure cannot check the condition. A Count-Min sketch has no way to know whether some key’s count went negative — that is exactly the sort of per-key fact it does not store. It will run, it will answer, and its answers will be below the truth without any indication that the model has changed.

Measured

Over all 1,988 keys with a nonzero count, on a general turnstile stream with half the updates negative:

structure bits queries below the truth worst below rms error
Count-Min, 4×64 8,192 1,805 of 1,988 (91%) −161 42.5
Count-Sketch, 4×64 8,192 99 of 200 (50%) −54 12.6

Two things in that table and the second is the sharper.

The one-sided structure is under the truth nine times in ten, which is not a degradation of the guarantee but its inversion: an estimate one could previously treat as an upper bound is now, in practice, a lower bound most of the time. And the structure that never promised a direction is both closer to the truth — a third of the error — and honest about which way it is wrong, because being wrong in both directions was always what it said it did.

Which structure is valid in which model, and what the violation costsA filled cell is a structure that declares itself valid in that model. An outlined cell is one that will run there without complaint and whose guarantee says nothing about it — and the number in it is what that costs, measured on a general turnstile stream of 40,000 updates over 2,048 keys with a deletion rate of 0.5, on which 796 keys end with a negative count. Count-Min came back BELOW the true count on 1,758 of 1,895 keys, which its own theorem forbids and which nothing in the returned number reveals; Count-Sketch, which promises nothing about the direction of its error, came back below on 48% of them, as it is designed to. The models differ in four dials: whether an update may be negative, whether a count may end negative, whether items expire, and how many passes are allowed.Count-Min8,192 bitsCount-Sketch8,192 bitstug-of-war, F22,560 bitsGreenwald–Khanna0 bitsexponential histogram0 bitscash register+strict turnstile±general turnstile± may go negativesliding window+ expiresdeclareddeclared93% underdeclareddeclareddeclareddeclareddeclareddeclareddeclareddeclared40,000 updates · deletion rate 0.5 · every count exact1,758 of 1,895 under
Fig. 3 The declaration made machinery. A filled cell is a structure that says it is valid in that model; a dashed one is a structure that will run there without complaint. The number in the dashed cell is the measured share of queries below the truth, which is what running there costs.

More state does not help

The natural response to a structure being wrong is to give it more room. Here is what that buys.

columns ww bits queries below the truth
16 2,048 1,791 of 1,988 (90.1%)
32 4,096 1,821 of 1,988 (91.6%)
64 8,192 1,805 of 1,988 (90.8%)
128 16,384 1,838 of 1,988 (92.5%)
256 32,768 1,817 of 1,988 (91.4%)
512 65,536 1,816 of 1,988 (91.3%)

Thirty-two times the state and the share does not move. If anything it rises slightly, and the reason is worth following: a wider table means fewer keys per cell, so the positive mass that used to mask a negative neighbour is no longer there. The extra state removes the noise that was concealing the failure.

That is a genuinely uncomfortable shape. The usual relationship between state and quality — more bits, better answers — is intact for the magnitude of the error, which does fall. It is inverted for the property, which was never a matter of accuracy at all.

Before that list, one thing worth settling: whether a system is in the third model is a question about its sources rather than about its code, and it is therefore a question nobody looking at the sketch can answer. The updates arrive already signed, the structure adds them, and whether some key’s total is allowed below zero depends on whether every subtraction is guaranteed to follow an addition of the same key — a guarantee that lives in whichever service produced the stream, is rarely written down, and is exactly the kind of property that holds until a retry, a reordering or a second producer arrives. That is why the four situations below are worth enumerating: each is a perfectly ordinary way for that guarantee to lapse without anybody deciding to lapse it.

Where general turnstile streams come from

It would be convenient if the third model were exotic. Four places it is not:

Differences between two feeds. Subscribe to two sources and sketch the difference: additions from one, subtractions from the other. Nothing constrains the result to be non-negative and the whole point is to find where the two disagree.

Adjustments applied out of order. A distributed system that emits corrections — “that event was double-counted, subtract two” — can deliver the correction before the event, and a sketch that processes them as they arrive is in the general model for as long as the reordering lasts.

Windows implemented by subtraction. The obvious way to maintain a summary of the last hour is to add each arrival and subtract it again an hour later. If the subtraction is driven by a separate timer or a separate feed, a key whose insertion was missed still gets its deletion, and the count goes negative.

Reconciliation. Two replicas exchange sketches to find where they differ. The difference of two frequency vectors is a general turnstile object almost by definition.

Each of those is a normal engineering situation and none of them looks, at the point of writing the code, like leaving a model behind.

Signed error on 150 keys, in the general turnstile modelEach mark is one key's estimate minus its exact count, so a mark left of the line is an estimate BELOW the truth. The stream is 60,000 updates over 2,048 keys, insertions and deletions, counts may go negative. Count-Min 4×64 holds 8,192 bits and came back under on 0 of 150 keys, worst under 0 and worst over 252; Count-Sketch 4×64 holds 8,192 bits and came back under on 79 of 150 keys, worst under -354 and worst over 1,031. Count-Min's one-sidedness is a consequence of every cell being a sum of non-negative counts; it survives deletions exactly as long as no count goes below zero, which is a condition on the stream that the structure cannot check and its bound never mentions.Count-Min 4×64exact0 under150 overrms 122Count-Sketch 4×64-1,113-55705571,11379 under71 overrms 114general turnstile · insertions and deletions, counts may go negative · counts exact8,192 bits each
Fig. 4 The same general turnstile stream with a gentler deletion rate — 30% rather than 50%. Only a fifth of the keys end up negative and the top keys stay comfortably positive, so Count-Min is above the truth on every one of the 150 heaviest keys drawn here. The model is violated and the symptom is absent, which is the failure mode this essay exists to name.

The strict model is where most systems actually are

It is worth dwelling on the middle case, because a reader who has followed this far may conclude that deletions are dangerous and they mostly are not.

A count of things currently present — sessions open, items in a cart, rows in a partition, files in a directory — is a strict turnstile quantity by construction: nothing is removed that was not first added, so no count can go below zero, and Count-Min’s theorem applies without a word of modification. The measurement above bears that out at every deletion rate the generator can produce while holding its own condition.

deletion rate model gross updates net L1L_1 queries below the truth
30%, strict strict turnstile 60,000 23,868 0 of 200
30%, general general turnstile 60,000 24,540 0 of 200
50%, general general turnstile 60,000 5,308 184 of 200

The middle row is the one to look at twice. That stream is in the general model — 216 of its keys end with a negative count — and Count-Min was above the truth on all 200 keys queried. The guarantee did not hold; it merely was not observed to fail, because the keys queried were the heavy ones and the heavy ones stayed comfortably positive.

So the symptom is a function of which keys are asked about, and the guarantee is a function of the model. Those are different things, and a test that queries the top hundred keys will find a violated model perfectly healthy for as long as the violation stays in the tail.

Signed error on 50 keys, in the strict turnstile modelEach mark is one key's estimate minus its exact count, so a mark left of the line is an estimate BELOW the truth. The stream is 60,000 updates over 2,048 keys, insertions and deletions, no count below zero. Count-Min 4×64 holds 8,192 bits and came back under on 0 of 50 keys, worst under 0 and worst over 2; Count-Sketch 4×64 holds 8,192 bits and came back under on 17 of 50 keys, worst under -1 and worst over 1,215. Count-Min's one-sidedness is a consequence of every cell being a sum of non-negative counts; it survives deletions exactly as long as no count goes below zero, which is a condition on the stream that the structure cannot check and its bound never mentions.Count-Min 4×64exact0 under50 overrms 0Count-Sketch 4×64-1,312-65606561,31217 under33 overrms 172strict turnstile · insertions and deletions, no count below zero · counts exact8,192 bits each
Fig. 5 A strict turnstile with 40% of its updates deletions — more churn, and the same picture as the 30% one. Nothing about the rate of deletion matters to the theorem; what matters is whether any count is allowed below zero, which is a property of how the deletions are chosen rather than how many there are.

The distinction between refusing and warning is worth one sentence of its own, because it is the difference between a check that runs and a check that gets ignored.

The machinery, and why it refuses rather than warns

The library behind these figures will not run a summary outside the model it declares. runUnder takes a structure, a sequence and a model name, and it throws twice: if the structure does not declare itself valid in that model, and — the more useful half — if the sequence is not actually in the model it was offered as.

The second check is computed from the updates rather than declared alongside them. A sequence generated with deletions and labelled cash-register is caught before anything is measured, which is a mistake nothing else here would notice: the estimates come back, they look reasonable, and the number on the plate is a measurement of something that was never claimed.

Every figure in this essay was drawn from a run that violated its model deliberately, and each one says so. That is the family’s own rule — { anyway: true } returns the violation rather than swallowing it, and the plate prints it — and it exists because a plate that quietly rendered an out-of-model run would be committing the exact error the essay is about.

This is the same device as the one-pass wrapper and the table that throws on a cell read before it is written: the model’s defining restriction, enforced by the machinery instead of remembered by the author.

What is left when the direction goes

Count-Min in the general turnstile is not useless, and saying what remains is more useful than saying what is lost.

The magnitude bound survives, in the right norm. The error is still bounded by εf1\varepsilon \lVert f \rVert_1 of the net vector, with ε=e/w\varepsilon = e/w — it is now two-sided, that is all. The usual repair is to take the median of the dd cells rather than the minimum, which is unbiased in this model and gives a bound with the same shape. At which point the structure is Count-Sketch with worse signs, which is a fair summary of the situation.

Linearity survives everything. The table is the sum of the updates, so it can be merged, subtracted, and reconciled in the general model exactly as in the cash register. That property is what makes turnstile streams tractable at all, and it is the subject of the strangest measurement in this phase, which is in the next paragraph but one.

Nothing else does. The upper-bound reading, the confident filtering, the ability to say “at most”, the composition arguments — those were all consequences of the direction, and the direction is what went.

The bound is quoted against the wrong number

One more measurement, and it is about a mistake the sketch does not make and its user easily does.

Build a stream of 19,027 updates in which nearly everything is added and then taken back: the net vector has an L1L_1 norm of 973 and the gross traffic is 19,027, so 94.9% of what went past has cancelled. Now build the same net vector directly, in 236 updates, one per key.

The two Count-Sketch tables are identical, cell for cell. Not close — identical, because a linear sketch’s state is the sum of its updates and the two sequences have the same sum. Every estimate is the same, every error is the same, and the structure has no way of knowing which sequence it saw.

The bound is εf1\varepsilon \lVert f \rVert_1, and f1\lVert f \rVert_1 means the norm of the net vector, 973. Quote it instead against the number of updates — the natural reading of “the error is ε\varepsilon times the length of the stream” — and the promised error is 19.6 times too large. On this stream the guarantee would be stated as ±808 when the structure’s actual guarantee is ±41.

The direction of that error is the merciful one: the quoted bound is too pessimistic rather than too optimistic. But it is a bound nobody can use, and it comes from the same habit as everything else in this essay — repeating a guarantee without its subject.

Signed error on 100 keys, in the general turnstile modelEach mark is one key's estimate minus its exact count, so a mark left of the line is an estimate BELOW the truth. The stream is 60,000 updates over 2,048 keys, every key added and mostly taken back. Count-Min 4×64 holds 8,192 bits and came back under on 0 of 100 keys, worst under 0 and worst over 24; Count-Sketch 4×64 holds 8,192 bits and came back under on 55 of 100 keys, worst under -17 and worst over 94. Count-Min's one-sidedness is a consequence of every cell being a sum of non-negative counts; it survives deletions exactly as long as no count goes below zero, which is a condition on the stream that the structure cannot check and its bound never mentions.Count-Min 4×64exact0 under100 overrms 14Count-Sketch 4×64-101-5005010155 under45 overrms 11general turnstile · every key added and mostly taken back · counts exact8,192 bits each
Fig. 6 The cancelling stream itself, queried. The errors are small because the surviving vector is small: an error that is a fraction of what is left is a fraction of very little. The sketch is behaving exactly as its bound says and the bound is about a quantity 19.6 times smaller than the stream that produced it.

The repair that keeps the direction, and what it costs

There is a construction that recovers a one-sided reading in the general turnstile, it needs no new theory, and its price turns out to be the quantity the previous section is about — which makes the two halves of this essay one thing rather than two.

Split the stream. Send every positive update to one Count-Min sketch and the absolute value of every negative update to a second. Each of those two streams is a cash-register stream by construction, so each sketch is exactly in the model its theorem was proved in, and each returns an over-estimate of its own half:

P^xfx+,N^xfx\hat{P}_x \ge f^+_x, \qquad \hat{N}_x \ge f^-_x

The estimate for the net count is P^xN^x\hat{P}_x - \hat{N}_x. It is no longer one-sided — one over-estimate minus another can land either way — but it now has a bound again, and the bound is the sum of the two: ε(f+1+f1)\varepsilon(\lVert f^+\rVert_1 + \lVert f^-\rVert_1).

That norm is the gross traffic, not the net vector. On the cancelling stream measured above, the gross is 19,027 and the net is 973, so the repaired structure’s guarantee is 19.6 times weaker than the guarantee the single sketch has in the cash-register model — which is precisely the factor the last section identifies as the difference between quoting a bound against the stream and quoting it against the vector.

So the two findings are the same finding. A structure that keeps the positive and negative parts apart pays exactly the cancellation ratio, and a structure that adds them keeps the tight bound and loses the direction. Nothing recovers both, and the arithmetic says why: the direction is a statement about a sum of non-negative terms, and cancellation is what makes the sum stop being one.

Two things do survive the split and are worth having.

An upper bound on the positive part. P^x\hat{P}_x is a genuine one-sided over-estimate of how much was ever added for key xx, whatever the deletions did. For a question like did this key ever exceed a threshold — which is what a great many alerting rules actually ask — that is the right quantity and the single merged sketch cannot supply it.

Twice the state. Two tables where there was one, which is the honest price beside the weaker bound, and which is why the split is a decision rather than a default.

The rule this leaves is narrow enough to apply mechanically: if the deletions are strict, use one sketch and keep the theorem; if they are not, either accept a two-sided estimate from one sketch or pay two sketches and the cancellation ratio for a bound whose subject is stated. What is not available is the reading everybody actually wants — a tight one-sided bound on a vector that cancels — and the arithmetic above is a short proof that it is not an implementation problem.

What to take from it

The lesson is not that Count-Min is fragile. Its theorem is true, its proof is three lines, and both are unchanged by anything measured here.

The lesson is that a guarantee has a subject, and the subject is a model — a statement about what the input is allowed to do. The four dials this phase separates are whether updates may be negative, whether counts may end negative, whether items expire, and how independent the hashes are; the previous phase’s every measurement had all four set one way and none of its plates said so.

Absolute over-count against a key's true count, 4×64 sketchThe same run, drawn as the absolute over-count rather than the relative one. The dashed line is the additive bound e/w × N = 2,548, and nothing is above it. This is the plot the guarantee is about, and it is flat: the error a key suffers has nothing to do with how often that key occurred. Every claim of the form "accurate to within a per cent" is a claim about this axis divided by the other one.1101001,00010,00010³true count of the keyover-counte/w × N = 2,548heaviest keyrarest key4×64 counters · 8,192 bits · Zipf s = 1.1 · 2,004 distinct keys at 1,035 positionsbound 2,548, worst 1,262
Fig. 7 The cash-register picture one last time, for comparison: Count-Min’s error against the true frequency, all of it above the line. Everything in this essay is what happens to this plot when the stream is allowed to subtract.

A structure cannot check its own model and a bound cannot carry one. What can is a declaration, checked against the data, at the point the two meet. That is what this field’s machinery now does, and this essay is what it was built for.

What this makes readable

Essays that name this one as a prerequisite.

Named alongside this one

Essays reaching for the same objects. Nobody chose these; they are what the concept index makes visible.

What links here

Every essay whose body links to this one.

The objects this essay names

Each one links to every other essay that touches it.

Additive errorCancellationCount-Min sketchCount-SketchFailure modeFrequency vectorGuaranteeHonest limitLinear sketchOne-sided errorPreconditionStream modelStreaming algorithmTurnstile model