Core Challenges of Distributed Systems
When you build a distributed system, there are three core challenges that need to be solved. I like to think of these challenges (especially when early in a system’s design) as a cycle.
The cyclical challenges of distributed systems
The classic example works like this:
-
We start by wanting to improve Performance. Perhaps we need to handle many times more requests per second than we currently do, our user count has increased by a factor of 10x, etc.
-
To do this, we introduce sharding. We’ll have many machines, and pass each request to one of these machines, allowing us to increase throughput of the system just by adding more machines.
-
However, this causes a problem. With just one machine, faults like hard disk failure, power issues, network blips, and so on, were very rare: you might run for years with no downtime. But when we start sharding requests to 1000 machines, even rare faults become common; almost certainly every week at least one of our machines will experience some kind of fault. So, we need to improve our Fault Tolerance.
-
To do this, we introduce replication. Whatever data, state, or context is necessary for our system to work properly, must be replicated at least once behind the scenes, otherwise our system will stop working altogether if the machine holding that data experiences a failure.
-
This introduces a new problem. As soon as we have multiple copies of our data or state in the system, we introduce the risk that these copies will go out of sync in various ways, which means the user of our system might see data that is old, incorrect, or corrupted. So we want to improve our system’s Consistency.
-
To do this, we introduce coordination. In order to have assurance that requests for data will behave consistently, the different parts of the system need to communicate in some way. This could be additional calls a client makes, coordination that happens behind the scenes between worker machines, and so on.
-
But, all this coordination introduces overhead, which impacts our Performance — and so the cycle begins again.
More generally, for any distributed system:
- Scaling out tends to cause more faults.
- Recovering from faults tends to require more redundancy.
- Enforcing consistency tends to make things slower.
Getting more formal with PACELC
The more formal version of this idea is the PACELC design principle. PACELC is an acronym:
If Partition, choose Availability or Consistency; Else, choose Latency or Consistency.
“Partition” here refers to network partition, a famously difficult fault for distributed systems, where some of your machines are unable to communicate with the rest of your machines (although they may still be accepting requests from clients). If this situation occurs, this principle states that you can guarantee you will always respond to a request (availability) or that your data will remain consistent, but not both.
When we aren’t experiencing this worst-case fault, we can expect some level of both availability and consistency, but now we must choose to prioritize either consistency or latency — in other words, in order to guarantee consistency, we must give up some of our performance.
Because this principle is academic, it focuses on the extreme ends of the system — things we can state are always true, or things that are impossible. That’s useful, but in general, I prefer to think in terms of “Performance” (which includes but is not limited to typical latency) and “Fault Tolerance” (which includes but is not limited to network partitions). Within the constraints of PACELC there are all kinds of distributed system designs that can give varying amounts of confidence in these 3 aspects of our systems.
Example Tradeoffs: Remote Build Caching
I’ve recently been working with remote build caching solutions for CI/CD, and there are interesting parallels there.
For languages like Swift and Kotlin, caching (for example the type done by Gradle) is very granular: almost every file can be a cache hit, and in a typical 10-minute compile you might make 5000 or more individual requests to the cache service. The value of a cache hit is the savings of a one-file compile, which might be somewhere in the 50-300ms range; therefore, the cost of every request to the cache needs to be relatively low (under 50ms) to be worth it.
For Swift in particular, using something like OmniCache (backing Xcode CAS), you might choose to intentionally fragment your cache. Instead of having 1 cache service, you may create endpoints specific to regions or even availability zones, called by build machines in the same region. In order to get this performance (i.e. reduced latency) for each call, you are willing to give up some consistency: a fragmented cache means you might get a cache miss even if a different machine recently cached the output you want. You’re also willing to give up fault tolerance (i.e. availability); if your region’s cache service goes down, all requests will be cache misses and you’ll compile everything as if there was no cache. But these downsides may be worth it to get sub-10ms average latency.
In contrast, if you are using a tool like Rush or NX and caching the output of TypeScript compilations, typically you are compiling an entire TypeScript project, minifying, bundling, then compressing the results and storing them in the cache. The value of a cache hit (and the cost of a cache miss) could be 30+ seconds, maybe even multiple minutes in some cases. Here, we would value the opposite: we are willing to pay extra in per-request latency if it means consistency (we are guaranteed to get a hit if any other machine has recently compiled the thing we want), and since we are fine with extra latency per request, we might as well also provide some fault tolerance (fall back to another region’s cache if ours is unavailable).
Take these examples with a grain of salt — “consistency” here is used loosely, since caches are keyed by hashes based on content, we are in no danger of returning a wrong or stale value, only a miss. And, by definition, our ability to compile anything we miss is fault tolerance; we can complete the job even if the entire remote build cache service is down (although we might do it much slower than normal). But the example illustrates how even in the same domain, two systems with the exact same purpose might have different demands on performance, consistency, and fault tolerance.