Fundamentals 14 min read

TCP vs UDP: Deep Dive for Java Interview Success

This guide breaks down the core differences between TCP and UDP—including connection handling, reliability, header overhead, transmission mode, and real‑world use cases—while providing interview focus points, concise answer tables, detailed analysis, common follow‑up questions, and handy mnemonics for Java developers.

Java Architect Handbook
Java Architect Handbook
Java Architect Handbook
TCP vs UDP: Deep Dive for Java Interview Success

Interview Focus Points

Fundamental mastery : Demonstrate understanding of connection style, transmission mechanism, header overhead and their practical impact, not just “TCP is reliable, UDP is not”.

Scenario selection ability : Explain when to choose TCP versus UDP based on reliability versus real‑time performance requirements.

Depth of principle : Discuss OSI layers, header structures, congestion control and flow control to show genuine comprehension of transport‑layer design.

Core Answer

The essential distinction is that TCP is connection‑oriented while UDP is connection‑less . This leads to differences in reliability, ordering, speed, header size, flow/congestion control and transmission mode.

Connection : TCP establishes a three‑way handshake; UDP sends packets directly.

Reliability : TCP provides ACK, retransmission, timeout handling and congestion control; UDP offers no guarantees.

Ordering : TCP guarantees in‑order delivery via sequence numbers; UDP may deliver out of order.

Speed : TCP incurs handshake and ACK overhead, making it slower; UDP has minimal overhead, giving lower latency.

Header overhead : TCP header is at least 20 bytes; UDP header is 8 bytes.

Flow/ congestion control : TCP implements both; UDP does not.

Transmission mode : TCP is a byte‑stream (no inherent message boundaries); UDP is a datagram (preserves message boundaries).

Typical use cases : TCP – file transfer, web browsing, email, remote login, database connections. UDP – video streaming, DNS, online gaming, voice calls, IoT sensor data, QUIC/HTTP/3.

In‑Depth Analysis

1. Connection: Three‑Way Handshake vs. Connection‑less

TCP’s three‑step handshake (SYN, SYN‑ACK, ACK) confirms both sides can send and receive, synchronises initial sequence numbers and prevents stale SYN packets. This adds roughly 1.5 RTT before data can flow.

TCP three‑way handshake
TCP three‑way handshake

UDP skips this entirely and sends packets immediately, resulting in lower initial latency.

UDP connectionless
UDP connectionless

2. Reliability: Full Guarantees vs. Bare‑bones

TCP provides a suite of mechanisms:

Sequence numbers for ordering

ACKs for confirmation

Timeout‑based retransmission (RTO)

Sliding window for flow control

Congestion control (slow start, avoidance, fast retransmit, fast recovery)

Checksum for error detection

TCP reliability mechanisms
TCP reliability mechanisms

UDP lacks these mechanisms; it simply sends packets without acknowledgment or retransmission.

UDP no reliability
UDP no reliability

3. Header Overhead: 20 bytes vs. 8 bytes

TCP header fields (source/destination ports, sequence/ack numbers, data offset, flags, window size, checksum, urgent pointer, etc.) total at least 20 bytes.

TCP header
TCP header

UDP header contains only source/destination ports, length and checksum – 8 bytes.

UDP header
UDP header

4. Transmission Mode: Byte‑stream vs. Datagram

TCP treats data as a continuous byte stream. Applications must implement framing to handle message boundaries and to solve “sticky packet” (粘包) and “packet splitting” (拆包) issues.

TCP byte stream
TCP byte stream

UDP sends discrete datagrams, preserving message boundaries and avoiding sticky‑packet problems.

UDP datagram
UDP datagram

5. Application Scenario Comparison

When reliability matters (choose TCP)

HTTP/HTTPS – complete web content required

FTP – file transfer must be loss‑free

SMTP – accurate email delivery

SSH – commands and output must be intact

Database connections – SQL statements and results must be exact

When speed and loss tolerance matter (choose UDP)

DNS queries – small packets, fast response prioritized

Live video streaming – occasional frame loss acceptable, low latency essential

Voice calls – stutter worse than slight delay

Online gaming – real‑time priority, lost packets can be interpolated

IoT sensor data – high volume, occasional loss tolerable

QUIC/HTTP/3 – UDP with application‑level reliability for best of both worlds

6. QUIC: UDP’s Comeback

QUIC runs on UDP to avoid TCP’s head‑of‑line blocking and slow handshake. It implements reliability (ACK, retransmission, congestion control) at the application layer and integrates TLS 1.3 for built‑in security and connection migration.

QUIC protocol
QUIC protocol

High‑Frequency Follow‑up Questions

When to use TCP vs. UDP? Use TCP for high reliability (file transfer, web pages). Use UDP for low latency where occasional loss is acceptable (video, gaming). Modern trend: UDP + application‑level reliability (e.g., QUIC).

Why three‑way handshake? Why not two? Two‑way cannot confirm the client’s receive capability nor prevent stale SYN packets. Three‑way ensures both sides can send/receive and synchronises initial sequence numbers.

Can UDP be made reliable? Yes, by adding ACK, timeout retransmission, sequence numbers at the application layer—QUIC is a real‑world example, though it adds development complexity.

What is TCP “sticky packet” and how to solve it? TCP’s byte‑stream can merge multiple small packets (粘包) or split large ones (拆包). Solutions: fixed‑length framing, delimiters, or length‑field + payload.

Common Interview Variants

Why is video streaming built on UDP instead of TCP?

Can TCP and UDP share the same port?

Why is HTTP/3 based on UDP rather than TCP?

How to make UDP reliable?

Memory Mnemonics

TCP : Connection‑oriented, reliable, ordered, three‑handshake, flow & congestion control, byte‑stream (sticky‑packet), 20‑byte header.

UDP : Connection‑less, unreliable, unordered, no control, datagram (message boundaries), 8‑byte header.

Selection Principle : Choose TCP for reliability, UDP for speed, and QUIC when both are needed.

Summary

TCP acts as a “reliable steward”: connection‑oriented, ordered delivery with flow and congestion control, suitable for file transfer, web browsing and any scenario demanding data accuracy. UDP behaves like a “reckless courier”: connection‑less, fast, low‑overhead, ideal for real‑time video, gaming and DNS. The emerging trend combines UDP’s speed with application‑level reliability (e.g., QUIC) to achieve both performance and correctness.

Code example

1.
我的私密学习小圈子,从0到1手撸企业实战项目~
2.
如何画出一张优秀的架构图?(老鸟必备)
3.
面试官:什么是跨域访问问题,怎么解决?
4.
实战!Arthas 定位 接口的超时问题,直接起飞!
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

tcpNetworkingUDPProtocol ComparisonJava Interview
Java Architect Handbook
Written by

Java Architect Handbook

Focused on Java interview questions and practical article sharing, covering algorithms, databases, Spring Boot, microservices, high concurrency, JVM, Docker containers, and ELK-related knowledge. Looking forward to progressing together with you.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.