System Design Protocols: REST, RPC, and TCP/UDP
Abstract AlgorithmsTL;DR
Exploring Communication Layers: Choosing Between REST, gRPC, TCP, and UDP

1. API Paradigms: REST vs. RPC
When Server A talks to Server B, how do they structure the conversation?
REST (Representational State Transfer)
The language of the web.
- Concept: Resources (Nouns) are manipulated via HTTP Verbs.
- Format: Usually JSON.
- Example:
GET /users/123(Get user)POST /users(Create user)
- Pros:
- Universal: Every language/browser supports HTTP/JSON.
- Stateless: Easy to scale.
- Debuggable: You can read the JSON.
- Cons:
- Bloated: JSON contains repeated field names (
{"name": "Alice"}). - Over-fetching: You might get more data than you need.
- Text-based: Slower to parse than binary.
- Bloated: JSON contains repeated field names (
RPC (Remote Procedure Call) / gRPC
Calling a function on a remote server as if it were local.
- Concept: Actions (Verbs).
- Format: Binary (Protobuf).
- Example:
getUser(123) - gRPC (Google RPC): The modern standard built on HTTP/2.
- Pros:
- Performance: Binary is much smaller and faster to parse than JSON.
- Schema: Strict contracts (.proto files) prevent type errors.
- Streaming: Supports bi-directional streaming out of the box.
- Cons:
- Complexity: Requires code generation.
- Not Browser Friendly: Hard to call directly from a web browser (needs a proxy).
Verdict: Use REST for public APIs (external). Use gRPC for internal microservices (server-to-server).
2. Transport Layer: TCP vs. UDP
Underneath HTTP and gRPC lies the transport layer.
TCP (Transmission Control Protocol)
The reliable workhorse.
- Philosophy: "I guarantee the data will arrive, in order, and without errors."
- Mechanism (3-Way Handshake):
- SYN ("Hello?")
- SYN-ACK ("I hear you.")
- ACK ("Great, let's talk.")
- Features:
- Retransmission: If a packet is lost, TCP resends it.
- Ordering: If packets arrive out of order (3, 1, 2), TCP fixes them (1, 2, 3).
- Congestion Control: Slows down if the network is busy.
- Use Case: Web browsing (HTTP), Email (SMTP), File Transfer (FTP). Accuracy > Speed.
UDP (User Datagram Protocol)
The speed demon.
- Philosophy: "I'm sending data. Good luck."
- Mechanism: Fire and forget. No handshake.
- Features:
- No Guarantee: Packets might be lost, duplicated, or out of order.
- Low Overhead: No handshake latency.
- Use Case:
- Video Streaming / VoIP: If you lose a pixel in a video frame, you don't want to pause the video to wait for it. You just skip it.
- Gaming: Real-time player positions. Old data is useless.
- Interview Tip: If building a Chat app, use TCP (messages must arrive). If building a Video Chat, use UDP (speed matters more).
3. WebSockets vs. Long Polling
How does the server send data to the client without the client asking first? (e.g., a Chat message).
Long Polling (The Hack)
- Client sends request: "Any new messages?"
- Server holds the request open until a message arrives.
- Server responds: "Yes, here."
- Client immediately sends a new request.
- Cons: Server resource heavy (holding connections).
WebSockets (The Solution)
- Client sends HTTP Upgrade request.
- Connection switches to a persistent, bi-directional TCP tunnel.
- Server can push data anytime.
- Pros: Real-time, low latency.
- Use Case: Chat apps, Stock tickers, Collaborative editing (Google Docs).
Summary
- REST is for public APIs (JSON, flexible).
- gRPC is for internal microservices (Binary, fast).
- TCP guarantees delivery (Web, Email).
- UDP guarantees speed (Video, Gaming).
- WebSockets enable real-time, bi-directional communication.
Next Up: Where do we store the data? We'll explore Databases: SQL vs NoSQL and Scaling Strategies.
Tags

Written by
Abstract Algorithms
@abstractalgorithms
