skip to content

A Redis cuckoo filter of unused coupon codes starts rejecting valid codes after weeks of CF.DEL calls: what went wrong, and how do you repair and prevent it?

level: seniorimportance: should knowfreq 26%

answer

  1. fingerprints, not items
  2. delete matches a short tag
  3. what did the delete really remove
  4. one delete per confirmed add

basics

~20 s

CF.DEL removes a matching fingerprint, not the item. Deleting a code that was never added, or deleting one twice, can erase another code's fingerprint, causing false negatives. Rebuild the filter from the database and delete only after it confirms the code.

solid answer

~50 s

A cuckoo filter stores an 8-bit fingerprint per item, and `CF.DEL` removes the first matching fingerprint in the item's two candidate buckets. It cannot tell whose fingerprint that is. So `CF.DEL` on a code that was never added, or a retry of a delete that already worked, can erase a live code's entry, and that code then gets `CF.EXISTS` replying `0`: a false negative, which the docs warn about. Even a reply of `1` does not prove the code was present. Since nothing records which entries were lost, the repair is to rebuild from the database into a new key and swap it in. To prevent it, make the database transition from unused to used the gate: only the request that wins it calls `CF.DEL`, once, for a code known to be in the filter.

code

plaintext · 14 lines
plaintext
> CF.INSERT cf ITEMS item1 item2 item2
1) (integer) 1
2) (integer) 1
3) (integer) 1
> CF.DEL cf item1
(integer) 1
> CF.DEL cf item1
(integer) 0
> CF.DEL cf item2
(integer) 1
> CF.DEL cf item2
(integer) 1
> CF.DEL cf item2
(integer) 0

go deeper

for a junior

Recall that a cuckoo filter allows deletes through CF.DEL, stores short fingerprints rather than items, and that the docs warn against deleting anything you did not definitely add.

for a middle

Explain why CF.DEL can hit the wrong item: it matches a fingerprint in two candidate buckets. Know that CF.ADD stores duplicates, and that CF.ADDNX can decline to store on a false positive.

for a senior

Trace the false negative from a retry or an unchecked delete to a rejected customer, sample known-live codes to prove it, rebuild from the database, and gate each delete on an atomic database transition.

for a principal

Weigh whether deletes justify the risk at all. A filter that needs a database gate on every delete may offer little over an exact set, and a Bloom filter rebuilt on a schedule can be the simpler standard.

## What the filter actually stores A Redis **cuckoo filter** is an array of buckets holding short **fingerprints**, not items. In the RedisBloom source a fingerprint is one byte. Each item has two candidate buckets, each with `BUCKETSIZE` slots, 2 by default. Different items can therefore share a fingerprint and a bucket, which is where false positives come from. `CF.DEL` looks in those two candidate buckets, newest sub-filter first, and clears the first slot holding a matching fingerprint. It has no way of knowing which item put it there. ## How deletes create false negatives The command page states the rule: never delete an item unless you are certain you added it, since doing otherwise may corrupt the filter and cause false negatives. Common ways to break it: 1. **Deleting unchecked input.** A handler calls `CF.DEL` on whatever code a customer typed. A garbage code that matches a live code's fingerprint erases that code. 2. **Retrying a delete.** A client times out and retries after the first call succeeded. The code's own entry has gone, so the retry can hit another code's entry. 3. **Double redemption.** Two concurrent requests for one code both call `CF.DEL`, with the same effect as a retry. 4. **Pairing `CF.ADDNX` with a delete.** `CF.ADDNX` replies `0` and stores nothing if the fingerprint already looks present, which can be a false positive. The later `CF.DEL` for that code removes the other code's entry. A delete of a never-added item erases something in exactly the case where `CF.EXISTS` would have given a false positive. By the docs' formula, the default bucket size of 2 gives about 1.56%. So a million unchecked deletes against a well-filled filter can erase on the order of 15,000 live codes. ## Why the reply does not help `CF.DEL` replies `1` when it removed a fingerprint and `0` when it found none. A `1` for a code you never added looks like success, while another customer's coupon has just gone. Checking `CF.EXISTS` first does not help either: it reads the same fingerprints, so it also replies `1` in precisely the dangerous case. In the coupon flow a `0` means invalid with no database check, so a false negative reaches the customer directly: the code is refused, and no log shows why. ## Diagnosing it - **Test known-live codes.** Take a sample of unused codes from the database and call `CF.EXISTS`. A `0` for an unredeemed code points to a bad delete, an add that failed unnoticed, or loss of the key. - **Compare the counts.** `Number of items inserted` in `CF.INFO` is the current count, falling with each delete. Fewer than the database's unused codes means too many deletes, though duplicates can mask it. `Number of items deleted` is not a lifetime total: the source resets it on compaction. - **Read the handler.** Look for a `CF.DEL` that runs before, or without, the database check. ## Repairing and preventing it The filter holds no list of what it lost, so nothing inside it can be repaired. Reserve a new key, add every unused code from the database, write new codes to both keys during the rebuild, then `RENAME` the new key over the old one. The prevention follows the flow the docs give for coupons: a `0` means invalid, a `1` means check the main database, and the code is removed from the filter only once the database says it was valid. - Make the database update from unused to used atomic, and call `CF.DEL` only in the request that won it. - Do not retry a `CF.DEL` blindly. A missed delete leaves a stale yes, which the database check catches. A repeated delete leaves a false no, which nothing catches. - Add with `CF.ADD` and pair each add with one delete. Do not issue a delete for a code whose `CF.ADDNX` replied `0`. ## The capacity side | Fact | Consequence | |---|---| | `CF.ADD` stores a duplicate each time | Replays fill buckets, and one delete leaves the other copies | | `CF.RESERVE` has no rate argument | Floor about 0.78% at `BUCKETSIZE 1`, about 1.56% at the default 2 | | The filter can declare itself full before `capacity` | Reserve spare capacity, or it expands | | Expansions are limited by `cf-max-expansions`, default 32 | Past it, adds fail with `Maximum expansions reached` |

  • If one code was added twice by a replayed job, what does a single CF.DEL leave behind?
    One copy remains, so CF.EXISTS keeps replying 1 and CF.COUNT reports about 1. CF.ADD treats each call as a separate entry. In the coupon flow that stale yes is caught by the database check, so it costs a database read rather than a wrong answer, but the duplicates still use up slots.
  • Would an EXPANSION setting have prevented these false negatives?
    No. EXPANSION only sizes the sub-filters added when a filter declares itself full. CF.DEL checks each sub-filter, newest first, and clears the first match, so more sub-filters give it no way of telling whose fingerprint it found. The docs also say sub-filters raise the false-positive rate, which makes such a match more likely.
  • When would you drop the cuckoo filter and use something exact?
    When the database is checked on every yes anyway and most lookups are real codes, the filter saves little. If codes are few enough to hold exactly, a Redis Set gives SREM with no fingerprint risk. Keep the filter only when most lookups are misses and memory matters.

Think of a cloakroom whose ledger records only a two-digit tag for each coat. Hand in a forged ticket with a matching tag and the attendant crosses out a line: the reply says done, yet the entry belonged to someone else, whose coat now looks as if it was never left.

  • CF.DEL replying 1 proves that the named item had been added.
  • Cuckoo filters support deletes, so calling CF.DEL on any input is safe.
  • Checking that CF.EXISTS replies 1 makes the following CF.DEL safe.
  • A cuckoo filter can never give a false negative.
  • Adding an item twice with CF.ADD stores it once, so one CF.DEL clears it.
  • After a bad delete, the filter can tell you which codes it has lost.