System Design: 3 – Scalability Explained Simply

In Part 1 we built an over-the-air (OTA) update backend for a million cars out of standard building blocks. In Part 2 we watched it struggle at 8 a.m. and learnt how to find the bottleneck. This last part asks the question every successful system eventually faces: what happens when the fleet grows to ten million cars, in several regions, and the carmaker wants two campaigns a month instead of two a year?

“Scalability” is often used as if it meant “fast”. It does not. A system is scalable if it can handle more load by adding resources, at a cost that grows roughly in proportion to the load, without a redesign. Fast at one million and impossible at ten million is not scalable.

This post covers the main tools in plain words: scaling up versus scaling out, keeping servers stateless, partitioning data across machines, consistent hashing, and the uncomfortable trade-off at the centre of every distributed system: consistency. As before, the system and its numbers are illustrative, and I point to ByteByteGo and the original papers at the end.

Scaling up or scaling out

There are only two fundamental ways to give a system more capacity.

Vertical scaling (scaling up) means a bigger machine: more cores, more memory, faster disks. It is the easiest option by far. Nothing in the software changes, there is still one database and one copy of the data, and transactions keep working exactly as before. Its limits are just as clear: the biggest machine you can buy is finite, prices rise steeply near the top of the range, upgrades often mean downtime, and one big machine is still one machine that can fail.

Horizontal scaling (scaling out) means more machines of the same size, sharing the work. In principle it has no upper limit, it uses cheap commodity hardware, and losing one machine out of fifty is a non-event. The price is paid in software: the work and the data must be split, the machines must coordinate, and every problem from Part 2 (partial failures, timeouts, retries) now applies inside your own system.

Vertical and horizontal scaling · core counts illustrative
Vertical and horizontal scaling · core counts illustrative

The honest answer is that good systems use both. In our OTA backend, the stateless application servers scale out trivially, because any server can answer any car. The primary database was scaled up for as long as possible, because scaling a database out is hard. At ten million cars, that day has come, and the rest of this post is about what it takes.

An embedded analogy: a faster microcontroller is vertical scaling; splitting functions across several ECUs connected by CAN is horizontal scaling. The second gives you more room, but now you need a communication matrix, timeouts and a plan for when a message is lost.

Statelessness, revisited: where the state goes

Part 1 said that application servers should be stateless. It is worth being precise about what that means, because “stateless” does not mean the system has no state. It means the state lives somewhere that every server can reach, so that no single server is special.

In an OTA backend there are three kinds of state, and each goes to a different home:

  • Request state lives only for one check-in: the car’s VIN, its current version, the answer being built. It stays in the server’s memory for a few milliseconds and is gone. This is fine.
  • Session state spans several requests: an authenticated session, a download that resumes, a multi-step install report. It must not live in one server’s memory. It goes into the request itself (a signed token the car presents every time) or into a shared store such as a cache cluster.
  • Durable state must survive everything: which software each car has, which campaigns exist, what each car reported. It goes into the database.
Three kinds of state in the OTA backend and where each one lives
Three kinds of state in the OTA backend and where each one lives

The tempting shortcut is sticky sessions: the load balancer always sends the same car to the same server, so that server can keep the car’s session in memory. It works until that server is restarted, scaled down or overloaded, and then every car it was holding loses its session at once. Sticky sessions turn stateless scaling back into stateful scaling, with all of its problems, so use them only as an optimisation, never as the place where truth lives.

Once the servers are truly stateless, scaling them is a solved problem. The hard part, and the rest of this post, is the state itself.

Scaling the data: partitioning by VIN

Replication, from Part 1, copies the same data to several machines. It scales reads, but every replica still holds everything, and every write still goes through one primary. With ten million cars sending install reports, diagnostics and status updates, the primary runs out of write capacity and disk long before the replicas run out of read capacity.

The next step is partitioning, usually called sharding: split the data into pieces and give each piece to a different database. Each shard holds only its share of the vehicles, takes only their writes, and can have its own replicas. Ten shards give you roughly ten times the write capacity and storage.

Rows of server racks with network cables in a data centre
Sharding splits the data across many machines. Photo by Taylor Vick on Unsplash

The question is how to decide which car lives on which shard. You need a shard key, a field that every request carries and that spreads the load evenly. For an OTA backend the obvious candidate is the VIN: every check-in includes it, almost every query is about one car, and there are millions of distinct values. A routing layer computes the shard from the VIN and sends the query there.

