Key Question
How do RPC frameworks ensure both sides agree on the message format?
Deep Dive
Without a shared contract, the client could send {"name": "Alice"} while the server expects bytes encoding [0x01, 0x00, 0x00, 0x00, 0x41, 0x6c, 0x69, 0x63, 0x65]. The Interface Definition Language (IDL) is the contract that prevents this mismatch.
Interface Definition Language (IDL)
An IDL is a language-neutral schema that defines the types and methods both sides agree on. Example in Protocol Buffers (protobuf):
syntax = "proto3";
message Person {
string name = 1;
int32 id = 2;
string email = 3;
}
The IDL compiler generates:
- Client stub (for the caller)
- Server stub/skeleton (for the callee)
- Serialization/deserialization code in multiple languages
A single .proto file can generate Java, Go, Python, and C++ code. Both sides compile from the same IDL β they must agree, or the call fails.
Serialization Formats Compared
Serialization converts in-memory data structures into a format suitable for network transmission.
Raw data: Person{name: "Alice", id: 42, email: "a@b.com"}
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
JSON: {"name":"Alice","id":42,"email":"a@b.com"}
Size: ~45 bytes | Speed: slow (text parsing)
Protobuf: 0A05416C69636510D4021A076140622E636F6D
Size: ~16 bytes | Speed: fast (binary, no parsing)
Thrift: Similar size/speed to Protobuf (TCompactProtocol)
~17 bytes | Schema required
Avro: 0x0A 0x05 0x41 0x6C 0x69 0x63 0x65 ...
Size: ~15 bytes | Schema sent with data (evolution-friendly)
| Format | Human Readable | Size | Speed | Schema Needed | Schema Evolution |
|---|---|---|---|---|---|
| JSON | Yes | Large | Slow | Optional | Manual |
| Protobuf | No | Compact | Fast | Required | Good (field numbers) |
| Thrift | No | Compact | Fast | Required | Good (field IDs) |
| Avro | No | Compact | Fast | Required | Excellent (defaults) |
Key Trade-offs
- JSON is unbeatable for debugging and simple APIs. But parsing JSON is CPU-intensive and the wire size is 2-3x binary formats.
- Protobuf uses field numbers (not names) on the wire, which makes messages small and schema evolution safe. Adding a new field doesnβt break old clients β they ignore unknown fields.
- Avro is special: the schema is embedded or known at read time. This makes Avro the best choice for data pipelines where the writer and reader may have different schema versions (e.g., Kafka, Hadoop).
Check Your Understanding
-
A protobuf message has fields:
string name = 1; int32 age = 2;. A client sends bytes for a message withint32 id = 2; float salary = 3;. What happens on the server side? -
Why is JSON slower than Protobuf for large messages?
-
When would you choose Avro over Protobuf?
The βSo What?β
Serialization is the single biggest performance lever in most RPC systems. Switching from JSON to Protobuf can cut latency by 5-10x (less data on the wire, less CPU deserializing). The IDL is also your living API documentation β if you can read the .proto file, you understand the contract. In production systems, never let clients and servers serialize independently without a shared schema; you will get mysterious parse errors at 3 AM.
βοΈ 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.