ASHU
System Design

Secret Architecture Behind Username Already Taken

10 min read

šŸ’” Ever wondered how Instagram or X tells you in milliseconds whether a username is available?

This post explains the exact system design, from beginner-level ideas to Big Tech scale — in a simple, skimmable format.


Quick Breakdown

  • Small apps → Database query
  • Medium apps → Cache
  • Big Tech → Bloom Filter + Cache + Distributed DB
  • Suggestions → Trie (Prefix Tree)

The Core Problem

When a user types a username:

  • The system must respond instantly
  • Even with billions of existing usernames
  • Without overloading databases

Level 1: Direct Database Check

Basic Idea

Check directly in the database.

SELECT COUNT(1)
FROM users
WHERE username = 'new_user';

Works when:

  • User base is small (thousands / millions)
  • Single database
  • Proper indexing

Breaks at scale:

  • Indexes grow very large
  • Too many read queries
  • Databases become bottlenecks

āš ļø Direct DB queries don't scale to billions of users


Level 2: Add a Cache (Redis / Memcached)

Idea

Store frequently checked usernames in memory.

User → App Server → Cache → Database

Request Flow

  1. Check cache
  2. Cache hit → return result instantly
  3. Cache miss → query DB
  4. Save result back to cache
        ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”
        │  Cache   │
        ā””ā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”€ā”€ā”˜
             │ miss
        ā”Œā”€ā”€ā”€ā”€ā–¼ā”€ā”€ā”€ā”€ā”€ā”
        │ Database │
        ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜

Benefits

  • Very fast for popular usernames
  • Reduces DB load

Limitations

  • Limited memory
  • Cache can get stale
  • First-time usernames still hit DB

šŸ’” Cache helps, but it's not enough for Big Tech scale


Level 3: Bloom Filter (Key Optimization)

What is a Bloom Filter?

A probabilistic data structure that answers:

"Does this username possibly exist?"

Rules

  • NO → username is definitely available
  • YES → username might exist (verify further)

How Bloom Filter Works

Adding a Username

username → hash1 → bit array
         → hash2 → bit array
         → hash3 → bit array

Checking a Username

If any bit = 0 → definitely NOT taken
If all bits = 1 → probably taken
[0 1 1 0 1 1 1 0]
        ↑ ↑ ↑

Why Bloom Filters Are Powerful

  • Extremely fast (memory-only)
  • Very space-efficient
  • Filters out most DB queries

āš ļø False positives are possible, false negatives are not


Level 4: Real Production Flow (Big Tech)

Full Architecture

User
 ↓
Load Balancer
 ↓
Bloom Filter
 ↓
Cache
 ↓
Database

Step-by-Step

  1. Bloom Filter rejects most usernames instantly
  2. Cache confirms frequently checked names
  3. Database is hit only rarely

šŸ”„ Most requests never reach the database


Level 5: Username Suggestions (Trie / Prefix Tree)

When daniel is taken, platforms suggest:

  • daniel_123
  • daniel_dev
  • daniel2025

What is a Trie?

A tree-like structure that stores strings by prefixes.

d
└── a
    └── n
        ā”œā”€ā”€ i → e → l
        └── n → y

Why Tries are Used

  • Lookup time depends only on string length (O(M))
  • Fast autocomplete
  • Easy username suggestions

Trade-offs

  • High memory usage
  • Hard to update in distributed systems

šŸ’” Compressed Tries (Radix Trees) reduce memory usage


Final Summary

LayerPurpose
DatabaseSource of truth
CacheFast repeated lookups
Bloom FilterEliminate most DB hits
TrieSuggestions & autocomplete

✨ Big Tech systems scale by layering smart optimizations — not by relying on one trick.


One-Line Takeaway

Speed at scale comes from filtering early and touching the database as little as possible.