API protocol decision framework

API & Messaging Protocol Decision Framework
This document summarizes communication protocols and messaging systems discussed, with explanations, comparisons, and a practical decision framework.
1. API Communication Protocols Overview
1.1 REST (RESTful API)
-
Model: Request / Response
-
Transport: HTTP/1.1 or HTTP/2
-
Serialization: JSON (text-based)
-
Strengths:
- Universally supported (browsers, mobile, backend)
- Easy to debug and test
- Ideal for CRUD and public APIs
-
Limitations:
- No native streaming
- Client must initiate requests
Use when: Public APIs, simple client-server communication, CRUD operations.
1.2 SSE (Server-Sent Events)
-
Model: One-way streaming (Server → Client)
-
Transport: HTTP
-
Serialization: Text
-
Strengths:
- Simple server push
- Built-in browser support
- Auto-reconnect
-
Limitations:
- No client → server streaming
- Not bidirectional
Use when: Notifications, live logs, order status updates.
1.3 WebSocket
-
Model: Full-duplex (Client ⇄ Server)
-
Transport: TCP (via HTTP Upgrade)
-
Protocol: WebSocket frames (not HTTP after upgrade)
-
Strengths:
- Real-time, low latency
- Bidirectional communication
- Native browser support
-
Limitations:
- No schema or contract by default
- Harder to scale and observe
Use when: Chat, gaming, collaborative applications, live dashboards.
1.4 gRPC
-
Model: RPC (Unary + Streaming)
-
Transport: HTTP/2 (mandatory)
-
Serialization: Protobuf (binary)
-
Strengths:
- High performance
- Strong contracts (proto files)
- Multiplexing, flow control, streaming
-
Limitations:
- Not browser-friendly (needs gRPC-Web)
- Requires HTTP/2 and modern infrastructure
Use when: Internal microservices, high-throughput systems, streaming between services.
2. Serialization Concepts
2.1 Serialization in REST
- Serialization: Object → JSON → HTTP body
- Deserialization: HTTP body (JSON) → Object
- JSON is text-based, human-readable, but larger and slower to parse.
2.2 Serialization in gRPC
- Uses Protocol Buffers
- Data is encoded as binary
- No field names on the wire
- Smaller payloads and faster parsing
Mental model:
- REST = Objects → Text → Bytes
- gRPC = Objects → Binary → Bytes
3. HTTP, TCP, and WebSocket Upgrade
3.1 TCP
- Reliable byte stream
- Guarantees order and delivery
- Does not understand messages
3.2 HTTP
- Application protocol on top of TCP
- Request / Response model
- Server cannot push by default
3.3 WebSocket Upgrade Flow
- Client sends HTTP request with
Upgrade: websocket - Server responds with
101 Switching Protocols - Connection switches to WebSocket protocol
- Full-duplex communication over the same TCP connection
4. Why gRPC Requires HTTP/2
4.1 Multiplexed Streams
- Multiple RPC calls share one TCP connection
- No head-of-line blocking
4.2 Flow Control
- Sender respects receiver capacity
- Prevents memory overflow
- Applied per stream and per connection
4.3 Bidirectional Streaming
- Client and server send messages independently
- Enables real-time pipelines and data streams
5. Messaging & Streaming Systems
5.1 RabbitMQ
-
Type: Message Broker (Task Queue)
-
Model: Message is consumed once
-
Strengths:
- Reliable delivery
- Routing and acknowledgements
- Dead-letter queues
-
Best for: Background jobs, commands, workflows
5.2 Kafka
-
Type: Distributed Event Log
-
Model: Append-only log with offsets
-
Strengths:
- High throughput
- Message replay
- Multiple consumers
-
Best for: Event-driven systems, analytics, event sourcing
5.3 Redis (as MQ)
-
Type: In-memory data store
-
Patterns: Pub/Sub, Streams, Lists
-
Strengths:
- Very low latency
- Simple setup
-
Limitations:
- Weaker durability
- Not designed as primary MQ
Best for: Lightweight queues, real-time signals, caching-based workflows
6. Comparison Tables
6.1 API Protocols
| Protocol | Direction | Streaming | Browser | Performance | Use Case |
|---|---|---|---|---|---|
| REST | Client → Server | No | Yes | Medium | Public APIs |
| SSE | Server → Client | Yes | Yes | Medium | Notifications |
| WebSocket | Bidirectional | Yes | Yes | High | Real-time apps |
| gRPC | Bidirectional | Yes | No* | Very High | Microservices |
*Browser requires gRPC-Web + proxy
6.2 Messaging Systems
| Feature | RabbitMQ | Kafka | Redis |
|---|---|---|---|
| Model | Tasks | Events | Lightweight MQ |
| Replay | No | Yes | Limited |
| Throughput | Medium | Very High | High |
| Latency | Low | Low–Medium | Very Low |
| Durability | Disk | Disk | Memory-first |
7. Decision Framework
Step 1: Who is the client?
- Browser → REST / SSE / WebSocket
- Service → gRPC
Step 2: Communication pattern
- Request/Response → REST or gRPC
- Server push → SSE
- Full duplex → WebSocket or gRPC
Step 3: Data model
- Tasks (do something) → RabbitMQ
- Events (something happened) → Kafka
Step 4: Replay requirement
- Needed → Kafka
- Not needed → RabbitMQ / Redis
Step 5: Scale & performance
- Simple & fast → Redis
- High throughput → Kafka
- Reliable workflows → RabbitMQ
8. Common Architecture Patterns
Hybrid API + Microservices
Browser
↓ REST / WebSocket
API Gateway
↓ gRPC
Internal Services
Async Processing
API → RabbitMQ → Worker
Event-Driven System
Service A → Kafka → Service B / C / Analytics
9. One-line Rules of Thumb
- Public APIs → REST
- Server push → SSE
- Browser real-time → WebSocket
- Service-to-service → gRPC
- Tasks → RabbitMQ
- Events → Kafka
- Fast & simple → Redis
10. Summary
Protocol and messaging selection should be driven by client type, communication pattern, performance requirements, and operational maturity. REST dominates public APIs, WebSocket and SSE handle real-time browser use cases, gRPC optimizes internal service communication, RabbitMQ manages task-based workflows, Kafka powers event-driven architectures, and Redis fills lightweight, low-latency gaps.
Duong Ngo
Full-Stack AI Developer with 12+ years of experience