©Code by Samnoon
Blog
← All posts

networking

The networking a backend developer actually uses

How TCP behaves under you, when UDP is the better choice, and why voice traffic rides on RTP over UDP.

6 min read

A while back I wrote a few posts on Medium about the OSI model and transport layer protocols. Reading them again now, they spend a lot of words on the seven layers and very few on the things that actually come up when a service misbehaves. This is the version I'd write today.

The OSI model, briefly

The seven-layer model is a teaching tool. Day to day you deal with four things: Ethernet or Wi-Fi at the bottom, IP for addresses and routing, TCP or UDP for transport, and your application protocol on top.

The layer numbers mostly survive in jargon. A "layer 4 load balancer" looks at IP addresses and ports and nothing else. A "layer 7" one understands HTTP, so it can route on paths and headers. That's about all most people use the numbers for, and it's fine.

TCP: a reliable byte stream, and what it costs

TCP gives you an ordered stream of bytes. You write bytes in on one side, they come out on the other in the same order, and lost packets get retransmitted without you asking. Nearly every TCP behavior that surprises people falls out of how it delivers on that promise.

The handshake. Before any data moves there's a SYN, a SYN-ACK and an ACK. That's a full round trip before your first byte of request, and TLS adds one more (TLS 1.3) or two (TLS 1.2). Over a 150 ms link you've spent 300 to 450 ms before the server has seen anything. This is why connection reuse matters so much: keep-alive, connection pools, HTTP/2. Most "why is the first request slow" questions end here.

Head-of-line blocking. Because the stream is ordered, one lost packet holds up everything behind it until the retransmission arrives. HTTP/2 multiplexes many requests over one TCP connection, so a single lost packet stalls all of them at once. That's a large part of why HTTP/3 moved to QUIC, which runs over UDP and does ordering per stream instead of per connection.

Nagle and delayed ACKs. Nagle's algorithm holds back small writes until the previous data is acknowledged. Meanwhile the receiver may hold its ACK back for a moment, hoping to piggyback it on a response: RFC 1122 allows up to 500 ms, and Linux uses somewhere between 40 and 200 ms. Put the two together and a protocol that sends a request in two small writes can pick up a strange, very consistent extra delay, often exactly 40 ms on Linux. TCP_NODELAY disables Nagle, and most HTTP clients and database drivers already set it. If you're writing raw sockets, you probably want it too.

TIME_WAIT. Whichever side closes the connection first keeps it in TIME_WAIT for a while (60 seconds on Linux) so stray packets from the old connection can't land in a new one. A service that opens and closes thousands of short connections a second to the same backend can run out of local ports this way. ss -tan state time-wait | wc -l tells you if that's happening. The fix is nearly always connection reuse, not kernel tuning.

Keepalive. TCP keepalive is off unless the application turns it on, and even then Linux waits two hours before the first probe. Firewalls and NAT gateways drop idle connections long before that. If you hold idle connections open for a long time (database pools, message brokers), enable keepalive with shorter timers or have the application send its own heartbeats. Otherwise the first request after a quiet night hangs until it times out.

UDP: packets, and nothing else

UDP sends datagrams. No connection, no retransmission, no ordering, no congestion control. A packet might arrive once, not at all, twice, or after the one you sent after it.

That sounds strictly worse than TCP until the data has a deadline. A retransmitted packet that arrives after it's useful is worse than no packet at all, because it used bandwidth and, in an ordered protocol, held up everything behind it.

DNS uses UDP because a query fits in one packet and asking again is cheap. Games use it for position updates because the next update makes the previous one irrelevant. Voice uses it for the same reason.

Why voice runs on RTP over UDP

A call sends audio in small chunks, usually one every 20 ms. With G.711, the codec most phone networks still speak, that's 160 bytes of audio per packet: 8,000 samples a second, one byte each, 20 ms at a time.

If one packet goes missing, the listener hears a tiny gap, and the receiver's jitter buffer can paper over it with packet loss concealment. If TCP retransmitted it instead, the packet would arrive at least a round trip late, and every packet behind it would wait. A 20 ms blip becomes a few hundred milliseconds of silence followed by a burst of stale audio. For live speech, late is the same as lost, only more annoying.

UDP knows nothing about audio, so voice puts RTP on top of it. The RTP header is 12 bytes and carries what the receiver needs to rebuild the stream:

FieldWhat it's for
Sequence numberSpotting loss and reordering
TimestampPlaying samples at the right moment, measuring jitter
SSRCTelling streams apart (one per source)
Payload typeWhich codec the payload uses

RTCP runs alongside and reports loss, jitter and round-trip time back to the sender. When a voice platform shows you call quality numbers, they usually come from RTCP.

A bit of arithmetic that comes up more than you'd think: 160 bytes of audio, plus 12 for RTP, 8 for UDP and 20 for IPv4, is 200 bytes per packet. Fifty packets a second makes 80 kbit/s on the wire for a codec that's nominally 64 kbit/s, before Ethernet framing. Someone always asks how many calls fit on a link, and that's where the answer starts.

The audio isn't the whole call, though. Setting the call up (who's calling whom, which codec, which ports) is the signalling protocol's job, usually SIP these days, and it travels separately from the RTP stream.

Tools that answer most questions

ss -tanp lists TCP connections with their state and the process that owns each one. It replaced netstat years ago and it's on every modern Linux box.

tcpdump -ni any port 5432 -w db.pcap captures traffic you can open in Wireshark later. The -n matters: without it tcpdump does reverse DNS on every address, which is slow and adds DNS traffic to the very capture you're trying to read.

mtr example.com combines ping and traceroute and keeps running, so you can see which hop starts dropping packets.

And this one answers most "the API is slow" questions on its own:

curl -s -o /dev/null \
  -w 'connect %{time_connect}s  tls %{time_appconnect}s  first byte %{time_starttransfer}s\n' \
  https://api.example.com/health

It splits a slow request into TCP connect, TLS handshake and time to first byte. If connect is slow, look at the network. If TLS is slow, look at the handshake (or the lack of connection reuse). If first byte is slow, it's the server, and you can stop blaming the network.

None of this needs the OSI model memorized. It needs a packet capture and some patience.

References

#networking #backend