Communication Protocols
This document covers protocols for service communication and their appropriate use cases.
Protocol Overview
Loading diagram...
REST (Representational State Transfer)
The most common API style for web services.
Core Principles
| Principle | Description |
|---|---|
| Stateless | Each request contains all needed information |
| Resource-based | URLs represent resources, not actions |
| HTTP methods | Verbs for operations (GET, POST, etc.) |
| Uniform interface | Consistent URL patterns and responses |
HTTP Methods
Loading diagram...
| Method | Operation | Idempotent | Safe |
|---|---|---|---|
| GET | Read resource | Yes | Yes |
| POST | Create resource | No | No |
| PUT | Replace resource | Yes | No |
| PATCH | Partial update | No | No |
| DELETE | Remove resource | Yes | No |
URL Design
| Pattern | Example | Description |
|---|---|---|
| Collection | /users | List all users |
| Item | /users/123 | Specific user |
| Nested | /users/123/orders | User's orders |
| Filter | /users?status=active | Query parameters |
| Action | /users/123/activate | Non-CRUD action |
Status Codes
| Range | Category | Common Codes |
|---|---|---|
| 2xx | Success | 200 OK, 201 Created, 204 No Content |
| 3xx | Redirect | 301 Moved, 304 Not Modified |
| 4xx | Client Error | 400 Bad Request, 401 Unauthorized, 404 Not Found |
| 5xx | Server Error | 500 Internal Error, 503 Unavailable |
REST Characteristics
| Advantages | Disadvantages |
|---|---|
| Simple, widely understood | Overfetching/underfetching |
| Cacheable (GET requests) | Multiple round trips |
| Language agnostic | No real-time support |
| Extensive tooling | Versioning complexity |
RPC (Remote Procedure Call)
Call functions on remote servers as if they were local.
REST vs RPC Style
Loading diagram...
| Aspect | REST | RPC |
|---|---|---|
| Focus | Resources | Actions |
| URLs | Nouns | Verbs |
| Example | GET /users/123 | POST /getUser {id: 123} |
| Caching | Built-in (HTTP) | Manual |
| Use cases | CRUD, public APIs | Internal services |
gRPC
Google's high-performance RPC framework.
Loading diagram...
Key features:
- Protocol Buffers (binary, smaller than JSON)
- HTTP/2 (multiplexing, streaming)
- Bi-directional streaming
- Auto-generated client/server code
gRPC Streaming Modes
| Mode | Use Case |
|---|---|
| Unary | Simple request/response |
| Server streaming | Large response, real-time updates |
| Client streaming | File upload, batch operations |
| Bidirectional | Chat, gaming, collaboration |
gRPC vs REST Selection
| Use gRPC | Use REST |
|---|---|
| Internal microservices | Public APIs |
| High performance required | Browser clients |
| Streaming required | Simple CRUD |
| Strong typing desired | Human-readable format needed |
WebSockets
Full-duplex communication over a single TCP connection.
Loading diagram...
WebSocket vs HTTP
| Aspect | HTTP | WebSocket |
|---|---|---|
| Connection | New per request | Persistent |
| Direction | Client initiates | Bidirectional |
| Overhead | Headers every request | Low after handshake |
| Server push | Not native | Native |
| Use case | Request/response | Real-time |
WebSocket Use Cases
| Use Case | Rationale |
|---|---|
| Chat applications | Instant message delivery |
| Live dashboards | Real-time data updates |
| Gaming | Low-latency bidirectional |
| Collaborative editing | Sync between users |
| Live sports/trading | Continuous data streams |
Scaling WebSockets
Loading diagram...
Scaling strategies:
- Sticky sessions (route user to same server)
- Pub/sub for cross-server messaging (Redis, Kafka)
- Connection state in shared store
Server-Sent Events (SSE)
One-way server-to-client streaming over HTTP.
Loading diagram...
SSE vs WebSocket
| Aspect | SSE | WebSocket |
|---|---|---|
| Direction | Server to Client | Bidirectional |
| Protocol | HTTP | Custom over TCP |
| Reconnection | Automatic | Manual |
| Browser support | Native EventSource | Native WebSocket |
| Complexity | Lower | Higher |
Use SSE: Server-to-client only (notifications, feeds, dashboards)
Use WebSocket: Bidirectional (chat, gaming, collaboration)
Long Polling
Simulates server push over HTTP.
Loading diagram...
Real-time Options Comparison
| Technique | Latency | Server Load | Browser Support | Complexity |
|---|---|---|---|---|
| Polling | High | High | Universal | Low |
| Long Polling | Medium | Medium | Universal | Medium |
| SSE | Low | Low | Good | Low |
| WebSocket | Lowest | Low | Good | Higher |
Message Queues
Asynchronous communication between services.
Loading diagram...
Async vs Sync Selection
| Use Async (Queue) | Use Sync (HTTP/RPC) |
|---|---|
| Long-running tasks | Immediate response needed |
| Spiky traffic | Consistent load |
| Decoupled services | Tight coupling acceptable |
| Failure tolerance needed | Simple error handling |
| Order processing, emails | User-facing requests |
Message Queue Patterns
| Pattern | Description | Use Case |
|---|---|---|
| Point-to-point | One producer, one consumer | Task distribution |
| Pub/Sub | One message, many consumers | Event notifications |
| Request/Reply | Async request with response queue | Long-running with result |
| Dead Letter | Failed messages stored separately | Error handling |
Protocol Selection Guide
Loading diagram...
Protocol Selection Summary
| Protocol | Use Case |
|---|---|
| REST | Public APIs, CRUD, simple services |
| gRPC | Internal microservices, performance-critical |
| WebSocket | Real-time bidirectional (chat, gaming) |
| SSE | Server push only (notifications, feeds) |
| Message queues | Async, decoupled, reliable |