Bird
Raised Fist0
Interview Prepcomputer-networkseasyAmazonGoogleMicrosoftFlipkartSwiggyRazorpayPhonePe

TCP vs UDP - When to Use Each (Reliability vs Speed)

Choose your preparation mode3 modes available
▶
Steps
setup

Start TCP Connection: Client sends SYN

The client initiates a TCP connection by sending a SYN packet to the server to request a connection.

💡 This step starts the reliable connection setup process unique to TCP, which is essential before data transfer.
Line:send_packet(client, server, flags=['SYN'])
💡 TCP requires a handshake before communication, unlike UDP.
📊
TCP vs UDP - When to Use Each (Reliability vs Speed) - Watch the Algorithm Execute, Step by Step
Watching the packet flow step-by-step reveals the fundamental differences in how TCP and UDP operate, making it easier to understand when to use each protocol.
Step 1/12
·Active fill★Answer cell
Client sends SYN to initiate TCP connection
Hop: 1
Client
Server
Router
📦Packet
↗ fromclient
↘ torouter1
📄 payloadSYN
🔌 protocolTCP
🚩 flagsSYN
src→192.168.1.2:12345
dst→192.168.1.10:80
flags→SYN
SYN: client→server (seq=0)
Router forwards SYN packet towards server
Hop: 2
Client
Server
Router
📦Packet
↗ fromrouter1
↘ toserver
📄 payloadSYN
🔌 protocolTCP
🚩 flagsSYN
src→192.168.1.2:12345
dst→192.168.1.10:80
flags→SYN
SYN: client→server (seq=0)
Server replies with SYN-ACK to acknowledge connection request
Hop: 1
Client
Server
Router
📦Packet
↗ fromserver
↘ torouter1
📄 payloadSYN-ACK
🔌 protocolTCP
🚩 flagsSYN, ACK
src→192.168.1.10:80
dst→192.168.1.2:12345
flags→SYN, ACK
SYN: client→server (seq=0)
SYN-ACK: server→client (seq=0, ack=1)
Router forwards SYN-ACK packet to client
Hop: 2
Client
Server
Router
📦Packet
↗ fromrouter1
↘ toclient
📄 payloadSYN-ACK
🔌 protocolTCP
🚩 flagsSYN, ACK
src→192.168.1.10:80
dst→192.168.1.2:12345
flags→SYN, ACK
SYN: client→server (seq=0)
SYN-ACK: server→client (seq=0, ack=1)
Client sends ACK to complete TCP handshake
Hop: 1
Client
Server
Router
📦Packet
↗ fromclient
↘ torouter1
📄 payloadACK
🔌 protocolTCP
🚩 flagsACK
src→192.168.1.2:12345
dst→192.168.1.10:80
flags→ACK
SYN: client→server (seq=0)
SYN-ACK: server→client (seq=0, ack=1)
ACK: client→server (seq=1, ack=1)
Router forwards ACK packet to server
Hop: 2
Client
Server
Router
📦Packet
↗ fromrouter1
↘ toserver
📄 payloadACK
🔌 protocolTCP
🚩 flagsACK
src→192.168.1.2:12345
dst→192.168.1.10:80
flags→ACK
SYN: client→server (seq=0)
SYN-ACK: server→client (seq=0, ack=1)
ACK: client→server (seq=1, ack=1)
Client sends TCP data packet after connection established
Hop: 1
Client
Server
Router
📦Packet
↗ fromclient
↘ torouter1
📄 payloadHello, Server!
🔌 protocolTCP
🚩 flagsPSH, ACK
src→192.168.1.2:12345
dst→192.168.1.10:80
data→Hello, Server!
flags→PSH, ACK
SYN-ACK: server→client (seq=0, ack=1)
ACK: client→server (seq=1, ack=1)
DATA: client→server (seq=1) 'Hello, Server!'
Router forwards TCP data packet to server
Hop: 2
Client
Server
Router
📦Packet
↗ fromrouter1
↘ toserver
📄 payloadHello, Server!
🔌 protocolTCP
🚩 flagsPSH, ACK
src→192.168.1.2:12345
dst→192.168.1.10:80
data→Hello, Server!
flags→PSH, ACK
SYN-ACK: server→client (seq=0, ack=1)
ACK: client→server (seq=1, ack=1)
DATA: client→server (seq=1) 'Hello, Server!'
Server sends ACK to confirm data receipt
Hop: 1
Client
Server
Router
📦Packet
↗ fromserver
↘ torouter1
📄 payloadACK
🔌 protocolTCP
🚩 flagsACK
src→192.168.1.10:80
dst→192.168.1.2:12345
flags→ACK
ACK: client→server (seq=1, ack=1)
DATA: client→server (seq=1) 'Hello, Server!'
ACK: server→client (ack=14)
Client sends UDP data packet without handshake
Hop: 1
Client
Server
Router
📦Packet
↗ fromclient
↘ torouter1
📄 payloadHello via UDP
🔌 protocolUDP
🚩 flags
src→192.168.1.2:54321
dst→192.168.1.10:8080
data→Hello via UDP
DATA: client→server (seq=1) 'Hello, Server!'
ACK: server→client (ack=14)
DATA: client→server 'Hello via UDP'
Router forwards UDP data packet to server
Hop: 2
Client
Server
Router
📦Packet
↗ fromrouter1
↘ toserver
📄 payloadHello via UDP
🔌 protocolUDP
🚩 flags
src→192.168.1.2:54321
dst→192.168.1.10:8080
data→Hello via UDP
DATA: client→server (seq=1) 'Hello, Server!'
ACK: server→client (ack=14)
DATA: client→server 'Hello via UDP'
Server receives UDP data packet without acknowledgment
Hop: 0
Client
Server
Router
DATA: client→server (seq=1) 'Hello, Server!'
ACK: server→client (ack=14)
DATA: client→server 'Hello via UDP'

