arthrop0d

Brief articles on varied topics.

Bloom Filters in Database Read Paths

databasesalgorithms

A Bloom filter is a compact, probabilistic data structure that answers a membership question: “Could this item be in this set?” It can definitely identify items that are absent, but it may occasionally say “possibly present” for an item that is not there.

That trade-off is useful in databases because checking a small in-memory bit array is much cheaper than reading a file, page, or remote storage object that cannot contain the requested row.

The problem: unnecessary reads

Many databases store data across multiple files or pages. A point lookup for user_id = 42 may need to ask which of those locations could contain that key, then read several candidates from disk or an object store.

Without a fast way to reject candidates, the database might do this:

  1. Open a storage file.
  2. Read its index or data pages.
  3. Search for the key.
  4. Discover that the key is not there.
  5. Repeat for other files.

The lookup may be logically simple, but storage reads are relatively expensive. They consume I/O bandwidth, add latency, and compete with useful work.

A Bloom filter gives each file or data partition a cheap summary of the keys it contains. The database checks that summary first. If the filter says the key cannot be present, the database skips the read entirely.

You may encounter Bloom filters in documentation or configuration for an LSM-tree database. An LSM tree is a storage design that accumulates writes in sorted files and later merges those files. A lookup may need to consider several files, which makes rejecting impossible files especially valuable.

The bit array

A Bloom filter starts with an array of bits, initially all set to zero. Unlike a normal set, it does not store the original keys. It stores only evidence that certain positions have been touched.

Suppose the filter has eight positions:

index:  0 1 2 3 4 5 6 7
bits:   0 0 0 0 0 0 0 0

To add a key, the filter runs the key through several hash functions. A hash function deterministically turns an input into a number that appears distributed across a range. Each hash result selects one bit position, and the filter changes those bits from 0 to 1.

For example, adding alice might select positions 1, 4, and 6:

index:  0 1 2 3 4 5 6 7
bits:   0 1 0 0 1 0 1 0

Adding another key can set more bits. Bits are never cleared during ordinary insertion, because clearing one might erase evidence contributed by several different keys.

The filter therefore loses information as it compresses the set. It remembers that some keys caused some bits to be set, but not which key caused which bit.

Answering a membership query

To ask whether alice might be present, the filter hashes alice with the same hash functions used during insertion. It checks every resulting bit.

The second result is deliberately uncertain. Other keys may have set those same positions, making an absent key look present.

A compact example:

Filter bits:       0 1 0 0 1 0 1 0
Hash positions:      1     4     6
Checked bits:        1     1     1  -> possibly present

If the hash positions had included position 2, the answer would be definite absence because position 2 is still zero.

This produces the central guarantee:

A correctly maintained Bloom filter has no false negatives, but it can have false positives.

A false positive here means “possibly present” when the key is actually absent. It does not mean the database returns incorrect data. The filter is only deciding whether to perform a more authoritative check.

Why multiple hash functions help

Using one hash position would make the filter too weak: many unrelated keys would collide at the same bit. Multiple positions create a pattern for each key. An absent key must happen to find all of its positions already set before it produces a false positive.

However, adding more hash functions is not automatically better. More functions set more bits for every key, causing the array to fill sooner. A filter’s design balances:

Once most bits are set to 1, many queries will pass the filter and the database will gain little from it. Databases therefore size filters using expected key counts and a target error rate. They may create a separate filter for each file or partition rather than one enormous filter for the entire database.

How the database uses the result

Consider a database searching three storage files for a key:

File Bloom-filter result Database action
A Definitely absent Skip the file
B Possibly present Read and verify
C Definitely absent Skip the file

For file B, the database still consults its index or data. The Bloom filter never replaces the real lookup; it only prevents lookups that cannot succeed.

If file B produces a false positive, the database performs one unnecessary read and then finds no matching key. That costs time, but it does not change the answer. This is why false positives are acceptable: the expensive operation is optional work, while correctness remains the responsibility of the underlying index and stored data.

Filters must still be maintained consistently. If a file is rewritten, its filter must describe the rewritten contents. Deletions also require care: because ordinary Bloom filters do not support safely clearing individual bits, systems commonly build a new filter when rewriting data, or use a related structure designed for deletions.

The next time a database log mentions a Bloom-filter check or a filter false-positive rate, read it as an I/O optimization. The filter is a cheap gate: a zero bit rules a storage location out, while an all-one result grants permission to spend the cost of checking.

← All articles