Distributed & Decentralized Systems Curriculum
Time Causality Β· Architectural Models RPC

Key Question

What are the fundamental architectural patterns for organizing distributed systems?

Deep Dive

Distributed systems are organized around a few recurring architectural patterns. The choice of model determines how nodes communicate, who controls state, and how the system scales under load.

1. Client-Server

One or more centralized servers provide services; clients send requests and receive responses. Servers are stateful (maintain session state) or stateless (each request is self-contained). This is the oldest and most intuitive model.

       Request (e.g., GET /users/42)
  Client ──────────────────────────►  Server
        ◄──────────────────────────
              Response (JSON)

Advantages: Centralized control makes security, auth, and data consistency straightforward. You know exactly where the data lives.

Disadvantages: Single point of failure β€” if the server goes down, everyone is blocked. Scalability is limited by the server’s capacity (vertical scaling hits a ceiling). A single server handling 10M users requires significant hardware.

2. Peer-to-Peer

Every node (peer) acts as both client and server. There is no central coordinator. Each peer stores and serves data, routes requests, and enforces rules collectively.

  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
  β”‚ Peer A  │◄───►│ Peer B  β”‚
  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
       β–²               β–²
       β”‚               β”‚
       β–Ό               β–Ό
  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
  β”‚ Peer C  │◄───►│ Peer D  β”‚
  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Advantages: Scales horizontally β€” each new peer adds capacity. No SPOF. Highly fault-tolerant (the network survives node failures). Example: BitTorrent β€” thousands of peers sharing file chunks without any central server.

Disadvantages: Coordination is expensive (consensus, routing, discovery). Consistency is harder to enforce. Security is more complex (no central authority to ban bad actors).

3. Multi-Tier (n-Tier)

The system is split into distinct layers: presentation (UI), application logic, and data storage. Each layer can be deployed and scaled independently.

  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
  β”‚  Browser/App β”‚  (Presentation Tier)
  β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚ HTTP
  β”Œβ”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”
  β”‚  API Server  β”‚  (Logic Tier)
  β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚ SQL
  β”Œβ”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”
  β”‚   Database   β”‚  (Data Tier)
  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

This is the default for modern web apps. You can scale the API tier independently of the database and swap the frontend without touching the backend.

Hybrid Models

Real systems often blend patterns:

  • Super-peers (Skype, early KaZaA): Most peers connect to super-peers, which form a P2P backbone. Combines P2P’s scalability with client-server’s manageability.
  • Edge computing: Client devices process data locally, sync to cloud servers when connected. Like a P2P-client-server hybrid.
  • Microservices: Each service is a small client-server system; collectively they form a distributed graph that behaves more like P2P internally.

Check Your Understanding

  1. A team is building a real-time multiplayer game for 10,000 concurrent players. Should they use client-server or P2P? Why?

  2. What are the key disadvantages of a 3-tier architecture compared to a pure P2P design?

  3. Why might you combine super-peers with a P2P overlay rather than using pure P2P?

The β€œSo What?”

Architecture is the first design decision you make in any distributed system. Pick client-server when you need strong consistency and control. Pick P2P when you need massive scale and fault tolerance. Pick multi-tier when you need to evolve and scale different parts independently. Most production systems are hybrids β€” the trick is knowing which pattern fits each subsystem.


✏️ Exercises

Exercises: Architectural Models & RPC

Exercise 1: Architecture Choice

A team is building a file-sharing application for 100,000 users. Files are read-heavy (mostly downloads), users are geographically distributed, and there is no budget for centralized infrastructure.

  1. Which architectural model (client-server, P2P, multi-tier, hybrid) would you recommend?
  2. What specific design decisions would you make to handle the read-heavy workload?
  3. What are the top three problems you’d need to solve?

Exercise 2: RPC Call Failure Analysis

A client calls deductBalance(userID: "u42", amount: 50.00) via RPC. The client stub sends the request to the server. For each scenario below, state what happens and whether the client’s balance is correct:

  1. The client stub marshals the request and sends it. The server receives it, unmarshals, calls the function (which deducts $50 from the DB), but the server crashes before sending the response.
  2. The server receives the request, processes it, and sends the response. The response is lost in the network. The client times out and throws an exception.
  3. The client sends the request. The server processes it and sends the response. The client receives the response. Everything works β€” but the network duplicates the request and the server processes it twice.

Exercise 3: Protobuf Field Evolution

You have this protobuf schema deployed in production:

message Order {
  string order_id = 1;
  string user_id = 2;
  float total = 3;
}

You want to add a string coupon_code = 4 field. Some old server instances still running don’t know about field 4.

  1. Will old servers crash when they receive a message with coupon_code set?
  2. A client built from the old schema processes a message that has coupon_code. What does the client see?
  3. What happens if you later delete field 3 (total) and reuse its number for a new int64 total_cents = 3?
πŸ‘οΈ View Solutions

Solutions: Architectural Models & RPC

Exercise 1: Architecture Choice

  1. Recommended model: Hybrid with P2P as the primary model β€” users share files directly with each other β€” plus a small set of super-peers (or a lightweight tracker) for discovery.

    • Pure P2P (BitTorrent-style) handles read-heavy workloads naturally: popular files are replicated across many peers, distributing the download load.
    • A tracker (a lightweight centralized component) solves the discovery problem β€” where to find each file.
    • A small number of β€œseed” servers can ensure unpopular files remain available (no single point of failure because seeds are optional).
  2. Design decisions:

    • Chunk files into pieces so peers can download different chunks from different peers in parallel.
    • Use content-addressed storage (hash of the file content as the identifier) to verify integrity.
    • Implement a tit-for-tat incentive mechanism (you share, you get faster downloads).
  3. Top three problems:

    • Discovery: How do peers find each other and learn which files are available? (Tracker nodes + DHT)
    • Churn: Peers join and leave constantly. How do you maintain availability of rare files? (Replication factor, redundancy)
    • Trust: How do you prevent peers from serving corrupted data? (Content hashing, cryptographic verification)

Exercise 2: RPC Call Failure Analysis

  1. Correct? β€” No. The server deducted $50 but crashed before the client got confirmation. The client assumes the call failed and may retry. You now have a duplicate deduction (or at least a $50 mismatch unless the operation is idempotent). This is the classic β€œat-most-once vs at-least-once” dilemma. Solution: make deductBalance idempotent (using a request ID or idempotency key).

  2. Correct? β€” The server did deduct $50. The client got an exception and doesn’t know whether the deduction happened. This is the exactly-once is impossible problem in distributed systems. The client must check the balance or retry with idempotency.

  3. Correct? β€” No. The balance is deducted twice β€” $100 total instead of $50. The server processed the request twice because the transport layer delivered a duplicate. Solution: deduplication at the server (track recently seen request IDs).

Key insight: In all three cases, the client cannot trivially know the correct balance without additional mechanisms (idempotency keys, at-least-once delivery with dedup, transactional outboxes).


Exercise 3: Protobuf Field Evolution

  1. Will old servers crash? No. Protobuf is designed for forward compatibility. Old servers that don’t know about field 4 will simply ignore the unknown bytes. The message is self-describing enough that unknown fields are skipped during deserialization.

  2. What does the old client see? The coupon_code field will be absent (default empty string). The client sees order.coupon_code == "". The data is not lost β€” if the client re-serializes the message, the unknown bytes for field 4 are preserved and passed through.

  3. What happens if you delete field 3 and reuse its number? Disaster. Old servers still running will interpret the new total_cents field as the old total field and read garbage. Never reuse a field number. Instead, mark the field as reserved 3; in the new schema β€” this prevents accidental reuse.