How would you use a Bloom filter to prevent a cache stampede or unnecessary database lookups for items that definitely do not exist?
Short answer
You would populate a Bloom filter with all the identifiers of items that are known to actually exist in your database, and before performing an expensive database lookup for a requested item, first check the Bloom filter, immediately returning a not found response without ever…
Advertisement
AModel answer
You would populate a Bloom filter with all the identifiers of items that are known to actually exist in your database, and before performing an expensive database lookup for a requested item, first check the Bloom filter, immediately returning a not found response without ever touching the database if the filter indicates the item definitely does not exist, only proceeding to the database when the filter suggests it might.
if not BF.EXISTS existing_products 'product_999':
return 'Not Found' # skip the database entirely
else:
return database.get_product('product_999')Real-world example
An e-commerce platform prevents unnecessary database queries for clearly invalid or non existent product ids by checking a Bloom filter first, significantly reducing load on their database from bots or scanners probing for random product ids.
Likely follow-ups
- How do you keep the Bloom filter synchronized as new items are actually added to the underlying database?
- What happens if the Bloom filter becomes out of sync with the actual database contents?
Related topics