Skip to content

Scalability: coping with more

People say a system is “scalable” the way they say a car is “fast”: as if it were a fixed property. It is not. Scalability is how well a system copes as its load grows, and what it costs to keep up.

That makes it a question with two parts, and you cannot answer it without both.

  1. With the same resources, what happens to performance as load grows? Latency rises, errors appear, something saturates.
  2. If you add resources, how much more load can you carry? If you double the resources, can you carry double the load?

The first part needs a measure of load. Martin Kleppmann calls it a load parameter, and what it is depends on the system: requests per second for a web service, the share of reads for a database, the number of active users in a chat room, the number of followers for a social feed. A system can scale well for one load parameter and badly for another.

Mental model. Scalability is a curve, not a label. On one axis is load, on the other is the cost or the resources needed to keep performance acceptable. A system is scalable for a given load, over a given range, if the line stays reasonable.

If doubling the resources doubles the load you can carry, scaling is linear. That is the ideal, and it happens when requests are independent: each request needs a bit of CPU, and nothing is shared between them.

Real systems tend to fall short of it. Doubling the resources gives less than double the capacity when something is shared: a database that every instance asks, a lock, a queue with one consumer. In the worst case, adding more makes things worse, because the instances spend their time coordinating with each other.

The practical question is always: what is shared, and does it scale? Almost every real limit is a shared thing that somebody forgot.

Try it: scale the API, and see what happens

Section titled “Try it: scale the API, and see what happens”

The lab below is a small web system: a load balancer, API instances, a cache, and a database. The database was sized for far less traffic than the API can take, which is common: the API is stateless and easy to copy, and the database is not.

Start with the default settings, 12,000 requests per second. The API is at 25% of its capacity and the database at 46%: everything is comfortable. Now change one thing at a time.

Requests pass a load balancer to three API instances. The API reads through a cache and falls back to a database whose connection pool is sized for far less traffic than the API can take. Change the traffic, the number of instances, the cache hit rate, and the database connections, and watch the cause and effect play out.

12,000 req/s
API instances
3
90%
40
0.3 ms0.2 ms0.5 ms0.5 msClientsHealthy<1%Load balancerHealthy<1%APIHealthy25%CacheHealthy14%DatabaseHealthy46%
0.3 ms0.2 ms0.5 ms0.5 msClientsHealthy<1%Load balancerHealthy<1%APIHealthy25%CacheHealthy14%DatabaseHealthy46%

Each dot stands for many requests. The numbers show the real rate.

No bottleneck: every component has headroom.

Key figures

Traffic
12,000 req/s
CPU
25%HealthyAPI utilisation
Memory
20%Healthy404 MB of 2,048 MB per API instance
Latency
p50 4.5 msp95 19 msp99 28 msHealthyWhole request, all requests
Error rate
0%HealthyRequests that fail
Database load
46%HealthyDatabase utilisation
Cache load
14%HealthyCache utilisation

Cause and effect

Each step leads to the next. A step that is changing right now is marked.

0 ms
Metrics
ComponentStateUtilisationOffered (req/s)Served (req/s)Rejected (req/s)p50p95p99
ClientsHealthy<1%12K12K00 ms0 ms0 ms
Load balancerHealthy<1%12K12K00.03 ms0.15 ms0.23 ms
APIHealthy25%12K12K02.8 ms12 ms18 ms
CacheHealthy14%11K11K00.14 ms0.6 ms0.92 ms
DatabaseHealthy46%2.3K2.3K05.5 ms24 ms37 ms
Traffic
12,000 req/s
Successful
12,000 req/s
Error rate
0%
Mean latency
6.3 ms

Illustrative values, not benchmarks of any real product.

Model notes

Assumptions

  • Requests arrive at random, as a Poisson process, at a constant average rate.
  • Each node is one queue, served first-come first-served by several identical slots (instances × concurrency).
  • Service times are random with the stated mean; by default they are exponentially distributed.
  • Each node is analysed separately, with the traffic it receives treated as Poisson.
  • Nine in ten requests are reads. The cache answers the share set by the hit rate; the rest, and every write, go to the database.

