Skip to main content

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

PrincipleDescription
StatelessEach request contains all needed information
Resource-basedURLs represent resources, not actions
HTTP methodsVerbs for operations (GET, POST, etc.)
Uniform interfaceConsistent URL patterns and responses

HTTP Methods

Loading diagram...
MethodOperationIdempotentSafe
GETRead resourceYesYes
POSTCreate resourceNoNo
PUTReplace resourceYesNo
PATCHPartial updateNoNo
DELETERemove resourceYesNo

URL Design

PatternExampleDescription
Collection/usersList all users
Item/users/123Specific user
Nested/users/123/ordersUser's orders
Filter/users?status=activeQuery parameters
Action/users/123/activateNon-CRUD action

Status Codes

RangeCategoryCommon Codes
2xxSuccess200 OK, 201 Created, 204 No Content
3xxRedirect301 Moved, 304 Not Modified
4xxClient Error400 Bad Request, 401 Unauthorized, 404 Not Found
5xxServer Error500 Internal Error, 503 Unavailable

REST Characteristics

AdvantagesDisadvantages
Simple, widely understoodOverfetching/underfetching
Cacheable (GET requests)Multiple round trips
Language agnosticNo real-time support
Extensive toolingVersioning complexity

RPC (Remote Procedure Call)

Call functions on remote servers as if they were local.

REST vs RPC Style

Loading diagram...
AspectRESTRPC
FocusResourcesActions
URLsNounsVerbs
ExampleGET /users/123POST /getUser {id: 123}
CachingBuilt-in (HTTP)Manual
Use casesCRUD, public APIsInternal 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

ModeUse Case
UnarySimple request/response
Server streamingLarge response, real-time updates
Client streamingFile upload, batch operations
BidirectionalChat, gaming, collaboration

gRPC vs REST Selection

Use gRPCUse REST
Internal microservicesPublic APIs
High performance requiredBrowser clients
Streaming requiredSimple CRUD
Strong typing desiredHuman-readable format needed

WebSockets

Full-duplex communication over a single TCP connection.

Loading diagram...

WebSocket vs HTTP

AspectHTTPWebSocket
ConnectionNew per requestPersistent
DirectionClient initiatesBidirectional
OverheadHeaders every requestLow after handshake
Server pushNot nativeNative
Use caseRequest/responseReal-time

WebSocket Use Cases

Use CaseRationale
Chat applicationsInstant message delivery
Live dashboardsReal-time data updates
GamingLow-latency bidirectional
Collaborative editingSync between users
Live sports/tradingContinuous 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

AspectSSEWebSocket
DirectionServer to ClientBidirectional
ProtocolHTTPCustom over TCP
ReconnectionAutomaticManual
Browser supportNative EventSourceNative WebSocket
ComplexityLowerHigher

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

TechniqueLatencyServer LoadBrowser SupportComplexity
PollingHighHighUniversalLow
Long PollingMediumMediumUniversalMedium
SSELowLowGoodLow
WebSocketLowestLowGoodHigher

Message Queues

Asynchronous communication between services.

Loading diagram...

Async vs Sync Selection

Use Async (Queue)Use Sync (HTTP/RPC)
Long-running tasksImmediate response needed
Spiky trafficConsistent load
Decoupled servicesTight coupling acceptable
Failure tolerance neededSimple error handling
Order processing, emailsUser-facing requests

Message Queue Patterns

PatternDescriptionUse Case
Point-to-pointOne producer, one consumerTask distribution
Pub/SubOne message, many consumersEvent notifications
Request/ReplyAsync request with response queueLong-running with result
Dead LetterFailed messages stored separatelyError handling

Protocol Selection Guide

Loading diagram...

Protocol Selection Summary

ProtocolUse Case
RESTPublic APIs, CRUD, simple services
gRPCInternal microservices, performance-critical
WebSocketReal-time bidirectional (chat, gaming)
SSEServer push only (notifications, feeds)
Message queuesAsync, decoupled, reliable