← Back to Index

3. Protocols

TCP vs UDP

FeatureTCPUDP
ConnectionConnection-oriented (3-way handshake)Connectionless
ReliabilityGuaranteed delivery via ACKs/retransmissionBest-effort, no guarantees
OrderingSequenced deliveryNo ordering guarantees
SpeedSlower (overhead)Faster (low overhead)
Use CasesWeb (HTTP), email (SMTP), file transfer (FTP)Streaming video, VoIP, gaming, DNS
# TCP 3-way handshake
Client → SYN            → Server
Client ← SYN+ACK        ← Server
Client → ACK            → Server
# Connection established, data transfer begins

HTTP / HTTPS

HTTP is the foundational protocol of the web. HTTPS adds TLS encryption on top of HTTP. Key concepts:

# HTTP Request
GET /api/users/123 HTTP/1.1
Host: example.com
Authorization: Bearer token123
Accept: application/json

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

{"id": 123, "name": "Alice"}

REST (Representational State Transfer)

REST is an architectural style for designing networked applications. Key principles:

Real-Time Protocols

Modern API Protocols

# gRPC service definition (Protocol Buffers)
service UserService {
  rpc GetUser (GetUserRequest) returns (User);
  rpc ListUsers (ListUsersRequest) returns (stream User);
}

message GetUserRequest { string user_id = 1; }
Exercise: Your team is building a real-time collaborative document editor (like Google Docs). Which protocols would you use for: a) sending edits between users, b) loading the document from the server, c) notifying users of presence (who is editing). Justify each choice.