Sharding the vehicle database by VIN · illustrative
Sharding the vehicle database by VIN · illustrative

Sharding has real costs, and it is worth knowing them before you start:

  • Queries across shards get expensive. “Which cars on campaign 4.3 failed?” now has to ask every shard and merge the answers. Such questions often move to a separate analytics store fed by the message queue.
  • Transactions across shards are hard. Anything that must change two cars atomically now spans two databases. Good shard keys make this rare.
  • Rebalancing is a project. Adding shards means moving data while the system is running, which is where the next two sections come in.

Choosing a shard key

“Shard by VIN” still leaves a decision: how exactly does a VIN map to a shard? There are three common strategies, and the choice matters more than almost anything else in the data design.

  • Range partitioning. Shard 1 holds VINs from A to C, shard 2 from C to F, and so on. Range queries are easy and the layout is human-readable. The danger is that real keys are not evenly spread. For a single carmaker, the first characters of every VIN are the same manufacturer code, and the serial numbers at the end grow over time. Ranges therefore tend to line up with model years, and the newest cars, which are driven most and receive the most updates, all land on the last shards.
  • Hash partitioning. Compute a hash of the VIN and use it to pick the shard. A good hash scatters similar keys across all shards, so the load is almost perfectly even. The cost is that neighbouring VINs end up far apart, so range queries must ask every shard.
  • Partitioning by region or tenant. All cars registered in one market live together. This keeps data close to its users and makes data-residency rules easier to meet, but markets are very different in size, so the biggest one becomes the hot shard.

The chart below shows how uneven range partitioning can get in our illustrative fleet, compared with hashing.

Illustrative load per shard for range and hash partitioning
Illustrative load per shard for range and hash partitioning

A related trap is the hot key: a single record that everyone reads or writes. In an OTA backend, the row for the current campaign is read by every car that checks in. No shard key spreads one row; the answer is the cache from Part 1, or several copies of the hot value.

Consistent hashing: adding a shard without moving everything

Hash partitioning has a hidden problem. The simplest version computes hash(VIN) mod N, where N is the number of shards. That works until you add a shard. Change N from 10 to 11 and almost every VIN now maps to a different shard: on average 10 out of 11 cars, about 91 % of all the data, must move. For a database with ten million vehicles, that is not a maintenance window; it is a migration project.

Consistent hashing, introduced by David Karger and colleagues in 1997 for web caches, fixes this. Picture the hash space as a ring. Each shard is placed at one or more points on the ring, and each VIN belongs to the first shard found going clockwise from the VIN’s own hash. When a new shard joins, it takes over only the keys between itself and its neighbour; every other key stays where it is. Going from 10 to 11 shards moves only about one eleventh of the data, around 9 %.

Consistent hashing on a ring · simplified
Consistent hashing on a ring · simplified

Two refinements make it work well in practice:

  • Virtual nodes. Each physical shard is placed at many points on the ring, not one. This evens out the load, and lets a bigger machine take a bigger share simply by owning more points. Amazon’s Dynamo paper from 2007 popularised this approach.
  • Replication along the ring. Each key is also copied to the next one or two shards clockwise, so losing a machine does not lose data.

The chart below compares the share of data that moves when shards are added, with naive modulo hashing and with consistent hashing. Many databases and caches use consistent hashing or a close relative internally, so you will rarely implement it yourself, but knowing why it exists explains why adding capacity to them is cheap.

Computed share of data that moves when one shard is added
Computed share of data that moves when one shard is added

Why consistency is hard: CAP and PACELC in plain words

As soon as the same data lives on more than one machine, replicas or shards in several regions, a question appears that a single database never had to answer: if two copies disagree, which one is right, and what should a reader see in the meantime?

The CAP theorem is the usual starting point. Stated simply: when the network between two copies breaks (a partition), a system must choose between consistency (every read sees the latest write, or gets an error) and availability (every request gets an answer, even if it may be stale). It cannot promise both while the copies cannot talk.

CAP is often quoted as “pick two of three”, and Eric Brewer, who formulated it, wrote in 2012 that this framing was always misleading. Partitions are rare, so most of the time a system can be both consistent and available; the hard choice appears only during a partition, and it can be made differently for different data and operations.

