{ "hypothesis": "With a fixed total API CPU/RAM budget (2.0 CPU / 512M), two replicas outperform both one (a single uvicorn worker cannot spend 2.0 CPU) and four (each worker is capped at 0.5 CPU, p95 collapses). Growing per-replica CPU still raises RPS until that slice is no longer tiny. PostgreSQL and HAProxy stay below their limits; extra replicas past 2 only add pool connections to the same 1.0-CPU database. The confirmed improvement is 2×1.0 instead of 4×0.5 at the same budget.", "host": { "ncpu": 12, "memory_bytes": 12445638656 }, "profile": { "concurrency": 24, "warmup_seconds": 5, "measure_seconds": 20, "workload": "closed-loop POST /api/v1/chats/{id}/messages through HAProxy", "fixed_budget": "API total 2.0 CPU / 512M; postgres 1.0 CPU / 512M; pool 10 per replica" }, "runs": [ { "id": "fixed-1", "series": "fixed", "replicas": 1, "api_cpus": "2.0", "api_memory": "512M", "postgres_cpus": "1.0", "pool": 10, "note": "1 replica takes the whole 2.0 CPU / 512M budget", "inspected_limits": [ { "name": "messenger-lab3-api-1", "service": "api", "cpus": 2.0, "memory_mb": 512 }, { "name": "messenger-lab3-lb-1", "service": "lb", "cpus": 0.5, "memory_mb": 128 }, { "name": "messenger-lab3-migrate-1", "service": "migrate", "cpus": 0.5, "memory_mb": 128 }, { "name": "messenger-lab3-postgres-1", "service": "postgres", "cpus": 1.0, "memory_mb": 512 } ], "metrics": { "successful_requests": 3750, "failed_requests": 0, "error_ratio": 0.0, "rps": 187.5, "p50_ms": 122.2, "p95_ms": 203.6, "p99_ms": 267.2, "instances": { "74b3971ded9d": 3750 }, "instance_count": 1 }, "resources": { "api_cpu_sum_pct": 98.52, "api_cpu_each_pct": { "messenger-lab3-api-1": 98.52 }, "api_util_pct": 49.3, "postgres_cpu_pct": 46.8, "postgres_util_pct": 46.8, "lb_cpu_pct": 19.88, "lb_util_pct": 39.8, "pg_backends_mean": 11.0, "pg_active_mean": 1.38, "pg_waiting_mean": 0.12, "pool_capacity": 10, "pool_util_pct": 110.0 }, "saturated": "pool", "saturation_scores": { "postgres": 46.8, "lb": 39.8, "api": 49.3, "pool": 110.0 }, "samples": 8 }, { "id": "fixed-2", "series": "fixed", "replicas": 2, "api_cpus": "1.0", "api_memory": "256M", "postgres_cpus": "1.0", "pool": 10, "note": "baseline: 2 × 1.0 CPU / 256M (also grow-1.00)", "inspected_limits": [ { "name": "messenger-lab3-api-1", "service": "api", "cpus": 1.0, "memory_mb": 256 }, { "name": "messenger-lab3-api-2", "service": "api", "cpus": 1.0, "memory_mb": 256 }, { "name": "messenger-lab3-lb-1", "service": "lb", "cpus": 0.5, "memory_mb": 128 }, { "name": "messenger-lab3-migrate-1", "service": "migrate", "cpus": 0.5, "memory_mb": 128 }, { "name": "messenger-lab3-postgres-1", "service": "postgres", "cpus": 1.0, "memory_mb": 512 } ], "metrics": { "successful_requests": 4990, "failed_requests": 0, "error_ratio": 0.0, "rps": 249.5, "p50_ms": 87.4, "p95_ms": 196.7, "p99_ms": 285.1, "instances": { "44ffd5fcad43": 2501, "b1551f2692b4": 2489 }, "instance_count": 2 }, "resources": { "api_cpu_sum_pct": 170.27, "api_cpu_each_pct": { "messenger-lab3-api-1": 83.81, "messenger-lab3-api-2": 86.46 }, "api_util_pct": 85.1, "postgres_cpu_pct": 66.23, "postgres_util_pct": 66.2, "lb_cpu_pct": 23.3, "lb_util_pct": 46.6, "pg_backends_mean": 21.0, "pg_active_mean": 2.0, "pg_waiting_mean": 0.75, "pool_capacity": 20, "pool_util_pct": 105.0 }, "saturated": "pool", "saturation_scores": { "postgres": 66.2, "lb": 46.6, "api": 85.1, "pool": 105.0 }, "samples": 8 }, { "id": "fixed-4", "series": "fixed", "replicas": 4, "api_cpus": "0.5", "api_memory": "128M", "postgres_cpus": "1.0", "pool": 10, "note": "4 replicas, same 2.0 CPU / 512M total", "inspected_limits": [ { "name": "messenger-lab3-api-1", "service": "api", "cpus": 0.5, "memory_mb": 128 }, { "name": "messenger-lab3-api-2", "service": "api", "cpus": 0.5, "memory_mb": 128 }, { "name": "messenger-lab3-api-3", "service": "api", "cpus": 0.5, "memory_mb": 128 }, { "name": "messenger-lab3-api-4", "service": "api", "cpus": 0.5, "memory_mb": 128 }, { "name": "messenger-lab3-lb-1", "service": "lb", "cpus": 0.5, "memory_mb": 128 }, { "name": "messenger-lab3-migrate-1", "service": "migrate", "cpus": 0.5, "memory_mb": 128 }, { "name": "messenger-lab3-postgres-1", "service": "postgres", "cpus": 1.0, "memory_mb": 512 } ], "metrics": { "successful_requests": 3717, "failed_requests": 0, "error_ratio": 0.0, "rps": 185.85, "p50_ms": 44.6, "p95_ms": 464.5, "p99_ms": 612.5, "instances": { "6b3abbff31a1": 925, "827845da8b75": 926, "7efb091f3c55": 925, "16b68cf38bcd": 941 }, "instance_count": 4 }, "resources": { "api_cpu_sum_pct": 165.5, "api_cpu_each_pct": { "messenger-lab3-api-1": 41.19, "messenger-lab3-api-2": 40.7, "messenger-lab3-api-3": 39.44, "messenger-lab3-api-4": 44.17 }, "api_util_pct": 82.8, "postgres_cpu_pct": 61.7, "postgres_util_pct": 61.7, "lb_cpu_pct": 18.79, "lb_util_pct": 37.6, "pg_backends_mean": 39.38, "pg_active_mean": 1.38, "pg_waiting_mean": 0.38, "pool_capacity": 40, "pool_util_pct": 98.5 }, "saturated": "pool", "saturation_scores": { "postgres": 61.7, "lb": 37.6, "api": 82.8, "pool": 98.5 }, "samples": 8 }, { "id": "grow-0.25", "series": "grow", "replicas": 2, "api_cpus": "0.25", "api_memory": "256M", "postgres_cpus": "1.0", "pool": 10, "note": "starve API CPU", "inspected_limits": [ { "name": "messenger-lab3-api-2", "service": "api", "cpus": 0.25, "memory_mb": 256 }, { "name": "messenger-lab3-api-4", "service": "api", "cpus": 0.25, "memory_mb": 256 }, { "name": "messenger-lab3-lb-1", "service": "lb", "cpus": 0.5, "memory_mb": 128 }, { "name": "messenger-lab3-migrate-1", "service": "migrate", "cpus": 0.5, "memory_mb": 128 }, { "name": "messenger-lab3-postgres-1", "service": "postgres", "cpus": 1.0, "memory_mb": 512 } ], "metrics": { "successful_requests": 501, "failed_requests": 0, "error_ratio": 0.0, "rps": 25.05, "p50_ms": 1086.6, "p95_ms": 1916.3, "p99_ms": 2246.0, "instances": { "96ce82c183ab": 252, "08070b66fd10": 249 }, "instance_count": 2 }, "resources": { "api_cpu_sum_pct": 48.01, "api_cpu_each_pct": { "messenger-lab3-api-2": 23.78, "messenger-lab3-api-4": 24.23 }, "api_util_pct": 96.0, "postgres_cpu_pct": 26.35, "postgres_util_pct": 26.4, "lb_cpu_pct": 5.8, "lb_util_pct": 11.6, "pg_backends_mean": 21.0, "pg_active_mean": 1.12, "pg_waiting_mean": 0.12, "pool_capacity": 20, "pool_util_pct": 105.0 }, "saturated": "pool", "saturation_scores": { "postgres": 26.4, "lb": 11.6, "api": 96.0, "pool": 105.0 }, "samples": 8 }, { "id": "grow-0.50", "series": "grow", "replicas": 2, "api_cpus": "0.50", "api_memory": "256M", "postgres_cpus": "1.0", "pool": 10, "note": "halfway API CPU", "inspected_limits": [ { "name": "messenger-lab3-api-2", "service": "api", "cpus": 0.5, "memory_mb": 256 }, { "name": "messenger-lab3-api-4", "service": "api", "cpus": 0.5, "memory_mb": 256 }, { "name": "messenger-lab3-lb-1", "service": "lb", "cpus": 0.5, "memory_mb": 128 }, { "name": "messenger-lab3-migrate-1", "service": "migrate", "cpus": 0.5, "memory_mb": 128 }, { "name": "messenger-lab3-postgres-1", "service": "postgres", "cpus": 1.0, "memory_mb": 512 } ], "metrics": { "successful_requests": 1877, "failed_requests": 0, "error_ratio": 0.0, "rps": 93.85, "p50_ms": 211.4, "p95_ms": 593.2, "p99_ms": 894.4, "instances": { "51e7463ae51d": 947, "ad32f7164869": 930 }, "instance_count": 2 }, "resources": { "api_cpu_sum_pct": 97.33, "api_cpu_each_pct": { "messenger-lab3-api-2": 48.85, "messenger-lab3-api-4": 48.48 }, "api_util_pct": 97.3, "postgres_cpu_pct": 37.57, "postgres_util_pct": 37.6, "lb_cpu_pct": 12.03, "lb_util_pct": 24.1, "pg_backends_mean": 21.0, "pg_active_mean": 1.71, "pg_waiting_mean": 0.57, "pool_capacity": 20, "pool_util_pct": 105.0 }, "saturated": "pool", "saturation_scores": { "postgres": 37.6, "lb": 24.1, "api": 97.3, "pool": 105.0 }, "samples": 7 }, { "id": "improve-pool20", "series": "improve", "replicas": 2, "api_cpus": "1.0", "api_memory": "256M", "postgres_cpus": "1.0", "pool": 20, "note": "same 2×1.0 API, pool 10 → 20 per replica", "inspected_limits": [ { "name": "messenger-lab3-api-2", "service": "api", "cpus": 1.0, "memory_mb": 256 }, { "name": "messenger-lab3-api-4", "service": "api", "cpus": 1.0, "memory_mb": 256 }, { "name": "messenger-lab3-lb-1", "service": "lb", "cpus": 0.5, "memory_mb": 128 }, { "name": "messenger-lab3-migrate-1", "service": "migrate", "cpus": 0.5, "memory_mb": 128 }, { "name": "messenger-lab3-postgres-1", "service": "postgres", "cpus": 1.0, "memory_mb": 512 } ], "metrics": { "successful_requests": 2813, "failed_requests": 0, "error_ratio": 0.0, "rps": 140.65, "p50_ms": 142.7, "p95_ms": 396.9, "p99_ms": 610.0, "instances": { "29e73f6300f7": 1407, "3eb380a3d7c4": 1406 }, "instance_count": 2 }, "resources": { "api_cpu_sum_pct": 178.32, "api_cpu_each_pct": { "messenger-lab3-api-2": 85.35, "messenger-lab3-api-4": 92.97 }, "api_util_pct": 89.2, "postgres_cpu_pct": 63.3, "postgres_util_pct": 63.3, "lb_cpu_pct": 22.61, "lb_util_pct": 45.2, "pg_backends_mean": 35.14, "pg_active_mean": 2.86, "pg_waiting_mean": 1.57, "pool_capacity": 40, "pool_util_pct": 87.8 }, "saturated": "api", "saturation_scores": { "postgres": 63.3, "lb": 45.2, "api": 89.2, "pool": 87.8 }, "samples": 7 } ], "finding": "Fixed 2.0 CPU / 512M budget: 1/2/4 replicas → 187.5 / 249.5 / 185.8 RPS. One replica uses only 49% of a 2.0 limit (single uvicorn worker ≈ 1 core). Four replicas cap each worker at 0.5 CPU (p95 464.5 ms). Growing 2×CPU 0.25→0.50→1.0: 25.1 → 93.8 → 249.5 RPS; API util 96%/97% then 85%; postgres util 66%; LB util 47%. Confirmed improvement: same budget, 4×0.5 → 2×1.0 raises RPS 185.8 → 249.5 (+34.2%) and p95 464.5 → 196.7 ms. Raising DB_POOL_MAX 10→20 was tried and rejected (140.7 RPS, p95 396.9 ms)." }