QA Interview Hub · Knowledge base · Quizzes · Українська

System Design › Scaling and reliability

Vertical and horizontal scaling

Vertical scaling is a bigger machine, horizontal scaling is more instances behind a load balancer. The first has a ceiling and stays a single point of failure; the second requires a stateless service, otherwise state in the memory of one instance breaks requests on another. Scaling is verified with load, stress, spike and soak tests on a realistic data volume, judged by p95, p99 and error rate.

System design · Scaling

On staging there are two instances and login suddenly "forgets" the user. Why?

A service worked flawlessly on one machine. It was deployed as two instances behind a load balancer and some users started falling out of their session: a request lands on an instance that knows nothing about them. Scaling changes not only speed but the class of possible bugs itself. The lesson covers two ways to add capacity, what they demand from a service, and how QA verifies that the system really scales.

How it works and why

Vertical scaling: a bigger machine (CPU, memory). No code changes, but there is a ceiling, the price grows faster than the capacity and the machine stays a single point of failure. Horizontal: more instances behind a load balancer. There is almost no ceiling and one instance failing does not take the service down, but the service must be stateless: everything needed to answer is in the request or in shared storage (database, cache, session store), not in process memory. Otherwise two instances see different "states of the world".

Two ways to grow

Vertical

A bigger machine

Simple, no code changes. A ceiling and a single point of failure.

Horizontal

More instances

Almost no ceiling, more resilient. Needs stateless, shared state, a balancer.

Figure 1. Both are legitimate; the choice depends on load and code.

Autoscaling adds or removes instances based on a metric: CPU, request count, queue length. Nuances tests catch: a new instance does not start instantly (warm-up, health check), and a database almost always scales worse than services, so the bottleneck simply moves there.

Example: flaky login on two instances

One instance:  POST /login -> 200, GET /me -> 200
Two instances:  POST /login -> 200 (A), GET /me -> 401 (B)

The session is stored in the memory of instance A and the balancer sent the next request to B. On one machine this does not reproduce. Diagnosis: repeat the same sequence several times and check which instance answered (a header or a log). The right fix is to move sessions to shared storage or switch to tokens the server can verify on its own. The temptation to "enable sticky sessions" hides the problem: when an instance dies the user is logged out anyway.

What QA tests

  • Load test: the expected load, p95 and p99 latency, error rate.
  • Stress test: where and how the system breaks: degrades gradually or falls over completely.
  • Spike test: a sudden peak (a sale, a payout at 9:00): does autoscaling keep up.
  • Soak test: hours of load: memory and connection leaks.
// k6
export const options = {
  scenarios: {
    peak: {
      executor: 'ramping-arrival-rate',
      startRate: 10, timeUnit: '1s',
      preAllocatedVUs: 100, maxVUs: 400,
      stages: [{ target: 200, duration: '2m' }, { target: 200, duration: '5m' }],
    },
  },
  thresholds: {
    http_req_duration: ['p(95)<500', 'p(99)<1200'], // ms
    http_req_failed: ['rate<0.01'],
  },
};

Thresholds turn the test into an automatic check: if p95 goes above 500 ms the run fails. The arrival rate model keeps the set flow of requests even when the system slows down (as long as there are VUs: maxVUs, without it the ceiling is preAllocatedVUs), and is therefore more honest than a fixed number of virtual users.

Limits and common mistakes

  • Load on staging with an empty database: a query fast on 1000 rows is slow on 50 million. You need a realistic data volume and profile.
  • The load generator becomes the bottleneck itself: a weak machine running k6 measures itself, not the service.
  • Looking at the average: it hides the tail. Look at p95 and p99.
  • Testing only services and forgetting the database, where scaling is harder.

Check yourself

After moving to two instances random 401s appeared. What is the hypothesis and how to check it?

Answer

Session state lives in the memory of one instance. Repeat login and requests several times and watch which instance answered: the 401 comes from a different one than issued the session.

Why does a load test on an empty database give false confidence?

Answer

Query time depends on data volume and indexes. On small data even a full scan is fast, and in production it becomes the bottleneck.

In an interview. Junior: Vertical scaling is simpler but has a ceiling. Horizontal gives resilience but requires stateless services. Middle/Senior: I verify scaling with load, stress, spike and soak tests on a realistic data volume, look at p95, p99 and error rate, and put thresholds into the test itself. Separately I hunt for bugs that appear only with several instances: in-memory state, local caches, sticky sessions, and watch whether the database became the bottleneck.

More articles in this chapter