Client-server and the load balancer
A load balancer spreads requests across several identical instances of a service and stops sending traffic to unhealthy ones through health checks. Services behind it must be stateless: state lives in a shared store, not in process memory, otherwise a request that lands on another instance returns, for example, 404. QA separately tests an instance failing, mixed versions during a rolling deploy and load balancer timeouts.
System design · Backend
The first request created a payment draft, the second got a 404. What changed between them?
A test creates a payment draft and immediately reads it by id. Locally everything works, but on a staging with three instances of the service about two reads out of three return 404. The code changed nothing between the two calls. The instance that handled them changed: the draft sat in the memory of the first one, and the read landed on the second. This is a typical bug when QA moves from a single process to a system behind a load balancer. The lesson covers how this scheme is built and what to test in it.
How it works and why. One instance of a service has a throughput limit and is a single point of failure. So several identical instances are run and a load balancer (LB) is put in front of them to distribute requests. An LB works at L4 (by TCP connections) or L7 (understands HTTP, can route by path or header). The algorithms differ: round robin in turn, least connections to the least loaded, a hash of IP or key so one client lands on the same instance. Besides distribution an LB runs health checks: it periodically probes an instance and stops sending traffic to it if it is unhealthy.
The path of a request through the load balancer
Stateless versus sticky. A stateless service keeps no client state in process memory: the session is in a token or a shared store. Then any instance handles any request and the service scales easily. Sticky sessions bind a client to one instance. This is simpler for stateful logic, but when the instance dies the session is lost and the load is distributed unevenly.
Example: checking that the state is not in memory. The faulty code keeps drafts in process memory:
// before: works with one instance, breaks behind an LB
const drafts = new Map<string, Draft>();
app.post('/drafts', (req, res) => {
const id = randomUUID();
drafts.set(id, req.body);
res.status(201).json({ id });
});
The test must make requests land on different instances. It helps if the service returns a service header with an instance identifier (here a hypothetical x-served-by; whether one exists depends on the project):
test('a draft is readable from any instance', async ({ request }) => {
const created = await request.post('/api/drafts', { data: { amount: 30 } });
const { id } = await created.json();
const instances = new Set<string>();
for (let i = 0; i < 20; i++) {
const res = await request.get('/api/drafts/' + id);
expect(res.status()).toBe(200);
instances.add(res.headers()['x-served-by'] ?? 'unknown');
}
// proves that the requests really went to different instances
expect(instances.size).toBeGreaterThan(1);
});
The fix is a shared store: the draft lives in the database or Redis and any instance finds it. The last line of the test matters: without it a green result may mean that all twenty requests went to the same instance.
State in memory versus a shared store
A Map in the process
The draft exists only on instance 1. A request to instance 2 gives 404, and when instance 1 dies the data is gone.
A shared store
The draft is in the DB or Redis. Any instance sees it, and an instance can be replaced without losing data.
What else to test.
- An instance dying mid-flow. The user is in the middle of a scenario: can they continue on another instance without logging in again.
- Rolling deploy. During an update some instances run the new version and some the old, and requests mix. The new version must read data written by the old one and vice versa, so database schema changes are made in two steps (expand first, contract later).
- LB timeouts. A long request (a report, an export) may be cut at the LB before the service answers: many balancers have an idle timeout, for example 60 seconds by default in AWS ALB. Check the value on the project.
- Health check. Does the LB take out an instance that responds badly, and put it back once it recovers.
Limits and alternatives. A Playwright or other HTTP client may keep one connection open, and an L4 balancer will send all requests to the same place: to hit different instances, new connections are made. Sticky sessions are sometimes justified, for example for WebSocket, but then instance failure is handled explicitly. For several regions another layer is added: DNS or a global balancer between regions, and the failure of a whole region has to be tested too.
Common mistakes.
- Testing on only one instance: state bugs do not show.
- Not checking that the requests were really distributed: a green test proves nothing.
- Forgetting mixed versions during a rolling deploy.
- Ignoring LB timeouts for long operations: on a stand without an LB everything works.
Check yourself
A test creates a draft and reads it: green on one instance, sometimes 404 behind an LB with three. What does it mean and where to look for the bug?
Answer
The draft state lives in the memory of an instance and not in a shared store. A request to another instance does not see it. Look for code that keeps data in a process variable (a Map, a local cache) and move it to the DB or Redis.
Why must the new version of a service work with the old data schema during a rolling deploy?
Answer
During the update both versions run at the same time and share one DB. If the new version requires a new column the old ones do not fill, or a migration drops a field the old ones still read, part of the requests will start failing. So changes are made in two steps: first add the compatible part, and remove the old only after the update is complete.