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

ProtocolTransportFormatStyleBest For
RESTHTTP 1.1/2JSON/XMLRequest-ResponseGeneral web APIs, CRUD apps.
gRPCHTTP 2Binary (Protobuf)Unary / StreamingHigh-performance microservices.
GraphQLHTTPJSONQuery-basedMobile/Web apps with complex data.
WebSocketsTCP (Upgrade)Binary / TextBi-directionalReal-time chat, live trading.
WebhooksHTTP POSTJSONEvent-drivenExternal system integrations.
SSEHTTPText1-way PushLive news feeds, dashboards.
SOAPHTTP/SMTPXMLProtocolBanking, 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