<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Prado7</title>
    <description>The latest articles on Forem by Prado7 (@prado7).</description>
    <link>https://forem.com/prado7</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3723217%2F4af273a7-7c61-4d55-bca8-a6f56ebd80f9.png</url>
      <title>Forem: Prado7</title>
      <link>https://forem.com/prado7</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/prado7"/>
    <language>en</language>
    <item>
      <title>Bloom Filters : Strictly NO, May be YES</title>
      <dc:creator>Prado7</dc:creator>
      <pubDate>Wed, 21 Jan 2026 08:26:27 +0000</pubDate>
      <link>https://forem.com/prado7/bloom-filters-strictly-no-may-be-yes-12j6</link>
      <guid>https://forem.com/prado7/bloom-filters-strictly-no-may-be-yes-12j6</guid>
      <description>&lt;p&gt;Ever wondered how large-scale systems check &lt;strong&gt;username availability&lt;/strong&gt;?&lt;br&gt;
Do they iterate over every single existing username and respond with a YES or NO?&lt;br&gt;
Well,that would take an eternity. So how do systems at scale handle this efficiently?&lt;/p&gt;

&lt;p&gt;👉 &lt;strong&gt;Enter Bloom Filters.&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  What is a Bloom Filter?
&lt;/h2&gt;

&lt;p&gt;The term Bloom Filter may sound fancy, but at its core, it's surprisingly simple.&lt;br&gt;
A Bloom Filter is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A probabilistic data structure&lt;/li&gt;
&lt;li&gt;Backed by a bit array (0's and 1's)&lt;/li&gt;
&lt;li&gt;Designed to answer one question&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;"Have we seen this element before?"&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  What does a Bloom Filter guarantee?
&lt;/h2&gt;

&lt;p&gt;A Bloom Filter can return only two types of answers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;❌ Definitely NO&lt;/li&gt;
&lt;li&gt;⚠️ Maybe YES&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It never produces false negatives, but false positives are possible.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;False negative:&lt;/strong&gt;&lt;br&gt;
Bloom Filter ==&amp;gt; not present&lt;br&gt;
Database ==&amp;gt; present (❌ never happens)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;False positive:&lt;/strong&gt;&lt;br&gt;
Bloom Filter ==&amp;gt; present&lt;br&gt;
Database ==&amp;gt; not present (⚠️ possible)&lt;/p&gt;
&lt;h2&gt;
  
  
  How does a Bloom Filter work?
&lt;/h2&gt;

&lt;p&gt;Let's walk through the flow step by step.&lt;br&gt;
&lt;strong&gt;Step 1 : User enters a word&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cat
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 2 : Initialize the Bloom Filter&lt;/strong&gt;&lt;br&gt;
To keep things simple, let's assume&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Bit array size =  10&lt;/li&gt;
&lt;li&gt;Number of hash functions = 2 (hash1, hash2)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Initial bit array&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0 1 2 3 4 5 6 7 8 9
0 0 0 0 0 0 0 0 0 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 3 : Hash the word&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Next we pass the word through the hash functions&lt;br&gt;
*Hash values are assumed for illustration&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;hash1("cat") ==&amp;gt; 4
hash2("cat") ==&amp;gt; 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Hash output is reduced using modulo to fit the bit array.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Step 4 : Set the bits&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We set the corresponding bits positions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0 1 2 3 4 5 6 7 8 9
0 0 0 0 1 1 0 0 0 0 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This indicates that &lt;strong&gt;we have seen this word&lt;/strong&gt;.&lt;br&gt;
The actual word is then stored in the database.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;The Bloom Filter does not store the word itself.&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  How does a Bloom Filter check if a word exists?
&lt;/h2&gt;

&lt;p&gt;When a user wants to check if a word exists, we repeat the same hashing process&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Case 1 : Word does not exist&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's check the word "dog":&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;hash1("dog") ==&amp;gt; 4
hash2("dog") ==&amp;gt; 7
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we check these bits/indexes in our bit array&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
0 1 2 3 4 5 6 7 8 9
0 0 0 0 1 1 0 0 0 0
        ↑     ↑
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since at least one bit is 0, we can say with certainty:&lt;br&gt;
❌ &lt;strong&gt;This word does not exist. (Strict NO)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Case 2 : Word may exist&lt;/strong&gt;&lt;br&gt;
Now let's check "cat" again:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;hash1("cat") ==&amp;gt; 4
hash2("cat") ==&amp;gt; 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0 1 2 3 4 5 6 7 8 9
0 0 0 0 1 1 0 0 0 0
        ↑ ↑
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here the corresponding bits/indexes are set (All bits are set)&lt;br&gt;
&lt;strong&gt;⚠️ This word may exist&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Why "May be YES"? (False Positives)
&lt;/h2&gt;

&lt;p&gt;Bloom Filters suffer from hash collisions.&lt;br&gt;
Multiple words can hash to the same bit positions.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;word : "bat"
hash1("bat") ==&amp;gt; 4
hash2("bat") ==&amp;gt; 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These bits were already set by "cat".&lt;br&gt;
Even though the word "bat" was never inserted, the Bloom Filter reports:&lt;br&gt;
⚠️ May be present&lt;br&gt;
&lt;strong&gt;This is the case of false positive.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  When do false positives increase?
&lt;/h2&gt;

&lt;p&gt;False positives are affected by&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Small bit array size&lt;/li&gt;
&lt;li&gt;Large number of inserted elements&lt;/li&gt;
&lt;li&gt;Too many hash functions&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;In short:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If any bit is not set → ❌ Definitely NO&lt;/li&gt;
&lt;li&gt;If all bits are set → ⚠️ Maybe YES&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  TL;DR
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Bloom Filters are an optimization technique&lt;/li&gt;
&lt;li&gt;They guarantee no false negatives&lt;/li&gt;
&lt;li&gt;They can produce false positives&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;In practice, Bloom filters are used as a fast pre-check before hitting a database.&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
&lt;em&gt;&lt;strong&gt;Bloom Filters don't tell you "YES or NO".&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
&lt;em&gt;&lt;strong&gt;They tell you "NO or MAYBE".&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>programming</category>
      <category>bloomfilters</category>
      <category>systemdesign</category>
    </item>
  </channel>
</rss>
