Day in System Designing
basic · System Designing
Once DNS has resolved a domain name to an IP address, the client needs a protocol to actually transport the data packets across the network. The two primary protocols operating at the Transport Layer of the internet stack are TCP and UDP . Choosing between them is one of the most fundamental trade-offs in system design: prioritizing accuracy versus prioritizing speed . 1. TCP (Transmission Control Protocol) TCP is designed for reliability. It ensures that every single packet of data arrives exactly as it was sent, in the correct order, with zero corruption. Core Characteristics: Connection-Oriented: Before any data can be sent, the client and server must establish a formal connection using a Three-Way Handshake. Ordered Delivery: Packets are tagged with sequence numbers. If packets arrive out of order due to network routing anomalies, TCP rearranges them into the correct order before handing them to the application. Reliable (Acknowledgments): The receiver must send an acknowledgment (ACK) back for every packet received. If the sender doesn’t get an ACK within a certain timeframe, it assumes the packet was lost and retransmits it. Flow & Congestion Control: TCP dynamically monitors network traffic. If the network is congested, it slows down its transmission rate to avoid dropping packets. Common Use Cases: TCP is mandatory for applications where data loss means corruption or failure: HTTP/HTTPS (Web Browsing): Missing a single packet could break an entire HTML page or JavaScript file. File Transfers (FTP/SFTP): Dropping bytes corrupts a downloaded image or executable program. Email (SMTP/IMAP): Missing text could alter the message entirely. Database Queries: Data transactions must be accurate and fully completed. 2. UDP (User Datagram Protocol) UDP is designed for speed and simplicity. It strips away all the overhead of handshakes, checks, and balances, opting to simply blast packets across the network as fast as possible. Core Characteristics: Connectionless: No handshake is required. The client just starts throwing packets (called datagrams) at the server's IP address and port. No Guarantee of Delivery: UDP is a "fire-and-forget" protocol. It does not track whether packets actually arrive, and it does not retransmit lost packets. Unordered: Packets take whatever path is fastest through the internet. They may arrive out of order, and UDP will not fix them. Lightweight Header: A TCP header is at least 20 bytes long to hold all its tracking metadata. A UDP header is only 8 bytes, reducing bandwidth consumption. Common Use Cases: UDP is ideal for real-time applications where speed is more critical than absolute perfection, or where the data is time-sensitive (if a packet is late, it's useless anyway): Video Streaming & VoIP (Zoom, FaceTime): If a packet drops, your screen glitches for a single pixel or a millisecond of audio skips. Waiting for a retransmission would cause the entire video to freeze or lag behind real-time. Online Gaming: If you are playing a fast-paced game, you need immediate position updates. If an update packet is lost, the next packet arriving milliseconds later will override it anyway. DNS Queries: DNS lookups need to be fast. A simple request/response fits neatly inside a single UDP packet. If it fails, the client just tries again. 3. Comparison Summary Feature TCP UDP Connection Requires 3-Way Handshake None (Fire-and-forget) Speed Slower (High overhead) Extremely Fast (Low overhead) Reliability Guaranteed delivery Best-effort, data can be lost Ordering Guaranteed sequential order Completely unordered Data Flow Data is read as a continuous stream Data is sent in discrete chunks (datagrams) 4. The Modern Shift: QUIC and HTTP/3 In modern system design, this rigid line is blurring. Historically, web traffic relied entirely on TCP. However, the TCP handshake plus the TLS (security) handshake causes noticeable latency. To fix this, Google designed a new protocol called QUIC , which forms the basis of HTTP/3 . QUIC actually runs on top of UDP for raw speed, but builds custom reliability, encryption, and ordering mechanisms directly into the application layer . This allows modern web applications to achieve the speed of UDP with the reliability of TCP.