API Communication Patterns
Choosing the right API protocol is a critical decision in System Design. Each style has unique trade-offs regarding latency, bandwidth, browser support, and complexity.
📊 The API Trade-off Matrix
| Protocol | Transport | Format | Style | Best For |
|---|---|---|---|---|
| REST | HTTP 1.1/2 | JSON/XML | Request-Response | General web APIs, CRUD apps. |
| gRPC | HTTP 2 | Binary (Protobuf) | Unary / Streaming | High-performance microservices. |
| GraphQL | HTTP | JSON | Query-based | Mobile/Web apps with complex data. |
| WebSockets | TCP (Upgrade) | Binary / Text | Bi-directional | Real-time chat, live trading. |
| Webhooks | HTTP POST | JSON | Event-driven | External system integrations. |
| SSE | HTTP | Text | 1-way Push | Live news feeds, dashboards. |
| SOAP | HTTP/SMTP | XML | Protocol | Banking, Healthcare, Enterprise. |
🛠️ Deep Dive by Use Case
1. High Performance: gRPC
Uses Protocol Buffers for efficient binary serialization and multiplexing via HTTP/2. It is significantly faster than REST but has limited native browser support (requires gRPC-Web).
2. Client-Driven: GraphQL
Allows the client to specify the exact fields needed. This prevents Over-fetching (getting more data than needed) and Under-fetching (making multiple calls to get related data).
3. Real-Time: WebSockets vs. SSE
- WebSockets: Ideal for bi-directional communication (e.g., a chat app where both sides send/receive constantly). Stateful and requires more server resources.
- SSE (Server-Sent Events): Ideal for uni-directional data (e.g., a stock price ticker). It is more lightweight than WebSockets as it works over standard HTTP.
4. Enterprise Rigidity: SOAP
While considered “heavy,” SOAP is still the standard for environments requiring strict security (WS-Security) and ACID-compliant transactions across multiple protocols.
Source: Ingested from YouTube: 7 API Types Every Developer Should Know