System Design
Vertical vs Horizontal Scaling
Tizim o’sganda ikki yo’l bor: yuqoriga yoki yonga kengayish.
Yana bir bor: tushunchalar
Vertical Scaling (Scale Up)
Mavjud mashinani kuchliroq mashina bilan almashtirish:
Server v1.0 Server v2.0
┌──────────┐ ┌──────────┐
│ 4 CPU │ → │ 32 CPU │
│ 16 GB │ │ 256 GB │
│ 1 TB │ │ 10 TB │
└──────────┘ └──────────┘
Horizontal Scaling (Scale Out)
Ko’proq mashina qo’shish:
Server 1 Server 1, 2, 3, 4...
┌──────────┐ ┌────┐ ┌────┐ ┌────┐ ┌────┐
│ 8 CPU │ → │8CPU│ │8CPU│ │8CPU│ │8CPU│
│ 32 GB │ │32GB│ │32GB│ │32GB│ │32GB│
└──────────┘ └────┘ └────┘ └────┘ └────┘
Chuqur taqqoslash
Narx (Cost)
Vertical:
2x CPU → 2x narx
4x CPU → 4x narx
8x CPU → 10x narx (eksponensial)
Katta serverlar juda qimmat. AWS’da:
t3.medium(2 CPU, 4GB) = $30/oym5.24xlarge(96 CPU, 384GB) = $4600/oy
Horizontal:
1 server → $100/oy
5 server → $500/oy (linear)
10 server → $1000/oy (linear)
Cheklov (Limits)
Vertical:
World's biggest server (AWS u-24tb1.metal):
- 448 vCPU
- 24 TB RAM
- $218K+ per year
Bu yerda tugaydi!
Horizontal: Cheksiz (nazariy):
Google: 1M+ servers
Amazon: 1M+ servers
Facebook: 100K+ servers
Deployment Strategy
Vertical:
- Yangi server tayyorlash
- Eski serverni to’xtatish
- Ma’lumotlarni ko’chirish
- Yangi serverni ishga tushirish
Downtime: 10 minut - 1 soat
Horizontal:
- Yangi server qo’shish
- Load balancerga qo’shish
- Eski serverni bosqichma-bosqich o’chirish
Zero downtime
Failure Handling
Vertical (Single Point of Failure):
┌──────────┐
│ Server │ ← Ishlamasa?
└──────────┘
↓
Butun tizim to'xtaydi
Horizontal (Redundancy):
┌────┐ ┌────┐ ┌────┐ ┌────┐
│ S1 │ │ S2 │ │ S3 │ │ S4 │
└────┘ └────┘ └────┘ └────┘
Ishlamasa?
↓
Qolgan 3 ta server ishlaydi
Qachon qaysi birini tanlash?
Vertical scaling - qachon mos
Startup bosqichi:
- 0-10K foydalanuvchi
- MVP testing
- Tez deploy kerak
Stateful tizimlar:
- Database (PostgreSQL, MySQL)
- Cache server (Redis single instance)
- File storage
Legacy ilovalar:
- Distributed qilish qiyin
- Qayta yozish imkoni yo’q
Development/Testing:
- Production’da horizontal, dev’da vertical
Horizontal scaling - qachon mos
Katta scale:
- 100K+ foydalanuvchi
- Million+ requests/day
- Cheksiz o’sish kutilsa
High availability kerak:
- 99.99%+ uptime
- Mission-critical tizimlar
- 24/7 ishlashi shart
Stateless tizimlar:
- Web serverlar
- API serverlar
- Microservices
Cloud-native:
- Kubernetes
- Auto-scaling
- Containerized apps
Amaliyotda: Hybrid Approach
Real projectlarda ikkalasi birgalikda ishlatiladi:
Misol: E-commerce platform
┌─────────────────────────┐
│ Load Balancer (HA) │
└───────────┬─────────────┘
│
┌───────┴───────┬──────────────┐
▼ ▼ ▼
┌────────┐ ┌────────┐ ┌────────┐
│Web Srv1│ │Web Srv2│ │Web Srv3│ ← Horizontal
│(4 CPU) │ │(4 CPU) │ │(4 CPU) │ ← Vertical (har biri kuchaytirilgan)
└────────┘ └────────┘ └────────┘
│ │ │
└───────────────┴──────────────┘
▼
┌──────────────┐
│ PostgreSQL │
│ Master │ ← Vertical (96 CPU, 768GB)
│ (Replica) │ ← + Horizontal (read replicas)
└──────────────┘
Strategiya:
- Web tier: Horizontal (stateless, easy)
- Database: Vertical + Read replicas (stateful)
- Cache: Distributed (horizontal)
Database Scaling
Vertical scaling (Master)
-- Bitta kuchli server
PostgreSQL: 96 CPU, 768GB RAM
- Write: 50K TPS
- Read: 100K TPS
Qachon yetarli:
- Startup - Medium size
- 1-10M rows
- Simple queries
Cheklov:
- Write throughput limited
- Narx eksponensial
Horizontal scaling (Sharding)
┌─────────────┐
│ Router │
└──────┬──────┘
│
┌───┴───┬───────┬───────┐
▼ ▼ ▼ ▼
┌─────┐ ┌─────┐ ┌─────┐ ┌─────┐
│Shard│ │Shard│ │Shard│ │Shard│
│ 1 │ │ 2 │ │ 3 │ │ 4 │
└─────┘ └─────┘ └─────┘ └─────┘
users users users users
1-25M 25-50M 50-75M 75-100M
Afzalliklari:
- Cheksiz write capacity
- Cost-effective
Murakkablik:
- Cross-shard queries qiyin
- Rebalancing kerak
- Transaction distributed
Cost Analysis misol
Scenario: 1M requests/day
Option 1: Vertical
1x c5.18xlarge (72 vCPU, 144GB)
Cost: $3,060/month
Capacity: 1.5M req/day
Option 2: Horizontal
10x c5.xlarge (4 vCPU, 8GB each)
Cost: $1,700/month
Capacity: 2M req/day (with load balancer)
Tejash: $1,360/month (44%)
Scale to 10M requests/day
Option 1: Vertical
1x u-12tb1.metal (448 vCPU, 12TB)
Cost: $109,200/month
Option 2: Horizontal
100x c5.xlarge
Cost: $17,000/month
Tejash: $92,200/month (84%)
Monitoring Metrics
Qachon scale qilish kerakligini bilish uchun:
Vertical scaling signals
CPU Usage > 80%
Memory Usage > 85%
Disk I/O > 90%
Response time degrading
Action: Server upgrade
Horizontal scaling signals
All servers > 70% CPU
Request queue growing
Timeout errors increasing
Response time > SLA
Action: Add more servers
Auto-scaling strategiyalari
AWS Auto Scaling Group
MinSize: 2
MaxSize: 10
DesiredCapacity: 4
ScaleOut: CPU > 70% for 5 minutes
ScaleIn: CPU < 30% for 10 minutes
Afzalliklari:
- Traffic spike’larga avtomatik
- Tungi vaqt scale down
- Cost optimization
Xulosa
Vertical Scaling:
- Oddiy, tez boshlash
- Stateful tizimlar uchun
- Cheklangan, qimmat
- Single point of failure
Horizontal Scaling:
- Cheksiz, arzon
- High availability
- Murakkab, stateless kerak
- Distributed systems muammolari
Real-world:
- Kichik boshlang (vertical)
- Tez o’sganda (horizontal)
- Hybrid approach eng yaxshi
- Monitoring va auto-scaling qo’ying
Keyingi dars: Stateful vs Stateless arxitektura.