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
| NO. | STEP | FROM | TO | TYPE | INFO |
|---|---|---|---|---|---|
| 1 | 01 | client | server | SYN | seq=1000, win=64240, MSS=1460 |
| 2 | 02 | server | client | SYN-ACK | seq=5000, ack=1001 |
| 3 | 03 | client | server | ACK | ack=5001 → connection established |
| 4 | 04 | client | server | PSH,ACK | seq=1001, len=100 (query bytes) |
| 5 | 04 | server | client | ACK | ack=1101 (got bytes 1001–1100) |
| 6 | 05 | client | server | FIN,ACK | seq=1101, ack=5001 |
| 7 | 05 | server | client | ACK | ack=1102 |
| 8 | 05 | server | client | FIN,ACK | seq=5001, ack=1102 |
| 9 | 05 | client | server | ACK | ack=5002 → client in TIME_WAIT |
- A client (say, a Python app) wants to talk to PostgreSQL on port 5432. No connection exists yet.
- The client sends SYN with its initial sequence number (1000 here for readability; real ones are random 32-bit values).
- The server replies SYN-ACK: its own sequence number, and an acknowledgment of the client's (1000 + 1).
- The client sends ACK. Both sides are ESTABLISHED. That cost one full round trip before any data.
- Data flows. Every byte is numbered, and the receiver acknowledges what it got. Missing bytes are resent.
- 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
- TCP/IP Illustrated, Volume 1: The Protocols: W. Richard Stevens, Kevin Fall
- Computer Networking: A Top-Down Approach, Kurose and Ross: chapter 3 (transport layer)
- High Performance Browser Networking: Building Blocks of TCP: Ilya Grigorik, free online