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:

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:

  1. Yangi server tayyorlash
  2. Eski serverni to’xtatish
  3. Ma’lumotlarni ko’chirish
  4. Yangi serverni ishga tushirish

Downtime: 10 minut - 1 soat

Horizontal:

  1. Yangi server qo’shish
  2. Load balancerga qo’shish
  3. 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:

Stateful tizimlar:

Legacy ilovalar:

Development/Testing:

Horizontal scaling - qachon mos

Katta scale:

High availability kerak:

Stateless tizimlar:

Cloud-native:

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:

Database Scaling

Vertical scaling (Master)

-- Bitta kuchli server
PostgreSQL: 96 CPU, 768GB RAM
- Write: 50K TPS
- Read: 100K TPS

Qachon yetarli:

Cheklov:

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:

Murakkablik:

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:

Xulosa

Vertical Scaling:

Horizontal Scaling:

Real-world:

Keyingi dars: Stateful vs Stateless arxitektura.