Daniel Abadi’s PACELC adds the part that matters every day. If there is a Partition, choose Availability or Consistency; Else, in normal operation, choose Latency or Consistency. Even with a perfect network, keeping copies in sync costs time: a write that must be confirmed by a replica in another region waits for that round trip. So the real, everyday trade-off is usually speed against freshness, not availability against correctness.

In the OTA example: suppose the link between the European and the US data centre fails. A car in Germany checks in. The European side can answer from its own copy, quickly but possibly without the campaign pause an engineer just set in the US, or it can refuse until the link is back. Neither answer is free, and the right one depends on what the data is.

The OTA partition choice between the EU and US data centres
The OTA partition choice between the EU and US data centres

Which OTA data needs which consistency

The practical lesson from CAP and PACELC is that consistency is not a setting for the whole system. It is a decision for each kind of data, based on what goes wrong if a reader sees an old value. For our backend:

DataIf a reader sees an old valueChoice
Campaign stop or pauseCars keep installing a software version that engineers have just stoppedStrong: confirmed everywhere before it counts; if a region cannot confirm, it stops offering the update
Which software a car has installedThe car is offered an update it already has; it declinesEventual, with read-your-own-writes for the same car
Campaign success dashboardNumbers are a minute behindEventual is fine
Download package location (signed URL)A car is sent to a file that is still being copiedStrong for publishing, then immutable
Diagnostic and telemetry dataAnalysis runs on data a few minutes oldEventual, batch processing

Notice the pattern: the safety-relevant control data is small, rarely written and must be strong, so it can afford the latency. The bulk data is huge, written constantly and tolerates delay, so it gets the cheap, fast path. Most well-designed systems end up exactly like this, with a small strongly consistent core and a large eventually consistent periphery.

The campaign stop deserves the extra emphasis because it is a safety decision, not just a data decision. For an automotive engineer this will feel familiar: it is the backend equivalent of a safe state. When in doubt, the system should fail towards “do not install”, never towards “install anyway”.

Autoscaling and going multi-region

Two more tools complete the picture.

Autoscaling adds and removes stateless servers automatically, based on a metric such as CPU utilisation, request rate or queue length. It turns the 8 a.m. rush from Part 2 into a routine event: the platform sees the queue grow, starts more application servers and workers, and removes them again in the afternoon. Three practical notes:

  • Scale on the right signal. For the workers, queue length is a better trigger than CPU, because it measures the work that is waiting rather than the work being done.
  • New machines take time to be useful. Starting a server, warming its cache and opening connections can take minutes. For a campaign that starts at a known time, scale up before 8:00 rather than reacting at 8:05.
  • Autoscaling only helps stateless parts. It does nothing for the database bottleneck; that still needs the data design above.

Multi-region means running the backend in several geographic regions, for example Europe, North America and Asia. Cars talk to the nearest region, which cuts latency and keeps working if one region fails completely. It is also often a legal necessity, because vehicle data may have to stay in the market where it was collected.

The easy way to go multi-region is to treat each region as its own, mostly independent copy of the system, with each car’s data living in its home region. That is just partitioning by region at the largest scale. The hard way is a single global database with writes in every region, which brings back every CAP and PACELC question at intercontinental latencies. For an OTA backend, the easy way covers almost everything: only the small, strongly consistent control data, such as campaign definitions and stop orders, needs to be shared across regions.

What I take away

  • Scale up while you can, scale out when you must. A bigger machine is the cheapest scalability you will ever buy; sharding is the most expensive.
  • Stateless servers, stateful stores. Push every bit of state out of the servers, and the compute side of scaling becomes routine.
  • The shard key is the most important data decision. Pick a key that every request carries and that spreads load evenly; hash it unless you have a strong reason not to.
  • Plan for adding capacity on day one. Consistent hashing, or a database that uses it, turns adding a shard from a migration into a configuration change.
  • Consistency is chosen per piece of data, not per system. Keep a small, strongly consistent core for decisions that matter, and let the rest be eventually consistent and fast.
  • Fail towards the safe state. In an automotive backend, “not sure” must mean “do not install”.

This closes the series. Parts 1 to 3 cover the building blocks, the bottlenecks and the scaling trade-offs; together they are most of what you need to read a system design, ask the right questions about it, and recognise when an embedded instinct applies and when it does not.

Further watching and reading

Sources

The OTA backend, the fleet sizes, the shard loads and the consistency choices are illustrative, not a description of any carmaker’s system.

Cover photo by Denys Nevozhai on Unsplash.