forked from mtusi2026/mtusi2026lab3
64 lines
3.4 KiB
Markdown
64 lines
3.4 KiB
Markdown
# Track 1 methodology: price of scaling
|
|
|
|
## Hypothesis
|
|
|
|
At a fixed total API CPU/RAM budget, adding API replicas raises HTTP
|
|
acknowledgement throughput only until a shared component saturates. On this
|
|
single Docker host the candidate shared bottlenecks are PostgreSQL CPU/WAL,
|
|
the pgx pool, HAProxy and the 2-vCPU VM itself. Once DB CPU is throttled, more
|
|
replicas add contention rather than throughput. Folding the message insert and
|
|
the idempotency record into one atomic statement should reduce DB work per
|
|
acknowledged message and raise throughput at the same limits.
|
|
|
|
## Fixed profile
|
|
|
|
* Load: closed-loop POST /api/v1/chats/{id}/messages through the LB, unique
|
|
Idempotency-Key and unique text per request, 32 Go workers,
|
|
5 s warmup, 20 s measured.
|
|
* Total API budget: 0.8 vCPU and 512 MiB, split 1x0.8 / 2x0.4 / 4x0.2 and
|
|
1x512M / 2x256M / 4x128M. PostgreSQL, Valkey and HAProxy limits are fixed.
|
|
* The whole stand runs on one Colima VM with 2 vCPU and 2 GiB RAM. The sum of
|
|
CPU limits is 1.8 cores in the fixed profile and 1.9 in the growth profile.
|
|
These are process-level replicas on one kernel, not independent nodes.
|
|
|
|
## Resource-growth series
|
|
|
|
At 4 replicas, total API CPU grows 0.4 -> 0.7 -> 0.9 vCPU (0.1/0.175/0.225 per
|
|
replica), RAM stays 512 MiB total, DB/Valkey/LB limits stay fixed. This
|
|
separates "HTTP capacity is the limit" from "shared DB is the limit".
|
|
|
|
## Improvement
|
|
|
|
At the saturated 4x0.2 profile the baseline image uses an explicit transaction,
|
|
a message INSERT and an idempotency INSERT (three SQL statements plus
|
|
BEGIN/COMMIT). The final image uses one data-modifying CTE that inserts the
|
|
message and the idempotency row atomically; a concurrent unique conflict aborts
|
|
the whole statement and is replayed. The client, profile and limits are
|
|
identical. Reported improvement is the median of 3 alternating runs.
|
|
|
|
## Metric sources and units
|
|
|
|
| Field | Source | Unit / meaning |
|
|
| --- | --- | --- |
|
|
| successful_rps | loadgen JSON | HTTP 2xx acknowledgements / measured seconds; not COMMIT count |
|
|
| successful_latency_ms | loadgen JSON | per-request wall time for successful requests, p50/p95/p99 |
|
|
| api_used_cores | cgroup v2 cpu.stat usage_usec delta / window | cores actually consumed by all API containers |
|
|
| api_throttled_seconds | cgroup v2 throttled_usec delta | CPU throttling, not load average |
|
|
| db_used_cores | cgroup v2 cpu.stat of PostgreSQL | cores consumed by the DB container |
|
|
| db_throttled_seconds | cgroup v2 throttled_usec of PostgreSQL | evidence of the DB CPU quota being hit |
|
|
| commits_per_second | pg_stat_database.xact_commit delta / window | DB transactions, includes auth/chat metadata |
|
|
| pool_waits_per_acquire | pgxpool.Stat EmptyAcquireCount delta / AcquireCount delta | fraction of pool acquisitions that had to wait |
|
|
| lb_qmax, lb_5xx | HAProxy stats CSV | queue maximum and 5xx on the public frontend |
|
|
| buffer_hit_ratio | pg_stat_database blks_hit/(hit+read) | shared-buffer locality |
|
|
| memory | cgroup v2 memory.current | cgroup working set, not process RSS |
|
|
|
|
## Limitations
|
|
|
|
* One Docker VM and one DB instance: no node failover, no DB replication.
|
|
* Closed-loop load has coordinated omission; the loadgen reports only
|
|
successful-request latency and separately counts failures.
|
|
* The load generator and the stack share the same 2-vCPU host; client cost is
|
|
visible and is part of the measured system.
|
|
* Absolute RPS is local and is not a production SLA.
|
|
|