Have you ever wondered how much overhead there is in talking over the internet? Or how many bits of metadata, or headers, travel alongside a single bit of data when you send it?
In this post, I want to dig a little deeper into some of the details of networking that nobody really talks about, by answering the 3 questions below.
The questions (if you don’t have the patience to read the whole thing, the answer is hidden as a spoiler under each question!):
1. To send one byte of useful data, how many bits of actual data move across the network at each layer? In other words, what percentage of network traffic is useful data?
- At layer 7, with HTTP, we have to send 224 bits.
- At layer 4, with TCP, we have to send 416 bits. (256 for the syn and 168 for the ack-data)
- At layer 3, with IPv4, we have to send 168 bits.
- At layer 2, with Wi-Fi 5 (802.11ac), we have to send 344 bits.
- At layer 1, with Wi-Fi 5 (802.11ac), we have to transmit the equivalent of 78003 bits of signal.
2. To send one byte at layer 7 as cheaply as possible, how many bits of data get transferred?
For one byte of data, 162246 bits move across the network, which puts the internet’s efficiency at less than 0.01% of the network’s actual capacity!3. In the most efficient case possible, what percentage is the network’s utility?
That one’s hard! I can’t be bothered right now, but I’ll just leave the max packet sizes here; maybe I’ll get to it later:
- HTTP: no limit
- TCP: 65,535 bytes
- IPv4: normal frame: 1400B, jumbo frame: 9216B
- Wifi 5 (802.11ac) [layer2]: 2,304 bytes
Networking overview#
To do these calculations, we need to be familiar with the general concepts of networking (if you already know this stuff, skip to the next heading).
According to the OSI model, a network is made up of 7 layers. What exactly each of these layers does and why it exists isn’t really our concern here, but what we do need to know is that, contrary to popular belief, these layers aren’t stacked on top of each other; they’re nested inside each other.
In general, at every layer, your job is to get a bunch of data, in the form of a packet (the unit of data at each layer, like a fragment, frame, packet, segment, stream, …), from a source to a destination. In very simple terms, you can think of a packet like this:
-----------------------------
| header | payload | footer |
-----------------------------
At every layer, each packet usually has a header section that marks the start of the packet and carries some important information about it, such as the source and destination addresses and the size of the payload. After the header comes the payload itself, which can be any sequence of bits and doesn’t matter to us. After the payload comes the footer, which marks the end of the packet. Of course, neither the header nor the footer is mandatory; whether they exist depends on the protocol you’re using to move data at that layer. For example, most protocols from layer 3 up usually don’t have a footer, so we can treat the footer size as 0 bits.
The important point here is that the payload of a packet at layer N is usually an entire packet at layer N+1, and that’s why we say network layers are nested inside each other rather than stacked on top of each other:
(The diagram below looks better on a laptop.)
+----+------------------------------------------------------------+----+
| | +====+==============================================+====+ | |
| | | | +~~~~+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+~~~~+ | | | |
|L1 h| |L2 h| |L3 h| payload (other layers packets) |L3 f| |L2 f| |L1 f|
| | | | +~~~~+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+~~~~+ | | | |
| | +====+==============================================+====+ | |
+----+------------------------------------------------------------+----+
So, to answer these questions, what we actually need to do is compute the size of the header and footer at each layer and add them all up. So what are these layers?
- Physical layer
- Data link layer
- Network layer
- Transport layer
- Session layer
- Presentation layer
- Application layer
Layers 5 and 6 are usually lumped in with layer 7, because depending on the application, some of them may not be needed at all. Here, we’re going to go through layers 1 to 4, and for 5, 6 and 7 we’ll just look at a simple HTTP request, in which layers 5 and 6 effectively play no role.
One more thing to know is that every layer has lots of protocols, and it’s the protocols that define the shape and size of a packet. Effectively, packets belong to protocols, not to layers. Here, I’ll only look at one protocol from each layer.
Defining the problem#
As I said, we’re going to compute the overhead of layers 1 to 4 and 7 one by one and add them up. To do that, we need to know exactly which protocol is used at each layer, which means our scenario has to be precisely defined. So here’s the scenario I’ll go with:
I want to connect to the internet from my laptop over Wi-Fi and send one byte of data to a server.
In this transfer, I only want to send the bits that have to be sent, and unless I’m forced to, I won’t use any of the protocols’ extra options.
Now let’s go through the layers one by one.
7. Application layer#
This layer is our entry point into the world of networking. At this layer, we’ll use HTTP/1.1 to send data. In this protocol, the simplest request is a GET on “/”. To keep the packet as small as possible, let’s also assume the destination domain is as short as it can possibly be. There are no single-letter TLDs; the shortest ones have two letters. Out of all the two-letter TLDs, I picked the illustrious .ir. There’s no such limit on the domain name itself, so we can have a single-letter domain.
So, all in all, we can assume we’re sending the following request:
GET / HTTP/1.1
Host: x.ir
This request needs 25 characters in total:
| Component | Content | Characters |
|------------|------------|------------|
| Method | GET | 3 |
| Space | (space) | 1 |
| URI | / | 1 |
| Space | (space) | 1 |
| Version | HTTP/1.1 | 8 |
| New line | \r\n | 2 |
| Host | Host: x.ir | 10 |
| New line | \r\n | 2 |
| End of req | \r\n | 2 |
which comes to 28 bytes, or 224 bits.
4. Transport layer#
At this layer, we’re going to look at TCP. Every packet in this protocol has a mandatory 160-bit header (meaning it’s there in every single packet), plus a large number of options that vary depending on the type of packet. What’s important and unique about this protocol is the 3-way handshake, during which the server and the client establish a connection with each other.
In this process, the client first sends a syn packet, then the server sends a syn-ack packet, and finally the client sends the third packet, the ack. This way, both the server and the client are sure the connection has been established, and they start transferring data. In modern TCP implementations, the client starts sending data along with the ack packet, reusing the ack packet to improve efficiency.
During this 3-way handshake, the server and the client agree on a set of parameters, and this negotiation happens through options in the header. The minimal set of options that must be sent in the handshake is MSS, WSCALE, SACK Permitted and NoOp.
The MSS option specifies the maximum size of a TCP packet on this connection. (4B)
The WSCALE option is used to set the window size. (3B)
SACK Permitted is an option for improving efficiency; we don’t care about it here, but it gets sent anyway. (2B)
And finally, since the TCP header size has to be a multiple of 4, the options are padded with NoOp. So in our particular example, NoOp comes to 3B.
In total, our options add up to 12 bytes, or 96 bits. So:
- The first packet, the syn, is 160+96=256 bits in total.
- The second packet, the syn-ack coming from the server, is 160+96=256 bits.
- The third packet, the ack+data sent by the client, is 160+224 (the size of the HTTP packet).
Of these 3 packets, the client sends the first and the third, so the total number of bits sent at layer 4 comes to 640 bits.
3. Network layer#
At this layer, we’re dealing with IPv4. This layer is relatively simpler. The IPv4 header is 20 bytes per packet and holds things like the source and destination IPs, the packet’s TTL, and so on. The only thing we need to watch out for is that since we had 2 packets at layer 4, we also need 2 packets at layer 3, one for each of the layer 4 packets.
So each packet is 20 bytes of IP header plus the size of the packet at layer 4, which means:
(160+256) + (160+384) = 960
2. Data link layer#
At this layer, we gradually leave the theoretical world of bits behind, and the concerns become more physical. For example, it now matters how we’re connecting to the internet. Since in this example the laptop connects to the internet with a Wi-Fi card, we’ll look at the Wi-Fi 5 (802.11ac) protocol.
In this protocol, packets have a footer in addition to a header. At layer 2, packets in this protocol start with a 240-bit MAC header. After that come 64 bits of LLC/SNAP, which more or less say which layer 3 protocol the data in the packet’s payload belongs to. Then comes the actual data, or payload, which here is the IP packets, followed by a 32-bit FCS, which counts as the footer and is used to check for bit flips and errors in the transmitted data.
So in total, each packet has 336 bits of overhead, and since we had 2 layer 3 packets, we have 2 packets here too, and the total amount of data sent comes to:
(336+(160+256)) + (336+(160+(160+224))) = 1632 bit
1. Physical layer#
This layer is a bit different from the layers above it, because its focus is on physically transmitting the data. That is: how do I send a bit of data as an electromagnetic wave through a noisy medium?
And at this layer, specifically for the Wi-Fi protocol, it’s really important to make sure that if we send a signal, it reaches the destination correctly and accurately, and its value doesn’t get changed by noise along the way.
That’s why, at this layer, the Wi-Fi protocol involves a lot of waiting, which goes something like this: “Let me wait a bit and see whether anyone else is doing something and making the medium noisy; if not, I’ll send.” So our overhead really comes in 2 parts:
- Like all the higher layers, the overhead of headers, footers, and so on.
- The time we spend waiting, during which we’re effectively not transferring any data, to make sure the data we’re about to send reaches the destination correctly.
For the first part, we need to see what happens to the packets at layer 1:
1.1. At this layer, for error correction, 1 parity bit is sent for every 3 bits (FEC), so the length of every packet effectively gets multiplied by 4/3.
1.2. Another thing that matters a lot at this layer, and a concern we only see here, is the pattern of the bit sequence. Since we’re actually dealing with waves here, some patterns, like runs of consecutive 1s or consecutive 0s, cause trouble because they keep the receiver from correctly detecting the frequency (it can’t tell whether it just received 5 zeros in a row or 6!). So there are usually some techniques that try to break up these runs. One of them is 4b-5b, which says: let’s map every 4 bits to a 5-bit sequence. That gives us enough room to pick those sequences so that they don’t have consecutive 0s! This algorithm was used in Ethernet, and its problem was that it increased the amount of data sent by 25%!
The solution Wi-Fi uses is to XOR the data bits with a series of random bits. That makes consecutive 0s much less likely, and we just send this random key at the start and end of the message so the receiver can decode it again. So with this method, 16 bits also get added to both the start and the end of each layer 1 packet.
1.3. Another problem is that at this layer, bits aren’t transmitted one by one but symbol by symbol, and each symbol holds 3120 bits. If the data we’re sending is less than that, it has to be padded up to 3120 bits!
So, all in all, the amount of data sent for our packets comes to:
layer 2 first packet = (336+(160+256)) = 752
layer 2 second packet = (336+(160+(160+224))) = 880
layer 1 first packet = 752 * (4/3) + 16 + 16 = 1003 + 32 = 1035
layer 1 second packet = 880 * (4/3) + 16 + 16 = 1174 + 32 = 1206
layer 1 first symbol = max(1035, 3120) = 3120
layer 1 second symbol = max(1206, 3120) = 3120
So in total, 6240 bits of data have to be sent.
For the second part, since our number is a measure of time rather than bits, I’ll take the Wi-Fi protocol’s transmission rate and multiply it by that time. In other words, what I’m measuring is: if we had sent meaningless bits during the time we spend waiting, how many bits would have gone out?
2.1. Before sending each packet in Wi-Fi 5, we have to wait for a DIFS to make sure the medium isn’t noisy. (34 microseconds)
2.2. After that, we go into the VHT Preamble, where we send a high-throughput data stream (Very High Throughput) to the receiver so it can sync its clock with ours. (40 microseconds)
2.3. Then it’s time to send the packet itself.
2.4. After that, we wait for a SIFS, and the receiver has to send the ack within that window; otherwise, we retransmit the packet. (16 microseconds)
So in total, we have to wait 90 microseconds per packet, which, at Wi-Fi 5’s transmission rate of 866.7 Mbps, is equivalent to:
886.7*1e6*90*1e-6 = 78,003 bit
So for every layer 2 packet, 78 thousand bits are going to waste at layer 1! Since we have 2 packets, that comes to 156006 bits! Add the 6240 bits of transmitted data, and it comes to 162246 bits!
So to transfer one byte of data (a request for “/” from a site), we move about 6240 bits of real data, and we waste the equivalent of 162246 bits of our resources. So the efficiency of the transmitted data comes to:
8/6240 = 0.001282051282
and the efficiency of the hardware and the available bandwidth comes to:
8/162246 = 0.00004930784118
So next time you feel like holding down F5 to reload a site 10 times, spare a thought for how disappointed the energy-saving PSA mascot would be in your wastefulness!