CacheU
High Level Design

Network Protocols in High Level Design (HLD)

A comprehensive guide to network protocols used in distributed systems and backend architectures. Covers TCP, UDP, HTTP, HTTPS, WebSocket, gRPC, DNS, and other protocols with architecture diagrams, real-world examples, comparisons, and JavaScript examples.

Network Protocols in High Level Design (HLD)

In distributed systems, services do not exist in isolation. They communicate across networks.
The rules governing how data is transmitted between systems are called network protocols.

A network protocol defines:

  • How data is formatted
  • How messages are transmitted
  • How errors are handled
  • How connections are established and terminated

In High Level Design, selecting the correct protocol directly affects:

System PropertyImpact
LatencyHow fast services communicate
ReliabilityPacket loss handling
ScalabilityConnection handling
SecurityEncryption and authentication
PerformanceBandwidth and CPU usage

Real World Analogy

Think of network protocols like languages used by different departments in a company.

ProtocolAnalogy
TCPRegistered courier delivery
UDPLoudspeaker announcements
HTTPStandard office communication
WebSocketsPhone call
gRPCStructured API contract
DNSPhonebook lookup

Each protocol serves a different communication need.


Network Protocol Stack

Protocols operate in layers. The most common model used in system design is the TCP/IP model.

Diagram
flowchart TD Application["Application Layer
(HTTP, WebSocket, gRPC, FTP, SMTP)"] Transport["Transport Layer
(TCP, UDP)"] Internet["Internet Layer
(IP)"] Network["Network Access Layer
(Ethernet, WiFi)"] Application --> Transport Transport --> Internet Internet --> Network

Each layer has a responsibility.

LayerResponsibility
ApplicationDefines communication rules
TransportEnsures delivery
InternetHandles addressing and routing
NetworkPhysical data transmission

1 Transport Layer Protocols

Transport protocols determine how data travels between services.

The two primary transport protocols are:

ProtocolKey Feature
TCPReliable communication
UDPFast but unreliable

TCP (Transmission Control Protocol)

TCP is a connection-oriented protocol that guarantees delivery.

Features:

  • Reliable delivery
  • Ordered packets
  • Retransmission
  • Flow control
  • Congestion control

TCP Connection Lifecycle

TCP uses a three-way handshake.

Diagram
sequenceDiagram participant Client participant Server Client->>Server: SYN Server->>Client: SYN-ACK Client->>Server: ACK

Connection established.


TCP Data Transfer

Diagram
sequenceDiagram Client->>Server: Packet 1 Server->>Client: ACK Client->>Server: Packet 2 Server->>Client: ACK

If packet is lost → TCP retransmits.


When TCP Is Used

TCP is used when data correctness is critical.

Examples:

SystemReason
Web APIsData must not be lost
DatabasesStrong consistency
Payment systemsReliability
File transferComplete delivery

UDP (User Datagram Protocol)

UDP is connectionless and faster than TCP.

Characteristics:

  • No connection setup
  • No guaranteed delivery
  • No ordering
  • Minimal overhead

UDP Data Flow

Diagram
sequenceDiagram Client->>Server: Packet Client->>Server: Packet Client->>Server: Packet

No acknowledgments.

Packets may:

  • arrive out of order
  • get lost
  • be duplicated

When UDP Is Used

UDP is ideal for real-time communication.

Examples:

SystemReason
Video streamingSome loss acceptable
Online gamingUltra-low latency
Voice callsReal-time communication
DNS queriesFast resolution

Application Layer Protocols

Application layer protocols define how applications communicate.

Common protocols:

ProtocolPrimary Use
HTTPWeb communication
HTTPSSecure HTTP
WebSocketReal-time communication
gRPCHigh-performance service communication
DNSName resolution
SMTPEmail sending

HTTP (HyperText Transfer Protocol)

HTTP is the foundation of the web.

It follows a request-response model.

Diagram
sequenceDiagram Browser->>Server: HTTP Request Server->>Browser: HTTP Response

Example request:

GET /users HTTP/1.1
Host: api.example.com

Example response:

HTTP/1.1 200 OK
Content-Type: application/json

HTTP Characteristics

FeatureDescription
StatelessEach request independent
Text-basedHuman readable
Request/ResponseClient initiates communication

HTTP Methods

MethodPurpose
GETRetrieve data
POSTCreate resource
PUTReplace resource
PATCHUpdate resource
DELETERemove resource

Example:

fetch("https://api.example.com/users")

HTTPS (HTTP Secure)

HTTPS is HTTP over TLS encryption.

It ensures:

  • Encryption
  • Integrity
  • Authentication

Architecture:

Diagram
flowchart LR Client --> TLS TLS --> HTTP HTTP --> Server

Benefits:

BenefitExplanation
PrivacyPrevents eavesdropping
SecurityPrevents tampering
AuthenticationVerifies server identity

WebSocket

WebSocket enables persistent bidirectional communication.

Unlike HTTP:

  • Connection stays open
  • Server can push data
  • Real-time communication

WebSocket Flow

Diagram
sequenceDiagram participant Client participant Server Client->>Server: HTTP Upgrade Request Server-->>Client: 101 Switching Protocols Client->>Server: WebSocket Message Server->>Client: WebSocket Message

WebSocket Use Cases

SystemExample
Chat applicationsWhatsApp Web
Live dashboardsStock prices
Multiplayer gamesReal-time updates
NotificationsLive alerts

JavaScript WebSocket Example

const socket = new WebSocket("wss://example.com/socket");
 
socket.onopen = () => {
  socket.send("Hello Server");
};
 
socket.onmessage = (event) => {
  console.log("Received:", event.data);
};

gRPC

gRPC is a high-performance RPC framework developed by Google.

Uses:

  • HTTP/2
  • Protocol Buffers
  • Binary serialization

gRPC Architecture

Diagram
flowchart LR Client --> gRPCStub gRPCStub --> HTTP2 HTTP2 --> ServerStub ServerStub --> Service

Why gRPC Is Fast

FeatureBenefit
Binary protocolSmaller payload
HTTP/2 multiplexingParallel requests
StreamingContinuous data

gRPC vs REST

FeatureRESTgRPC
ProtocolHTTP/1.1HTTP/2
Data formatJSONProtobuf
PerformanceModerateHigh
StreamingLimitedNative

DNS (Domain Name System)

DNS translates domain names into IP addresses.

Example:

google.com → 142.250.190.14

DNS Resolution Flow

Diagram
sequenceDiagram Browser->>DNS Resolver: Query google.com Resolver->>Root DNS: Where is .com? Root DNS->>Resolver: Ask TLD Resolver->>TLD DNS: Where is google.com? TLD->>Resolver: Ask Authoritative DNS Resolver->>Authoritative DNS: Request IP Authoritative DNS->>Resolver: IP Address Resolver->>Browser: IP Address

SMTP (Email Protocol)

SMTP is used for sending emails.

Architecture:

Diagram
flowchart LR EmailClient --> SMTPServer SMTPServer --> MailServer MailServer --> RecipientMailbox

Protocol Selection in System Design

Choosing the right protocol depends on system requirements.

RequirementRecommended Protocol
Reliable APIsHTTP/TCP
Real-time messagingWebSocket
MicroservicesgRPC
Video streamingUDP
Domain resolutionDNS

Protocols in Microservices Architecture

Diagram
flowchart LR Client --> API_Gateway API_Gateway --> ServiceA API_Gateway --> ServiceB ServiceA --> ServiceC ServiceB --> ServiceC

Protocols used:

CommunicationProtocol
Client → GatewayHTTPS
Gateway → ServicesHTTP/gRPC
Service → ServicegRPC
Internal discoveryDNS

Protocol Performance Comparison

ProtocolSpeedReliabilityUse Case
TCPMediumHighAPIs
UDPVery HighLowStreaming
HTTPMediumHighWeb
WebSocketHighHighReal-time
gRPCVery HighHighMicroservices

Real World Architecture Example

Example: YouTube streaming system

Diagram
flowchart TB User --> CDN CDN --> EdgeServer EdgeServer --> VideoServer VideoServer --> Storage

Protocols used:

LayerProtocol
Browser → CDNHTTPS
CDN → EdgeHTTP
StreamingUDP
Service communicationgRPC

Key Takeaways

  • Network protocols define how services communicate across networks
  • TCP ensures reliable delivery, while UDP prioritizes speed
  • HTTP/HTTPS power most web APIs
  • WebSocket enables real-time communication
  • gRPC is widely used in microservices architectures
  • DNS resolves domain names to IP addresses
  • Choosing the correct protocol is critical for performance, scalability, and reliability

Understanding network protocols is fundamental to designing large-scale distributed systems.