System Design: Scaling to 1 Million Users
Scaling isn’t about buying bigger servers. It’s about removing bottlenecks. Here is the evolution of a startup’s architecture.
Stage 1: The Monolith (0 - 1k Users)#
One server. Database + App + Web Server all on the same machine.
- Pros: Simple deployment, cheap.
- Cons: Single point of failure.
Stage 2: Vertical Scaling (1k - 10k Users)#
Upgrade the instance size (t3.micro -> m5.large). This works until it doesn’t. You eventually hit the limit of a single machine.
Stage 3: Horizontal Scaling (10k - 100k Users)#
This is the big jump.
- Load Balancer (ELB/Nginx): Distributes traffic across multiple app servers.
- Stateless Apps: User sessions moved to Redis.
- Database Separation: Master (Write) / Replica (Read) architecture.
graph TD
User --> LB[Load Balancer]
LB --> App1[App Server 1]
LB --> App2[App Server 2]
App1 --> Redis[Redis Cache]
App2 --> Redis
App1 --> DB_M[DB Master]
App2 --> DB_R[DB Replica]
DB_M -->|Replication| DB_R
Stage 4: 1M+ Users#
Now we need serious optimizations.
- CDN (Cloudflare): Cache static assets at the edge.
- Database Sharding: Split the database by User ID (Users 1-1M on DB1, 1M-2M on DB2).
- Async Workers (Celery/Kafka): Move everything non-essential (email, PDF generation) to background queues.