$ man 7 tcp # RFC 791 (IP) · RFC 9293 (TCP)

TCP/IP

TCP/IP is the protocol suite the internet runs on. IP (Internet Protocol) moves individual packets from one machine to another by address, with no promises: packets can be lost, duplicated or reordered. TCP (Transmission Control Protocol) runs on top and turns that unreliable packet service into a reliable, ordered byte stream between two programs. Your database driver, HTTP client and Kafka producer all sit on a TCP connection.

The layers

Layer Example Unit Addresses with
Application HTTP, PostgreSQL wire protocol, Kafka message URLs, topic names
Transport TCP, UDP segment / datagram port (5432, 443, 9092)
Internet IPv4, IPv6 packet IP address (10.0.1.20, 2001:db8::1)
Link Ethernet, Wi-Fi frame MAC address

Each layer wraps the one above in its own header. A connection is identified by five values: protocol, source IP, source port, destination IP, destination port. That is how one server can hold thousands of connections on port 5432 at once.

The OSI model has seven layers. TCP/IP merges OSI’s top three into “application”, which matches how real software is built.

A TCP connection, step by step

$ tcpdump -n 'tcp port 5432'
TCP connection: handshake, data, teardown
CLIENT 10.0.0.5:52114 SERVER 10.0.1.20:5432 SYN_SENT SYN seq=1000 SYN-ACK seq=5000 ack=1001 ACK ack=5001 ESTABLISHED ESTABLISHED data seq=1001 len=100 ACK ack=1101 FIN seq=1101 ACK ack=1102 FIN seq=5001 ACK ack=5002 TIME_WAIT (2 x MSL)
NO.STEPFROMTOTYPEINFO
101clientserverSYNseq=1000, win=64240, MSS=1460
202serverclientSYN-ACKseq=5000, ack=1001
303clientserverACKack=5001 → connection established
404clientserverPSH,ACKseq=1001, len=100 (query bytes)
504serverclientACKack=1101 (got bytes 1001–1100)
605clientserverFIN,ACKseq=1101, ack=5001
705serverclientACKack=1102
805serverclientFIN,ACKseq=5001, ack=1102
905clientserverACKack=5002 → client in TIME_WAIT
  1. A client (say, a Python app) wants to talk to PostgreSQL on port 5432. No connection exists yet.
  2. The client sends SYN with its initial sequence number (1000 here for readability; real ones are random 32-bit values).
  3. The server replies SYN-ACK: its own sequence number, and an acknowledgment of the client's (1000 + 1).
  4. The client sends ACK. Both sides are ESTABLISHED. That cost one full round trip before any data.
  5. Data flows. Every byte is numbered, and the receiver acknowledges what it got. Missing bytes are resent.
  6. Closing takes four segments: each side sends FIN and ACKs the other's. The side that closed first waits in TIME_WAIT.

What TCP gives you, and what it costs

  • Reliability. Every byte has a sequence number. Unacknowledged data is retransmitted after a timeout or after duplicate ACKs.
  • Ordering. The receiver reassembles bytes in order before handing them to the app.
  • Flow control. The receiver advertises a window: how much it can buffer. A slow consumer slows the sender.
  • Congestion control. The sender probes the network’s capacity with an algorithm such as CUBIC (the Linux default) or BBR, starting with slow start and backs off on loss.

The costs: one round trip for the handshake before any data, and head-of-line blocking: if one segment is lost, everything after it waits, even if it already arrived. That second cost is why HTTP/3 moved to QUIC over UDP.

Common mistakes

  • Opening a new connection per query. Each one costs a handshake, often plus TLS and database authentication. Use a connection pool (PgBouncer, HikariCP, SQLAlchemy’s pool).
  • No timeouts. A peer that vanishes without a FIN leaves your socket waiting forever. Set connect and read timeouts, and enable TCP keepalive for long-idle connections.
  • Running out of ephemeral ports. A client that opens and closes thousands of short connections piles up sockets in TIME_WAIT. Reuse connections instead of tuning the kernel.
  • Confusing “connection refused” with “timeout”. Refused (an RST) means the host is up but nothing listens on that port. A timeout usually means a firewall silently dropped the SYN.

Try it

ss -tan state established        # open TCP connections on Linux
nc -vz db1.internal 5432         # can I reach the port at all?
sudo tcpdump -n -i any 'tcp port 5432 and tcp[tcpflags] & (tcp-syn|tcp-fin) != 0'

Learning Resources

Books

Specifications

Videos

See also: UDP, HTTP, Firewalls.