TwiceToldby Develoker
ConceptsSystem designLabsCheat sheetsAbout

Develoker makes free tools for learning to build software. TwiceTold explains each idea twice, as an everyday analogy and the real thing; CloudArch lets you design cloud systems and put load on them.

About ยท Labs ยท Cheat sheets ยท Learning paths

โ† Back to Bloom filters

๐Ÿ’ก Bloom filters: cheat sheet

๐ŸŽจ The analogyA wall of lamps by a party door that guests switch on, so one look says who surely never came.
๐ŸŽ“ The regular explanationA compact probabilistic set that answers "is this element in the set?" with either "definitely not" or "maybe", using a bit array and k hash functions. It has false positives but never false negatives.
Definitely not, or maybeStep 4 of 4
๐Ÿง‘โ€๐Ÿ’ผDoorkeeper๐Ÿ’กLamp walla no is always right๐Ÿ“– Guest book (cellar)
  1. Someone asks if Zed came. The doorkeeper looks at the lamp wall, and it says Zed surely never came. Nobody walks to the cellar.
  2. Someone asks about Ana. The wall says maybe, so the doorkeeper walks down and finds Ana in the guest book.
  3. Someone asks about Cal. The wall says maybe, but Cal is not in the book. The walk was wasted, and that's all a wrong maybe ever costs.
  4. A "surely not" is always right. A "maybe" is sometimes wrong. The tiny wall still saves most trips to the cellar.
Adding sets k bitsStep 4 of 4
๐Ÿงฑ Lamp wall๐Ÿ‘ฉAna arrives๐Ÿ‘จBen arrives๐Ÿ’กLamp 0๐Ÿ’กLamp 1on๐Ÿ’กLamp 2on๐Ÿ’กLamp 3๐Ÿ’กLamp 4on, shared๐Ÿ’กLamp 5๐Ÿ’กLamp 6on๐Ÿ’กLamp 7on
  1. The party starts. All eight lamps are off, and the doorkeeper has three counting recipes ready.
  2. Ana arrives. The three recipes turn her name into lamps 1, 4 and 6, so those three lamps are switched on.
  3. Ben arrives. His recipes give lamps 2, 4 and 7. Lamp 4 is already on from Ana, so it just stays on.
  4. The wall never writes down a name. It only remembers which lamps are on, and lamps are never switched off.
Checking, and a false positiveStep 4 of 4
๐Ÿงฑ Lamp wall๐Ÿง”Did Zed come?definitely not๐Ÿ‘ฉDid Ana come?maybe (correct)๐Ÿง‘Did Cal come?false positive๐Ÿ’กLamp 0๐Ÿ’กLamp 1on๐Ÿ’กLamp 2on๐Ÿ’กLamp 3๐Ÿ’กLamp 4on๐Ÿ’กLamp 5๐Ÿ’กLamp 6on๐Ÿ’กLamp 7on
  1. Ana and Ben have come in, so lamps 1, 2, 4, 6 and 7 are on. Nobody else has arrived.
  2. Did Zed come? His recipes give lamps 0, 4 and 6. Lamp 0 is off, so Zed surely never came.
  3. Did Ana come? Lamps 1, 4 and 6 are all on, so the answer is maybe. And she really did come.
  4. Did Cal come? Lamps 1, 2 and 7 are all on, so the wall says maybe. But Cal never came. Ana and Ben lit those lamps between them.
A full filter says maybe to everythingStep 4 of 4
๐Ÿงฑ Lamp wallall on๐Ÿ‘ฆDan arrives๐Ÿ‘งDid Eve come?useless maybe๐Ÿ’กLamp 0on๐Ÿ’กLamp 1on๐Ÿ’กLamp 2on๐Ÿ’กLamp 3on๐Ÿ’กLamp 4on๐Ÿ’กLamp 5on๐Ÿ’กLamp 6on๐Ÿ’กLamp 7on๐ŸงฑBigger wallabout 1% wrong
  1. With just Ana and Ben inside, five of the eight lamps are already on. This little wall is filling up fast.
  2. Dan arrives and his recipes give lamps 0, 3 and 5. Now every single lamp on the wall is on.
  3. Did Eve come? Whatever lamps her recipes pick, they are all on, so the wall says maybe. A full wall says maybe to everyone and stops being useful.
  4. The fix is to plan ahead. A wall with about ten lamps for every expected guest gives a wrong maybe only about once in a hundred questions.
An overfilled filter sends every lookup to the slow storeStep 5 of 5
๐Ÿง‘โ€๐Ÿ’ผDoorkeeper๐Ÿ’ก Wall of lampsreplaced๐Ÿ“– Guest book in the cellar๐Ÿงฑ Bigger wallabout 1% wrong
  1. The wall was built for a thousand guests. Someone asks about a person who never came. A lamp is off, so the answer is surely not, and nobody walks to the cellar.
  2. The party keeps growing. Three thousand guests switch on lamps, three times what the wall was built for. Most lamps are now on.
  3. Another question about someone who never came. Their lamps happen to all be on, so the wall says maybe, and the doorkeeper has to go down to the cellar.
  4. The walks pile up, and the cellar gets slow. The wall stopped saving anything, and its mistakes can only be fixed by building a bigger one.
  5. The doorkeeper reads the guest book and builds a bigger wall with ten lamps per guest. Wrong maybes drop back to about one in a hundred. The cost was one full pass over the book.
Skipping disk reads in an LSM treeStep 4 of 4
๐Ÿชœ Top of the stairsrebuilt when merged๐Ÿ“ฆ The cellarCard box 1k07key key 7: k070k19key key 19: k1912Card box 2k23key key 23: k230k31key key 31: k3112Card box 3k42key key 42: k420k58key key 58: k5812๐Ÿง‘โ€๐Ÿ’ผDoorkeeper๐Ÿ’ก Lamps for box 1skipped๐Ÿ’ก Lamps for box 2skipped๐Ÿ’ก Lamps for box 3read 1 file
  1. The guest book has grown into three boxes of cards in the cellar. Each box has its own little lamp wall at the top of the stairs.
  2. The doorkeeper looks for card 42. Boxes 1 and 2 have a lamp off for it, so card 42 is surely not in them. Those boxes stay shut.
  3. Box 3's lamps say maybe. The doorkeeper walks down, opens only box 3, and finds card 42.
  4. Boxes are never changed, only replaced. When boxes are merged into a new one, it gets a brand new lamp wall to match.

๐Ÿงฉ Key questions

Someone asks about Zed, and one of Zed's lamps is off. What can the doorkeeper say?
Zed surely never came. If Zed had come, his recipes would have switched that lamp on, and lamps never go off.
Cal never came, but all three of Cal's lamps are on. What is that wrong "maybe" called?
A false positive. Other guests switched Cal's lamps on between them. It costs a wasted walk, but never a wrong "surely not".
The wall has far too many guests for its size. What happens?
Nearly every lamp is on, so nearly every answer is "maybe". Lamps only switch on. Plan about ten lamps per expected guest to keep wrong "maybe"s rare.
A late guest is written in the guest book, but nobody switches on their lamps. Later someone asks about them. What happens?
The wall says surely not, so a guest who really came is turned away. Lamps that were never switched on can only mean surely not. The wall must be updated every time the book is, or it breaks its one promise.