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 Distributed System Cycle: Performance -> Fault Tolerance -> Consistency -> Performance

The classic example works like this:

More generally, for any distributed system:

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.

PACELC: Availability vs Consistency and Consistency vs Latency

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.