# System Design kaise badhta hai: stage-by-stage checklist (howitworks.io)

Rule: simple se start karo, Day 1 se monitoring lagao, alert par bottleneck dhoondho, aur sirf usi ka fix lagao.

## Day 1 monitoring stack (sab open source)

| Tool | Kaam |
|------|------|
| OpenTelemetry | app se traces + metrics nikalna (SDK) |
| Prometheus | metrics store + alert rules |
| Grafana | dashboards + alerts |
| Jaeger | request trace ka waterfall: time kahan gaya |

## 6 stages: symptom, kaise pakda, fix, verify

| Stage | Symptom | Kaise pakda | Fix | Verify |
|-------|---------|-------------|-----|--------|
| Day 1 | koi problem nahi | - | monolith: 1 app server + 1 database | dashboards + alerts ready |
| Slow page | p95 latency 1 s se upar | Grafana alert, Jaeger trace, pg_stat_statements | Redis cache (cache-aside) | p95 neeche, cache hit rate upar, DB active queries kam |
| Sale traffic | CPU 80%+ , failed requests | CPU alert; sale se pehle k6 load test | horizontal scaling: 3 servers + load balancer, sessions Redis mein (stateless) | CPU sab servers par barabar; ek server band, users ko fark nahi |
| Slow checkout | request mein email, SMS, PDF ek ke baad ek | Jaeger waterfall | queue (RabbitMQ) + background workers | checkout fast; queue depth alert; workers badhao |
| Database load | primary par bahut queries, zyadatar reads | pg_stat_statements | read replicas (reads replica par, writes primary par) + CDN static files ke liye | primary load kam; replication lag alert |
| Badi team | ek bug se poori site down, har deploy mein dar | error dashboard, logs | microservices (har service ka apna DB) + API gateway | ek service crash, baaki chalti rahein; har team khud deploy |

Yaad rakho: microservices ka sabse bada fayda team ko milta hai (independent deploys, failure isolation). Traffic ke liye monolith bhi load balancer ke peeche scale ho jaata hai.

## Kaam ki queries aur commands

Sabse zyada time khane wali queries (PostgreSQL 13+, extension pg_stat_statements on hona chahiye):

```sql
SELECT calls, round(mean_exec_time) AS mean_ms, left(query, 80) AS query
FROM pg_stat_statements ORDER BY total_exec_time DESC LIMIT 5;
```

Replica kitna peeche hai (replica par chalao):

```sql
SELECT now() - pg_last_xact_replay_timestamp() AS replication_lag;
```

Alert ideas (Prometheus, metric naam aapke exporter par depend karte hain):

```
# p95 latency 1 s se upar (OpenTelemetry HTTP server metric)
histogram_quantile(0.95, sum by (le) (rate(http_server_request_duration_seconds_bucket[5m]))) > 1
# CPU 80% se upar, 1 minute tak (node_exporter)
100 * (1 - avg by (instance) (rate(node_cpu_seconds_total{mode="idle"}[1m]))) > 80
# Queue mein messages jama ho rahe (RabbitMQ prometheus plugin)
rabbitmq_queue_messages_ready > 300
```

Sale se pehle load test (k6):

```js
// load-test.js   chalao: k6 run --vus 200 --duration 1m load-test.js
import http from "k6/http";
export default function () { http.get("http://localhost:3000/products"); }
```

## Interview mein ye flow bolo

1. Requirements: users, reads vs writes, latency target.
2. Simple HLD: client, app server, database. Monitoring Day 1 se.
3. Har bottleneck ke liye: symptom, kaunsa metric/alert, fix, aur fix ke baad kya check karoge.
4. Order: cache, load balancer + stateless servers, queue, replicas + CDN, microservices sabse last.

Docs: opentelemetry.io, prometheus.io, grafana.com, jaegertracing.io, grafana.com/docs/k6, postgresql.org (pg_stat_statements), rabbitmq.com, martinfowler.com/bliki/MonolithFirst.html
Poora page: https://howitworksio.com/system-design-evolution/
