The Promise and Perils of Distributed Systems
The Limits of a Single Server
In this book, we will discuss distributed systems. But what exactly do we mean when we say “distributed systems”? And why is distribution necessary? Let’s start from the basics.
In today’s digital world, the majority of our activities rely on networked services. Whether it’s ordering food or managing finances, these services run on servers located somewhere. When using cloud services like AWS, GCP, or Azure, these servers are managed by the respective cloud providers. They store data, process user requests, and perform computations using the CPU, memory, network, and disks. These four fundamental physical resources are essential for any computation.
Consider a typical retail application functioning as a networked service, where users can perform actions such as adding items to their shopping cart, making purchases, viewing orders, and querying past orders. The capacity of a single server to handle user requests is ultimately determined by the limitations of four key resources: network bandwidth, disks, CPU, and memory.
The network bandwidth sets the maximum data transfer capacity over the network at any given time. For example, with a network bandwidth of 1Gbps (125MB/s) and 1KB records being written or read, the network can support a maximum of 125,000 requests per second. However, if the record size increases to 5KB, the number of requests that can be passed over the network decreases to only 25,000.
Disk performance depends on several factors, including the type of read or write operations and how well disk caches are used. Mechanical disks are also affected by hardware features such as rotational speed and seek time. Sequential operations usually have better performance than random ones. Moreover, the performance is influenced by concurrent read/write operations and software-based transactional processes. These factors can significantly affect the overall throughput and latency on a single server.
Figure 1.1 Resources of computation
Likewise, when the CPU or memory limit is reached, requests must wait for their turn to be processed. When these physical limits are pushed to their capacity, this results in queuing. As more requests pile up, waiting times increase, negatively impacting the server’s ability to efficiently handle user requests.
The impact of reaching the limits of these resources becomes evident in the overall throughput of the system, as illustrated in Figure 1.2 .
Figure 1.2 Drop in throughput with increase in requests
This poses a problem for end users. As the system is expected to accommodate an increasing user base, its performance actually degrades.
To ensure requests are served effectively, you have to divide and process them on multiple servers. This enables the utilization of separate CPUs, networks, memory, and disks to handle user requests. In our example, the workload should be divided so that each server handles approximately five hundred requests.
Separate Business Logic and Data Layer
A common approach is to separate an architecture into two parts. The first part is the stateless component responsible for exposing functionality to end users. This can take the form of a web application or, more commonly, a web API that serves user-facing applications. The second part is the stateful component, which is managed by a database ( Figure 1.3 ).
Figure 1.3 Separating compute and data
This way, most of the application logic executes on the separate server utilizing a separate network, CPU, memory, and disk. This architecture works particularly well if most users can be served from caches put at different layers in the architecture. It makes sure that only a portion of all requests need to reach the database layer.
As the number of user requests increases, more servers can be added to handle the stateless business logic. This scalability allows the system to accommodate a growing user base and ensures that requests can be processed efficiently. In the event of a server failure, a new server can be introduced to take over the workload and continue serving user requests seamlessly ( Figure 1.4 ).
Figure 1.4 Scaling compute with multiple servers
This approach is effective for many applications. However, there comes a point when the amount of data stored in stateful databases grows to hundreds of terabytes or even petabytes, or the number of requests to the database layer increases significantly. As a result, the simplistic architecture described above runs into limitations stemming from the physical constraints of the four fundamental resources on the server responsible for managing the data.
Partitioning Data
When a software system runs into hardware’s physical limits, the best approach to ensure proper request processing is to divide the data and process it on multiple servers ( Figure 1.5 ). This enables the utilization of separate CPUs, networks, memory, and disks to handle requests on smaller data portions.
Figure 1.5 Scaling data by distributing on multiple servers
A Look at Failures
When we utilize multiple machines with their own disk drives, network interconnects, processors, and memory units, the likelihood of failures becomes a significant concern. Consider the hard disk failure probability. If a disk has a failure rate of once in 1000 days, the probability of it failing on any given day is 1/1000, which may not be a major concern on its own. However, if we have thousands of disks, the probability of at least one disk failing on a given day becomes very high. If the partitioned data is being served from the disk that fails, it will become unavailable until the disk is recovered.
To gain insights into the types of failures that can occur look at the failure statistics from Jeff Dean’s 2009 talk [ Dean2009 ] on Google’s data centers as shown in Table 1.1 . Although these numbers are from 2009, they still provide a valuable representation of failure patterns.
Table 1.1 Failure Events per Year for a Cluster in a Data Center from Jeff Dean’s 2009 Talk [ Dean2009 ]
| Event | Details |
|---|---|
| Overheating | Power down most machines in < 5 min (~1–2 days to recover) |
| PDU Failure | ~500–1000 machines suddenly disappear (~6 hours to come back) |
| Rack Move | Plenty of warning, ~500–1000 machines powered down (~6 hours) |
| Network Rewiring | Rolling ~5% of machines down over 2-day span |
| Rack Failures | 40–80 machines instantly disappear (1–6 hours to get back) |
| Racks Go Wonky | 40–80 machines see 50% packet loss |
| Network Maintenances | 4 might cause ~30-minute random connectivity losses |
| Router Reloads | Takes out DNS and external VIPs for a couple minutes |
| Router Failures | Have to immediately pull traffic for an hour |
| Minor DNS Blips | Dozens of 30-second blips for DNS |
| Individual Machine Failures | 1000 individual machine failures |
| Hard Drive Failures | Thousands of hard drive failures |
When distributing stateless compute across multiple servers, failures can be managed relatively easily. If a server responsible for handling user requests fails, the requests can be redirected to another server, or a new server can be added to take over the workload. Since stateless compute does not rely on specific data stored on a server, any server can begin serving requests from any user without the need to load specific data beforehand.
Failures become particularly challenging when dealing with data. Creating a separate instance on a random server is not as straightforward. It requires careful consideration to ensure that the servers start in the correct state and coordinate with other nodes to avoid serving incorrect or stale data. This book mainly focuses on systems that face these types of challenges.
To ensure that the system remains functional even if certain components are experiencing failures, simply distributing data across cluster nodes is often insufficient. It is crucial to effectively mask the failures.
Replication: Masking Failures
Replication plays a crucial role in masking failures and ensuring service availability. If data is replicated on multiple machines, even in the event of failures, clients can connect to a server that holds a copy of the data.
However, doing this is not as simple as it sounds. The responsibility for masking failures falls on the software that handles user requests. The software must be able to detect failures and ensure that any inconsistencies are not visible to the users. Understanding the types of errors that a software system experiences is vital for successfully masking these failures.
Let’s look at some of the common problems that software systems experience and need to mask from the users of the system.
Process Crash
Software processes can crash unexpectedly due to various reasons. It could be a result of hardware failures or unhandled exceptions in the code. In containerized or cloud environments, monitoring software can automatically restart a process it recognizes as faulty. However, if a user has stored data on the server and received a successful response, it becomes crucial for the software to ensure that the data remains available after the process restarts. Measures need to be in place to handle process crashes and ensure data integrity and availability.
Network Delay
The TCP/IP network protocol operates asynchronously, meaning it does not provide a guaranteed upper bound on message delivery delay. This poses a challenge for software processes that communicate over TCP/IP. They must determine how long to wait for responses from other processes. If a response is not received within the designated time, they need to decide whether to retry or consider the other process as failed. This decision-making becomes crucial for maintaining the reliability and efficiency of communication between processes.
Process Pause
During the execution of a process, it can pause at any given moment. In garbage-collected languages like Java, execution can be interrupted by garbage collection pauses. In extreme cases, these pauses can last tens of seconds. As a result, other processes need to determine whether the paused process has failed. The situation becomes more complex when the paused process resumes and begins sending messages to other processes. The other processes then face a dilemma: Should they ignore the messages or process them, especially if they had previously marked the paused process as failed? Finding the right course of action in these circumstances is a challenging problem.
Unsynchronized Clocks
The clocks in the servers typically utilize quartz crystals. However, the oscillation frequency of a quartz crystal can be influenced by factors like temperature changes or vibrations. This can cause the clocks on different servers to have different times. Servers typically require a service such as NTP 1 that continuously synchronizes their clocks with time sources over the network. However, network faults can disrupt this service, leading to unsynchronized clocks on servers. 2 As a result, when processes need to order messages or determine the sequence of saved data, they cannot rely on the system timestamps because clock timings across servers can be inconsistent.
-
Network Time Protocol.
-
Even Google’s TrueTime clock machinery built using GPS clocks has clock skew. However, that clock skew has a guaranteed upper bound.
Defining the Term “Distributed Systems”
We will explore the common solutions to address the challenges posed by these failures. However, before we delve into that, let’s establish a definition for distributed systems based on our observations thus far.
A distributed system is a software architecture that consists of multiple interconnected nodes or servers working together to achieve a common goal. These nodes communicate with each other over a network and coordinate their actions to provide a unified and scalable computing environment.
In a distributed system, the workload is distributed across multiple servers, allowing for parallel processing and improved performance. The system is designed to handle large amounts of data and accommodate a high number of concurrent users. Most importantly, it offers fault tolerance and resilience by replicating data and services across multiple nodes, ensuring that the system remains operational even in the presence of failures or network disruptions.
The Patterns Approach
Professionals seeking practical advice need an intuitive understanding of these systems that goes beyond theory. They need detailed and specific explanations that help comprehend real code while remaining applicable to a wide range of systems. The Patterns approach is an excellent tool to fulfill these requirements.
The concept of patterns was initially introduced by architect Christopher Alexander in his book A Pattern Language [ Alexander1977 ]. This approach gained popularity in the software industry, thanks to the influential book widely known as the Gang Of Four [ Gamma1994 ] book.
Patterns, as a methodology, describe particular problems encountered in software systems, along with concrete solution structures that can be demonstrated by real code. One of the key strengths of patterns lies in their descriptive names and the specific code-level details they provide.
A pattern, by definition, is a “recurring solution” to a problem within a specific context. Therefore, something is only referred to as a pattern if it is observed repeatedly in multiple implementations. Generally, The Rule of Three 3 is followed—a pattern should be observed in at least three systems before it can be recognized as a pattern.
The patterns approach, employed in this book, is rooted in the study of actual codebases from various open source projects, such as Apache Kafka, 4 Apache Cassandra, 5 MongoDB, 6 Apache Pulsar, 7 etcd, 8 Apache ZooKeeper, 9 CockroachDB, 10 YugabyteDB, 11 Akka, 12 JGroups, 13 and others. These patterns are grounded in practical examples and can be applied to different software systems. By exploring the insights gained from these codebases, readers can learn to understand and apply these patterns to solve common software challenges.
Another important aspect of patterns is that they are not used in isolation but rather in conjunction with other patterns. Understanding how the patterns interlink makes it much easier to grasp the overall architecture of the system.
The next chapter takes a tour of most of the patterns and shows how they link together.