Core Concepts

Scalability

Find the resource limiting growth, then decide whether a larger machine or more machines can take on the work.

Free~14 min

Scaling up gives one machine more capacity. Scaling out adds machines that can share the work. Start with the resource reaching its limit and ask which change would relieve that limit.

Two ways to add capacity

Vertical, or scaling upHorizontal, or scaling out
MoveGive one machine more capacityAdd machines and spread work
Typical exampleIncrease memory so more database reads can be served without touching diskRun more application instances and spread incoming requests
Main limitHow far one machine can grow, and what the upgrade costsHow much work can be separated and which resources the machines still share

Name the limiting resource first. For example, more CPU will help when the machine is spending all its compute on requests. A write path waiting on storage may need a change to the storage path or to the amount of data each write touches.

Stateless and stateful paths

A stateless application instance can handle a request using the request itself or data it can get from shared systems. The application can run more copies and use a load balancer to route requests to healthy instances. In a URL shortener, any instance that can look up the short code can handle a redirect. Creating links still sends writes to the same database, where a different resource can become the bottleneck.

A database stores state. To scale its work, say which operations can go to other machines.

  • To move reads, use a replica, a copy that receives updates from the primary. A primary is the database writer in this design. A replica can serve reads that can accept the version it has. Data Replication shows how it catches up.
  • To spread writes, use shards, separate parts of the data with their own writers. We can assign different short codes to different shards. A request uses its code to find the writer that owns that part of the data. Database Sharding explains how to choose the key.

Moving reads may free CPU or disk work on the primary. It does not change where a write lands or split the work of recording it. PostgreSQL documentation on load balancing and replication

A sequence of choices

Start by testing one writer with the expected reads and writes. If it has a capacity problem, identify what holds it back. A larger machine or a better query may give it enough headroom. If reads are the problem and they can tolerate a copy, replica reads or caching may help. If the write path still saturates, estimate how much work can run on independent shards.

The numbers in this walkthrough illustrate how one workload responds to different changes. They are assumptions about that workload and those machines. In your design, measure the path that matters and leave space for traffic to grow.

Limits of adding machines

If one popular code accounts for most requests, assigning codes to shards still sends that code's work to one place. A cache might take some of the reads. The design would need a different way to spread work if that one key also overwhelms the writer.

Requests that need many shards can make more network calls and wait for the slowest answer. Transactions touching several shards can require coordination between their writers. Add this cost when comparing one machine with several.

Availability can change the sequence of choices. One machine can be fast enough today and still fail the requirement to survive losing it. Extra machines need enough spare capacity to handle the traffic after a failure. Availability

Interview habit

  1. Name the resource that reaches its limit and what the traffic looks like at that moment.
  2. Say how a larger machine or another machine would relieve that resource.
  3. Explain how requests find the machine that can serve them.
  4. Check the remaining shared bottleneck and what happens when one machine fails.

See also

Used in

  • URL Shortener - adding application instances and cache shards expands the redirect path. Splitting the mappings expands write capacity when the database reaches its limit.
  • Group Chat with Threads - gateways share connection work while message writers own different sets of channels.
  • Online Chess - game servers divide work by game ownership so one process does not run the whole fleet's active games.

Done reading? Mark it so it sticks in your dashboard.

Discussion