Simplifications

  • Above capacity there is no steady state. The model switches to a fluid approximation: the node serves at capacity, its queue grows until it reaches its limit, and further requests are rejected.
  • Percentiles assume an exponential tail. A service-time variability other than exponential changes the mean wait but not the shape of the tail.
  • A node frees its slot as soon as it has done its own work, before waiting on the services it calls. Real thread-per-request servers hold the slot, so a slow dependency exhausts them faster than this model shows.
  • A request calls its dependencies one after another. Parallel fan-out is not modelled.
  • When a caller times out and gives up, the callee still finishes the work, so abandoned requests keep consuming capacity.
  • Retries add load. Their backoff and jitter decide when that load arrives, which only the per-request simulation shows.
  • Retry feedback is solved from the healthy starting point, so it finds one stable outcome. Systems that stay stuck in overload after the cause is gone are not shown.
  • A throughput limit (for example maximum writes per second) is modelled as slower service, not as a separate rate limiter.
  • A caller’s timeout counts the mean time its dependencies take, not their full distribution.
  • Latency percentiles for a whole request are estimated, over every request, including those that are rejected and fail quickly. The mean is exact, and the percentiles come from a gamma distribution that has that mean and the variance of the time spent in each component.
  • Memory is modelled simply: a fixed base for each instance, plus a fixed amount for every request the instance holds, whether it is being served or waiting.
  • Each database connection serves one operation at a time, about 8 ms each, so 40 connections can serve about 5,000 operations per second. Reads and writes cost the same here; real databases treat them differently, and writes are usually the scarcer resource.

Parameter values are illustrative. They are not benchmarks of any real product.

  1. Raise the traffic to 40,000 requests per second. The API is at 83%, which is busy but fine. The database is at 152% of its capacity and about 6% of requests fail. The API is not what gives way first.
  2. Raise the traffic to 60,000, with 3 API instances. The API is now at 125% of its capacity, the database is at 182%, and 26.7% of requests fail. The API looks like the problem, because it is the one with an obvious fix: add instances.
  3. Add a fourth API instance. The API drops to 94%, and failures fall to 10.4%. Adding capacity to a bottleneck worked.
  4. Keep adding API instances, up to eight. Failures stay at 10.4%. Nothing changes. The API is now at 47%, with lots of room, and the system is no better, because the limit has moved.
  5. Raise the database connections from 40 to 120. Failures fall to 0%.

Step 4 is the point of the lesson. After step 3, the API had stopped being the limit, and the database, at 228% of its capacity, was the one that set it. Adding API instances after that was money spent on the wrong component.

There are two ways to add capacity.

  • Scaling up (vertical scaling) means a bigger machine: more CPU, more memory. It is simple, because nothing in the design changes, and it has a ceiling: the biggest machine there is, and a price that rises faster than the capacity.
  • Scaling out (horizontal scaling) means more machines. It has no such ceiling, but it only works when the work can be split, which is why stateless services are easy to scale out and databases are hard. The scalability module covers both in detail.

AWS’s reliability guidance gives a second reason to prefer many small resources over one large one: if one fails, less is lost. Scaling out is a scalability technique and an availability technique at once, which is the subject of the next lesson.

What happens if traffic doubles overnight?

Section titled “What happens if traffic doubles overnight?”

The system above is running at 20,000 requests per second, with the API at 42% and the database at 76%. A marketing campaign doubles the traffic to 40,000 tomorrow.

A quick look at the API says there is no problem: 42% becomes 83%, which is busy but fine. The team does not add capacity. At 40,000 the database is at 152%, and about 6% of requests fail on the day of the campaign.

The lesson is about how to look. The API’s utilisation was the figure everyone watched, and it was fine. The system’s capacity was set by the component with the highest utilisation after the change, and that was another one. Before a load increase, work out the load on every component, not only the one that scales easily. The calculation is not difficult. Each request has a path, and each component on the path sees some fraction of it.

  • Scalability is how well a system copes as load grows and what it costs to keep up. It is a relationship, not a label.
  • Always name the load parameter: requests per second, data size, users, fan-out.
  • Linear scaling means double the resources, double the load. Real systems fall short when something is shared.
  • The component with the highest utilisation sets the capacity of the whole system.
  • Adding capacity to a part that is not the bottleneck changes little, and relieving a bottleneck reveals the next one.
  • To predict a load increase, calculate the load on every component on the request path.

Next: availability, and what each nine costs.

Your progress

Built by GhaziAlibi.

© ShardCraft. All rights reserved.