Key Takeaways

✓ TCP requires a three-way handshake to establish a reliable connection before data transfer.

This handshake process is hard to visualize from code alone but is critical for TCP's reliability.

✓ UDP sends data immediately without connection setup or acknowledgments, prioritizing speed over reliability.

Seeing UDP's simple packet flow clarifies why it is faster but less reliable than TCP.

✓ Routers forward packets transparently regardless of protocol, highlighting the separation of network and transport layers.

Understanding this separation helps learners grasp how protocols interact in layered network models.

Practice

(1/5)
1. When a browser needs to resolve a domain name to an IP address, which DNS server is responsible for directing the query to the appropriate Top-Level Domain (TLD) server?
easy
A. The local DNS resolver on the user's machine
B. The root DNS server
C. The authoritative DNS server for the domain
D. The recursive DNS server operated by the ISP

Solution

  1. Step 1: Understand DNS hierarchy

    The DNS system is hierarchical: root servers are at the top, followed by TLD servers, then authoritative servers.
  2. Step 2: Role of root DNS servers

    Root servers do not resolve domain names directly but direct queries to the correct TLD server based on the domain extension (e.g., .com, .org).
  3. Step 3: Why other options are incorrect

    Local resolvers and recursive servers initiate queries but do not direct to TLD servers; authoritative servers provide final answers for domains.
  4. Final Answer:

    Option B -> Option B
  5. Quick Check:

    Root DNS servers direct queries to TLD servers -> correct
Hint: Root servers direct queries to TLD servers, not resolve domains themselves.
Common Mistakes:
  • Confusing recursive DNS servers with root servers
  • Assuming local resolver directs to TLD servers
  • Thinking authoritative servers handle query routing
2. Trace the sequence of layers a data packet passes through when a user sends an email using SMTP over TCP/IP. Which order correctly represents the encapsulation process from the sender's perspective?
easy
A. Application -> Transport -> Internet -> Network Interface
B. Network Interface -> Internet -> Transport -> Application
C. Internet -> Transport -> Application -> Network Interface
D. Transport -> Application -> Internet -> Network Interface

Solution

  1. Step 1: Understand encapsulation order in TCP/IP model

    Data starts at the Application Layer (SMTP), then is passed down to Transport Layer (TCP) for segmentation and connection management, then to Internet Layer (IP) for routing, and finally to Network Interface Layer for physical transmission.
  2. Final Answer:

    Option A -> Option A
  3. Quick Check:

    Encapsulation order is top-down: Application -> Transport -> Internet -> Network Interface [OK]
