๐ก 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.
- Someone asks if Zed came. The doorkeeper looks at the lamp wall, and it says Zed surely never came. Nobody walks to the cellar.
- Someone asks about Ana. The wall says maybe, so the doorkeeper walks down and finds Ana in the guest book.
- 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.
- A "surely not" is always right. A "maybe" is sometimes wrong. The tiny wall still saves most trips to the cellar.
- The party starts. All eight lamps are off, and the doorkeeper has three counting recipes ready.
- Ana arrives. The three recipes turn her name into lamps 1, 4 and 6, so those three lamps are switched on.
- Ben arrives. His recipes give lamps 2, 4 and 7. Lamp 4 is already on from Ana, so it just stays on.
- The wall never writes down a name. It only remembers which lamps are on, and lamps are never switched off.
- Ana and Ben have come in, so lamps 1, 2, 4, 6 and 7 are on. Nobody else has arrived.
- Did Zed come? His recipes give lamps 0, 4 and 6. Lamp 0 is off, so Zed surely never came.
- Did Ana come? Lamps 1, 4 and 6 are all on, so the answer is maybe. And she really did come.
- 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.
- With just Ana and Ben inside, five of the eight lamps are already on. This little wall is filling up fast.
- Dan arrives and his recipes give lamps 0, 3 and 5. Now every single lamp on the wall is on.
- 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.
- 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.
- 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.
- The party keeps growing. Three thousand guests switch on lamps, three times what the wall was built for. Most lamps are now on.
- 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.
- 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.
- 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.
- 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.
- 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.
- Box 3's lamps say maybe. The doorkeeper walks down, opens only box 3, and finds card 42.
- 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.