# 📚Distributed Systems

### 🔍 What is a Distributed System?

A distributed system is a network of independent computers that appear to users as a single coherent system. These systems work together to solve complex problems and handle large-scale operations.

<figure><img src="/files/iGFhYdS8P43DZUZ5rdLL" alt=""><figcaption></figcaption></figure>

### 🏗️ Key Components of Distributed Systems

1. 🖥️ Nodes: Individual computers or servers in the network
2. 🔗 Communication: Protocols for inter-node messaging
3. 🔄 Synchronization: Mechanisms to coordinate actions across nodes
4. 🗃️ Data Management: Strategies for data storage and retrieval
5. 🛡️ Fault Tolerance: Techniques to handle node failures

### 🏛️ Distributed System Architectures

#### Client-Server Architecture

In this model, clients request services from servers, which process these requests and return results.

<figure><img src="/files/jNdSOXDll8E7RIQdqGMO" alt=""><figcaption></figcaption></figure>

#### Peer-to-Peer (P2P) Architecture

In P2P systems, nodes act as both clients and servers, sharing resources directly without a central server.

<figure><img src="/files/9s1PUFIFcyczHiS0f5Wb" alt=""><figcaption></figcaption></figure>

### 📊 Comparison of Distributed System Architectures

| Feature              | Client-Server                     | Peer-to-Peer                     |
| -------------------- | --------------------------------- | -------------------------------- |
| Centralization       | Centralized                       | Decentralized                    |
| Scalability          | Limited by server capacity        | Highly scalable                  |
| Fault Tolerance      | Single point of failure (server)  | Highly fault-tolerant            |
| Security             | Easier to secure                  | More challenging to secure       |
| Resource Utilization | Efficient use of powerful servers | Utilizes idle resources of peers |

### 🛠️ Key Challenges in Distributed Systems

1. 🕒 Consistency: Ensuring data consistency across nodes
2. 🔀 Concurrency: Managing simultaneous operations
3. 🧩 Partitioning: Dividing data and tasks across nodes
4. 🔄 Replication: Maintaining multiple copies of data for reliability
5. 🚦 Consensus: Reaching agreement among nodes

### 💡 Example: Distributed Key-Value Store

Let's implement a simple distributed key-value store using a consistent hashing algorithm for data partitioning.

```python
import hashlib

class ConsistentHash:
    def __init__(self, nodes, virtual_nodes=100):
        self.nodes = nodes
        self.virtual_nodes = virtual_nodes
        self.ring = {}
        self._build_ring()

    def _build_ring(self):
        for node in self.nodes:
            for i in range(self.virtual_nodes):
                key = self._hash(f"{node}:{i}")
                self.ring[key] = node

    def _hash(self, key):
        return hashlib.md5(key.encode()).hexdigest()

    def get_node(self, key):
        if not self.ring:
            return None
        hash_key = self._hash(key)
        for node_hash in sorted(self.ring.keys()):
            if node_hash >= hash_key:
                return self.ring[node_hash]
        return self.ring[sorted(self.ring.keys())[0]]

class DistributedKVStore:
    def __init__(self, nodes):
        self.consistent_hash = ConsistentHash(nodes)
        self.data = {node: {} for node in nodes}

    def set(self, key, value):
        node = self.consistent_hash.get_node(key)
        self.data[node][key] = value

    def get(self, key):
        node = self.consistent_hash.get_node(key)
        return self.data[node].get(key)

# Usage
nodes = ["node1", "node2", "node3"]
kv_store = DistributedKVStore(nodes)

kv_store.set("user1", "John Doe")
kv_store.set("user2", "Jane Smith")

print(kv_store.get("user1"))  # Output: John Doe
print(kv_store.get("user2"))  # Output: Jane Smith
```

### 🚀 Serverless Architecture

Serverless architecture allows developers to build and run applications without managing servers, focusing solely on writing code.

* **Function as a Service (FaaS):** Execute code in response to events without provisioning servers
* **Backend as a Service (BaaS):** Third-party services for common backend functionalities
* **Auto-scaling:** Automatically adjusts resources based on demand

<figure><img src="/files/oZXGJpjP1pmMjHHzoxTp" alt=""><figcaption></figcaption></figure>

### 🧠 Distributed Systems: A Deeper Dive

