(https://i.ibb.co/vCPH40dX/d5mlatz5eovg1.png)
Conditional if (just being sarcastic)
The username disappears before you even finish typing it.
Usually they use Bloom filters.
Bloom filter.
Bloom filters or tries.
I search anything on Google and the results show up in about a second, even though there are trillions of data sources across the web.
Not millions, billions of accounts.
1] Indexed database
2] Hashing
3] Distributed systems like Google Spanner
These make lookup possible in O(log n), insanely fast.
Bonus:
They cache hot usernames - popular ones and recent queries.
All of this together makes it possible.
Many comments have already mentioned Bloom filters, but that's only part of the picture since they can tell if a username definitely doesn't exist. They can give false positives, saying a username may exist, so they're used as a fast pre‑check, not the final source of truth. There's a ByteMonk video https://youtu.be/_l5Q5kKHtR8 that explains this flow in detail - a must‑watch for me.
Bloom filter.