Lesson 8 of 9 · 24m
Load Testing and Capacity Planning That Survive Contact With Reality
Build production-shaped load tests, find knee points safely, and turn measurements into capacity margins instead of benchmark theater.
A load test that replays one URL with one anonymous user tells you how fast one unrealistic request can be. Production traffic is a mixture: logged-in users, bots, mobile clients, cold caches, writes, background jobs, and occasional pathological input. Capacity planning starts with modeling that mixture closely enough to expose the knee where latency stops degrading gracefully.
Model traffic as a system
scenarios:
browse_catalog:
weight: 62
think_time: 1.5s
search:
weight: 18
think_time: 3s
checkout:
weight: 4
think_time: 5s
bot_static_assets:
weight: 16Include cache hit ratios close to production, realistic session lifetimes, and representative payload sizes. If the write path depends on a payment sandbox, either isolate it or use a controlled mock; do not let a third-party sandbox become the supposed limit of your database.
Drive load from outside the machine under test
wrk is useful for simple HTTP ceilings, k6 is better for scripted behavior and metrics:
import http from 'k6/http';
import { sleep } from 'k6';
export const options = {
stages: [
{ duration: '2m', target: 100 },
{ duration: '5m', target: 300 },
{ duration: '2m', target: 0 },
],
};
export default function () {
http.get('https://staging.internal/catalog?page=2');
sleep(1);
}Run the load generator on separate hosts. Watch not just response time but FPM busy workers, listen queue, CPU steal, memory, database replication lag, Redis latency, and error rate.
Find the knee, then back away from it
The interesting output is not maximum requests per second. It is the point where p95 latency or error rate begins rising sharply. Plot throughput against latency and resource saturation. If 800 rps is the knee, plan normal operations far below it — often 30-50% depending on traffic variability, deploy frequency, and failure tolerance.
Capacity includes failure modes. If you need three nodes for normal traffic and one can die, you need four plus margin. Autoscaling reacts after demand rises; it cannot retroactively prevent a queue explosion.
Test deploy-shaped imperfections
Run tests while background workers process jobs, caches warm, and nodes rotate. Many systems pass sterile benchmarks then degrade every Tuesday because deploys reset OPcache, rebuild assets, and restart queue workers at the same moment.
Field notes
- Never test against production write paths unless the test is explicitly designed and approved for production.
- A sudden perfect plateau can mean the load generator saturated before the target did. Monitor both sides.
- Use percentiles and error budgets; averages hide queueing collapse. Record the exact artifact, configuration, database size, and seed data so the test is reproducible.
Was this lesson helpful?