Part 4 of 10
Bloom filters
LSM reads are slowest for very old keys and for keys that do not exist, because many files must be checked. A Bloom filter for each segment gives a fast, rough answer to "could this key be in this file?".
- It is a bit array, all zeros to start.
- Adding a key hashes it to a few bit positions and sets those bits to 1.
Guess first
To check a key, the filter hashes it to the same few bits. All of them are 1. What does that tell you?
A Bloom filter with 8 bits
Step 1 of 5
The filter starts as a row of bits, all 0. Each key is hashed twice, and each hash picks one bit.
- To check a key, hash it the same way and look at those bits.
- If any bit is 0, the key is surely not in the file.
- If all of them are 1, the key is probably there.
- A false positive happens when other keys happened to set all the same bits, even though this key was never added.
- There are no false negatives.
Checking bits is extremely cheap for a CPU. The filter is tiny next to the SSTable and is stored next to the sparse index. The false positive rate depends on how many keys there are, how many bits each key sets, and how many bits the array has. A handy rule: about 10 bits per key gives roughly 1% false positives, and each extra 5 bits per key cuts that rate about 10 times.
False positives do no real harm here. A sure "no" lets the engine skip the whole file. A wrong "maybe" only costs one wasted block read, and then the search moves on to the next file.
Compaction strategies
Engines let you choose when compaction runs and which files it merges.
Size-tiered compaction merges newer, smaller files into older, bigger ones.
- For example, four 100 MB files might merge into one 340 MB file, not 400 MB, because overwritten, deleted, and expired values are dropped along the way.
- The files holding old data grow very large, and merging them needs lots of spare disk space while the merge runs.
- It handles very high write rates well, because each value is rewritten only a few times, in big sequential merges.
Leveled compaction keeps fixed-size files grouped into levels L0, L1, L2, and so on.
- L0 holds the newest data.
- Levels after L0 are split by key range, for example one file for keys a to m and one for n to z.
- Each level has a size limit, and each limit is bigger than the one before.
- When a level is over its limit, some of its files are merged down into the next level.
- It works in smaller steps, uses less disk space, and makes reads cheaper, because each lookup checks fewer files.
Rule of thumb: size-tiered for write-heavy loads with few reads, leveled for read-heavy loads.
Embedded storage engines
An embedded database is a library that runs inside your app's process and reads files on the local disk. You call it with normal function calls, not over a network. It is very common in phone apps. On servers it fits when the data fits on one machine and there are few transactions at the same time. One example: a service with many small, fully separate customers can give each customer its own embedded database, as long as you never need to query across customers.