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
-
A team is building a real-time multiplayer game for 10,000 concurrent players. Should they use client-server or P2P? Why?
-
What are the key disadvantages of a 3-tier architecture compared to a pure P2P design?
-
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.
- Which architectural model (client-server, P2P, multi-tier, hybrid) would you recommend?
- What specific design decisions would you make to handle the read-heavy workload?
- 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:
- 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.
- 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.
- 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.
- Will old servers crash when they receive a message with
coupon_codeset? - A client built from the old schema processes a message that has
coupon_code. What does the client see? - What happens if you later delete field 3 (
total) and reuse its number for a newint64 total_cents = 3?
ποΈ View Solutions
Solutions: Architectural Models & RPC
Exercise 1: Architecture Choice
-
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).
-
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).
-
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
-
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
deductBalanceidempotent (using a request ID or idempotency key). -
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.
-
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
-
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.
-
What does the old client see? The
coupon_codefield will be absent (default empty string). The client seesorder.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. -
What happens if you delete field 3 and reuse its number? Disaster. Old servers still running will interpret the new
total_centsfield as the oldtotalfield and read garbage. Never reuse a field number. Instead, mark the field asreserved 3;in the new schema β this prevents accidental reuse.