#### 1. 🔄 Consistency Models

Consistency in distributed systems refers to how data updates are propagated and viewed across all nodes.

* **Strong Consistency:** All nodes see the same data at the same time
* **Eventual Consistency:** Given enough time, all updates will propagate to all nodes
* **Causal Consistency:** Causally related operations are seen in the same order by all nodes

<figure><img src="/files/ddcyHasnvkzOTSeSRpp3" alt=""><figcaption></figcaption></figure>

#### 2. 🔀 Concurrency Control

Managing simultaneous operations is crucial in distributed systems to maintain data integrity.

* **Pessimistic Concurrency Control:** Locks resources before operations
* **Optimistic Concurrency Control:** Allows operations and checks for conflicts later
* **Multiversion Concurrency Control (MVCC):** Maintains multiple versions of data

<figure><img src="/files/H9qPs0V66KC9r5r5AoDm" alt=""><figcaption></figcaption></figure>

#### 3. 🧩 Data Partitioning Strategies

Partitioning involves dividing data across multiple nodes to improve scalability and performance.

* **Range Partitioning:** Divides data based on ranges of a key
* **Hash Partitioning:** Uses a hash function to determine data placement
* **List Partitioning:** Assigns data to partitions based on lists of values

<figure><img src="/files/njkL7RX3YZV3LlpsOFfd" alt=""><figcaption></figcaption></figure>

#### 4. 🔄 Replication Techniques

Replication involves maintaining multiple copies of data across different nodes for fault tolerance and improved read performance.

* **Single-Leader Replication:** One node handles writes, others replicate
* **Multi-Leader Replication:** Multiple nodes can accept writes
* **Leaderless Replication:** Any node can accept reads and writes

  <figure><img src="/files/jazOMZB79CGXbUd5l61F" alt=""><figcaption></figcaption></figure>

#### 5. 🚦 Consensus Algorithms

Consensus algorithms ensure that all nodes in a distributed system agree on a single data value or state.

* **Paxos:** Classic consensus algorithm for asynchronous systems
* **Raft:** Designed for understandability, uses leader election
* **Byzantine Fault Tolerance (BFT):** Handles malicious nodes

  <figure><img src="/files/e4oe3Be2sXnH5Hz9JFvR" alt=""><figcaption></figcaption></figure>

### 🏗️ Advanced Distributed System Architectures

#### 1. 🌐 Microservices Architecture

Microservices architecture breaks down an application into small, independent services that communicate via APIs.

<figure><img src="/files/qPIcyXNOxmDsBcvhW53n" alt=""><figcaption></figcaption></figure>

#### 2. 🧊 Lambda Architecture

Lambda architecture combines batch and stream processing to handle large amounts of data.

<figure><img src="/files/8YT32Gh42gYqXZZTYE9g" alt=""><figcaption></figcaption></figure>

#### 3. 🧠 Event-Driven Architecture

Event-driven architecture focuses on producing, detecting, and reacting to events.

<figure><img src="/files/D5Crlk6dums9DTvS2p0e" alt=""><figcaption></figcaption></figure>

### 📊 Comprehensive Comparison of Distributed System Architectures

| Feature          | Client-Server    | Peer-to-Peer | Microservices            | Lambda                              | Event-Driven          |
| ---------------- | ---------------- | ------------ | ------------------------ | ----------------------------------- | --------------------- |
| Scalability      | Limited          | High         | High                     | Very High                           | High                  |
| Complexity       | Low              | Medium       | High                     | Very High                           | Medium                |
| Data Consistency | High             | Variable     | Eventually Consistent    | Eventually Consistent               | Eventually Consistent |
| Fault Tolerance  | Low              | High         | High                     | High                                | High                  |
| Latency          | Low              | Variable     | Low                      | Low for real-time, Higher for batch | Low                   |
| Use Cases        | Web Applications | File Sharing | Large-scale Web Services | Big Data Processing                 | Real-time Systems     |

This comprehensive overview of distributed systems provides a deeper understanding of the key concepts, architectures, and challenges involved in designing and implementing robust distributed systems. By mastering these principles, developers can create scalable, resilient, and efficient systems capable of handling the complexities of modern computing environments


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.mohitkapadiya.com/system-design/distributed-systems.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
