Interviewer Question 👇 You start typing a username on Gmail — and instantly the UI says: “Username already taken.” But here’s the catch: There are hundreds of millions (even billions) of users globally. 👉 How does the system check availability in milliseconds? 👉 How is it able to respond so fast without scanning massive datasets every time? #softwareEngineering #development #coding #interviewQuestion #programming
That's a great question. Here's what I found while researching this: https://medium.com/@joyalsaji/understanding-bloom-filters-the-hidden-power-behind-username-is-taken-checks-e9991e95c2af In short, it uses a Bloom Filter - a tiny checklist living in RAM. Your username gets hashed into a few bits. If any bit is 0 -> it's free. Done. No database hit at all. Only if it says "maybe taken" does it check the cache -> then the database. Most requests never even reach the DB. That's how it replies in milliseconds at billion-user scale.
When you type a username, the system isn’t checking every single user one by one that would be way too slow. Instead, it uses something like a hash table or indexed database, where usernames are stored in a way that allows instant lookup. So when you enter a username, the system can directly jump to it and check if it exists this usually takes constant time (O(1)). On top of that, large systems like Gmail are distributed, meaning the data is spread across multiple servers, so each server only handles a small portion of users. They also use caching (like Redis) to make repeated checks even faster. That’s why even with millions or billions of users, the response feels almost instant no full database scan is happening.
As a Full stack developer , there are multiple options to achieve that and its a combination of multiple things all together 1-There will be debouncing applied, avoiding flood of api calls 2-Database indexing based on email , indexing just cut millions of records due to its algorithm just like dictionaries do. 3-CDN or shared data delivery networks , companies like Google keep their data on multiple servers, causing low latency and fast response.
Thanks for increasing my knowledge.
Expression tree?
The system doesn't compare what is entered to what are already taken; instead, it checks the availability. For example, there are 10 numbers 0,1,2,3,4,5,6,7,8,9 you can take one of them if available. All of them are available, so whichever you take won't give throw error; however if the first 5 are already taken, and you try to take number 6, because 6 is not in the box, it is not available. The system just jump to all available username, and anything that is not there throw an error, but it doesn't check what are already taken.
It operates on bits and doesn't search through a list it run the input through a few hash functions and check specific indexes in bit array
the values we need to serach for often we should index it anyway and indexing the value and then geting from that database is as fast as you can get a value from an object like for example if you have object users={ bilal:1 } users["bilal"]
usernames["umar911"]; something like that
When you type a username, the system does not scan the whole database. First, the system uses indexing or hashing, which helps it directly locate where that username should be stored instead of searching everything. Before going to the database, it also checks a very fast memory called cache, which is stored on the company’s servers (not in the browser). If the username is found in the cache, it immediately returns “already taken.” If it is not in the cache, the system uses the indexed database lookup to quickly check if the username exists, and then stores the result in cache for future requests. This is why the system responds in milliseconds — because it uses indexing + caching instead of full database scanning.