Repository navigation
Open
Description
Background
As our system scales, we need to ensure stability and performance under high load. Current architecture is async-first (FastAPI + aiohttp + asyncpg), which is a solid foundation — but we have no baseline benchmarks to understand where the actual bottlenecks are.
Principle: Measure first, optimize second.
Phase 0: Benchmark & Identify Bottlenecks
Before any optimization, we need to establish baseline performance and find real bottlenecks under load.
0.1 — Load Testing Setup
- Set up load testing tool (e.g. Locust or k6 or vegeta)
- Write test scenarios:
- Scenario A: Non-streaming
/v1/chat/completions(baseline throughput) - Scenario B: Streaming
/v1/chat/completions(connection hold time) - Scenario C: Mixed traffic (70% streaming, 30% non-streaming)
- Scenario D: Burst traffic (sudden 10x spike)
- Scenario A: Non-streaming
- Use a mock LLM backend (fixed latency, e.g. 200ms) to isolate our system's overhead from provider latency
0.2 — Metrics to Collect
- Throughput: Max QPS before errors/timeouts appear
- Latency: P50 / P95 / P99 at various QPS levels
- Error rate: 5xx, timeouts, connection refused at saturation
- Resource usage: CPU, memory, open file descriptors, open connections
- Event loop lag: Are we blocking the async loop anywhere?
- DB pool utilization: How many connections are in use vs available?
- Connection pool behavior: aiohttp connector stats under load
0.3 — Stress Test Targets
| Test | What We're Looking For |
|---|---|
| Ramp up 0 → 1000 QPS | Where does latency start degrading? |
| Sustained 500 QPS for 10 min | Memory leaks? Connection leaks? |
| Kill a provider mid-test | Does circuit breaker work? How fast is failover? |
| 4 workers vs 1 worker | Does multi-worker actually help or cause issues? |
| Streaming with slow consumer | Do connections pile up? |
Phase 1: Fix Issues Found in Benchmarks
Based on benchmark results, address the actual bottlenecks. Potential candidates (to be validated):
- HTTP connection pool limits (
TCPConnector(limit=0)— is this actually a problem under load?) - Streaming idle timeout (do connections actually leak?)
- Lock contention in RouteExecutor (does it show up in profiling?)
- Rate limiter SQLite performance (is it a bottleneck with real traffic?)
- Circuit breaker cross-worker consistency (does it cause real failed requests?)
Phase 2: Architectural Improvements (If Needed)
Only pursue these if Phase 0/1 data shows they're necessary:
- Redis for distributed state (rate limiting, circuit breaker, health)
- Backpressure control
- Request priority queues
- pgbouncer / connection pooling optimization
Success Criteria
- We have a reproducible benchmark suite that can run in CI
- We know our baseline QPS and breaking point
- Optimizations are data-driven with before/after comparisons
- No regressions on P99 latency after changes
Metadata
Metadata
Assignees
Labels
No labels
Activity