Hint: Encapsulation flows top-down from Application to Network Interface
Common Mistakes:
  • Confusing encapsulation with decapsulation order
  • Mixing up Internet and Transport layers
  • Assuming Network Interface is the first layer
3. Trace the sequence of events when a TCP sender receives three duplicate ACKs for the same sequence number. What happens next?
easy
A. The sender ignores duplicate ACKs and continues sending new data segments
B. The sender waits for the retransmission timeout (RTO) before retransmitting the missing segment
C. The sender immediately retransmits the missing segment without waiting for RTO expiration
D. The sender resets the connection due to suspected network failure

Solution

  1. Step 1: Understand duplicate ACKs in TCP

    Three duplicate ACKs indicate packet loss but that the network is still delivering packets, triggering fast retransmission.
  2. Step 2: Analyze sender's response

    Upon receiving three duplicate ACKs, the sender retransmits the missing segment immediately without waiting for RTO.
  3. Step 3: Evaluate options

    The sender ignores duplicate ACKs and continues sending new data segments is incorrect because the sender does not ignore duplicate ACKs. The sender waits for the retransmission timeout (RTO) before retransmitting the missing segment is incorrect because RTO wait is bypassed. The sender immediately retransmits the missing segment without waiting for RTO expiration correctly describes the fast retransmit behavior. The sender resets the connection due to suspected network failure is an extreme reaction not triggered by duplicate ACKs.
  4. Final Answer:

    Option C -> Option C
  5. Quick Check:

    Fast retransmit improves performance by avoiding RTO delays.
Hint: 3 duplicate ACKs -> fast retransmit, no RTO wait.
Common Mistakes:
  • Confusing fast retransmit with waiting for RTO
  • Ignoring duplicate ACKs as noise
  • Assuming connection reset on packet loss
4. Which of the following statements about IPv6 headers is INCORRECT?
medium
A. IPv6 headers have a fixed length of 40 bytes, simplifying processing
B. IPv6 headers include a checksum field to ensure header integrity
C. IPv6 removes the header checksum present in IPv4 to reduce processing overhead
D. IPv6 headers support extension headers to add optional information

Solution

  1. Step 1: Recall IPv6 header structure

    IPv6 headers have fixed length and no checksum field.
  2. Step 2: Analyze options

    IPv6 headers have a fixed length of 40 bytes, simplifying processing is correct; IPv6 headers are fixed at 40 bytes. IPv6 headers include a checksum field to ensure header integrity is incorrect; IPv6 does not have a checksum field. IPv6 removes the header checksum present in IPv4 to reduce processing overhead is correct; checksum was removed to speed up processing. IPv6 headers support extension headers to add optional information is correct; extension headers provide optional info.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    IPv6 relies on upper-layer checksums, not header checksum.
Hint: IPv6 headers have no checksum field [OK]
Common Mistakes:
  • Assuming IPv6 headers have checksums like IPv4
  • Confusing extension headers with fixed header fields
5. Which of the following statements about the OSI Model is INCORRECT?
medium
A. The Data Link Layer is responsible for error detection and correction on the physical link.
B. The Network Layer is responsible for end-to-end reliable data delivery.
C. The Session Layer manages the establishment, maintenance, and termination of sessions.
D. The Physical Layer transmits raw bits over a physical medium.

Solution

  1. Step 1: Review Network Layer responsibilities

    The Network Layer handles routing and forwarding but does NOT guarantee reliable delivery.
  2. Step 2: Identify which layer ensures reliability

    Transport Layer provides end-to-end reliability, not Network Layer.
  3. Step 3: Validate other statements

    Data Link Layer handles error detection/correction on physical links, Session Layer manages sessions, Physical Layer transmits bits.
  4. Final Answer:

    Option B -> Option B
  5. Quick Check:

    Network Layer does NOT guarantee reliability [OK]
Hint: Reliability is Transport Layer's job, not Network Layer's [OK]
Common Mistakes:
  • Assuming Network Layer guarantees reliable delivery
  • Confusing Session Layer with Transport Layer
  • Misunderstanding Data Link Layer's error handling
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard