All Posts

System Design Protocols: REST, RPC, and TCP/UDP

Abstract AlgorithmsAbstract Algorithms
··3 min read

TL;DR

Exploring Communication Layers: Choosing Between REST, gRPC, TCP, and UDP

Cover Image for System Design Protocols: REST, RPC, and TCP/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.

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):
    1. SYN ("Hello?")
    2. SYN-ACK ("I hear you.")
    3. 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)

  1. Client sends request: "Any new messages?"
  2. Server holds the request open until a message arrives.
  3. Server responds: "Yes, here."
  4. Client immediately sends a new request.
  5. Cons: Server resource heavy (holding connections).

WebSockets (The Solution)

  1. Client sends HTTP Upgrade request.
  2. Connection switches to a persistent, bi-directional TCP tunnel.
  3. Server can push data anytime.
  4. Pros: Real-time, low latency.
  5. 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.

Abstract Algorithms

Written by

Abstract Algorithms

@abstractalgorithms