Skip to main content

Framing Efficiency, MTU Trade-offs & Bit/Byte Stuffing

Medium

Interview Question: "A network transmits frames with a 1500-byte payload, 40-byte header, and 4-byte trailer. Calculate the framing efficiency, compare with a 100-byte payload, explain why protocols do not use infinitely large frames, derive the optimal MTU formula, and walk through bit and byte stuffing."


Framing is the primary responsibility of the Data Link Layer (Layer 2). It transforms raw physical bitstreams into discrete, manageable units with delimiters, addressing, and error detection.

Engineers must balance two opposing forces when sizing frames: maximizing payload efficiency against minimizing retransmission penalties, buffer pressure, and serialization delay.


1. Step-by-Step Framing Efficiency Calculations​

Framing efficiency is the ratio of useful user data (payload) to the total physical bits transmitted across the link:

Efficiency (η)=Payload SizeTotal Frame Size×100%=LL+H×100%\text{Efficiency } (\eta) = \frac{\text{Payload Size}}{\text{Total Frame Size}} \times 100\% = \frac{L}{L + H} \times 100\%

Case A: Standard 1500-Byte Payload​

  • Payload (LL): 1500 bytes1500\text{ bytes}
  • Fixed Overhead (HH): 40 bytes (header)+4 bytes (trailer)=44 bytes40\text{ bytes (header)} + 4\text{ bytes (trailer)} = 44\text{ bytes}
  • Total Frame Size (FF): 1500+44=1544 bytes1500 + 44 = \mathbf{1544\text{ bytes}}

η1500=15001544≈0.9715  ⟹  97.15%\eta_{1500} = \frac{1500}{1544} \approx 0.9715 \implies \mathbf{97.15\%} (Overhead accounts for only 2.85%2.85\% of transmitted data).

Case B: Small 100-Byte Payload​

  • Payload (LL): 100 bytes100\text{ bytes}
  • Fixed Overhead (HH): 44 bytes44\text{ bytes}
  • Total Frame Size (FF): 100+44=144 bytes100 + 44 = \mathbf{144\text{ bytes}}

η100=100144≈0.6944  ⟹  69.44%\eta_{100} = \frac{100}{144} \approx 0.6944 \implies \mathbf{69.44\%}

Real-World Impact:​

For small payloads (such as SSH keystrokes, DNS queries, TCP Keep-Alives, or TCP pure ACKs where L=0L = 0 bytes), the fixed 44-byte tax consumes over 30%30\% of total link bandwidth. In the extreme case of a 40-byte TCP pure ACK with no payload, link efficiency is 0%!


2. Why Not Infinitely Large Frames? The Three Physical Walls​

If framing efficiency asymptotically approaches 100%100\% as payload size increases (lim⁡L→∞LL+H=1\lim_{L \to \infty} \frac{L}{L+H} = 1), why does standard Ethernet limit MTU to 1500 bytes?

  1. Bit Error Rates (BER) & Retransmission Penalty: Physical links have a non-zero probability of bit corruption (pp). A single inverted bit corrupts the entire frame's CRC, causing the receiver to discard the frame.
    • In a 1500-byte frame, a bit flip costs 1500 bytes of retransmission.
    • In a 10 MB frame, a bit flip discards all 10 megabytes, creating throughput collapse on noisy links.
  2. Head-of-Line Blocking & Serialization Delay: The serialization delay of a frame is Tt=L/RT_t = L / R. On a 10 Mbps link: Tserialize=10×106 bytes×8 bits10×106 bps=8.0 secondsT_{\text{serialize}} = \frac{10 \times 10^6\text{ bytes} \times 8\text{ bits}}{10 \times 10^6\text{ bps}} = \mathbf{8.0\text{ seconds}} A single giant frame holds the wire hostage for 8 seconds, forcing latency-critical VoIP, video, and DNS packets behind it in the queue to experience massive jitter and buffer bloat.
  3. Hardware Memory & DMA Buffer Pressure: Network Interface Cards (NICs) and switches allocate fixed-size ring buffers in silicon DMA memory. Extremely large frames exhaust switch packet memory, leading to buffer overflow and indiscriminate packet drops.

3. Mathematical Derivation of Optimal Frame Size (L∗L^*)​

Let:

  • HH = Fixed frame overhead in bytes
  • LL = Payload size in bytes
  • pp = Channel Bit Error Rate (BER)
  • Total bits per frame = 8(L+H)8(L + H)

The probability that an entire frame is transmitted with zero errors is: Psuccess=(1−p)8(L+H)≈e−8p(L+H)P_{\text{success}} = (1 - p)^{8(L + H)} \approx e^{-8p(L + H)}

The effective Goodput (useful user throughput) is: S(L)=(LL+H)⋅Psuccess=(LL+H)e−8p(L+H)S(L) = \left(\frac{L}{L + H}\right) \cdot P_{\text{success}} = \left(\frac{L}{L + H}\right) e^{-8p(L + H)}

To find the optimal payload L∗L^* that maximizes Goodput, take the derivative dS(L)dL=0\frac{dS(L)}{dL} = 0: ddL[LL+He−8p(L+H)]=0\frac{d}{dL} \left[ \frac{L}{L + H} e^{-8p(L + H)} \right] = 0 H(L+H)2e−8p(L+H)−LL+H⋅8p⋅e−8p(L+H)=0\frac{H}{(L + H)^2} e^{-8p(L + H)} - \frac{L}{L + H} \cdot 8p \cdot e^{-8p(L + H)} = 0 HL+H−8p⋅L=0  ⟹  8pL2+8pHL−H=0\frac{H}{L + H} - 8p \cdot L = 0 \implies 8p L^2 + 8pH L - H = 0

Since 8pH≪18pH \ll 1 in practical networks, 8pL2≈H8p L^2 \approx H: L∗≈H8pL^* \approx \sqrt{\frac{H}{8p}}

Real-World Numerical Check:​

For fixed overhead H=44 bytesH = 44\text{ bytes} and a typical link BER p=10−6p = 10^{-6} (11 bad bit in 10610^6 bits): L∗≈448×10−6=5.5×106≈2,345 bytesL^* \approx \sqrt{\frac{44}{8 \times 10^{-6}}} = \sqrt{5.5 \times 10^6} \approx \mathbf{2,345\text{ bytes}}

This mathematical optimum explains why Ethernet standardized on 1,500 bytes: it provides an ideal equilibrium near the apex of the goodput curve across copper and fiber media.


4. Framing Mechanisms: Bit Stuffing vs Byte Stuffing​

To determine where a frame begins and ends, protocols must demarcate boundaries without misinterpreting arbitrary user data as control flags.

1. Bit Stuffing (HDLC / SDLC Protocols):​

  • Delimiter Flag: 01111110 (0x7E).
  • Sender Rule (Zero-Bit Insertion): Whenever the transmitter detects five consecutive 1s (11111) in the data stream, it unconditionally injects a 0 bit immediately after the 5th 1.
  • Receiver Rule: Whenever the receiver reads five consecutive 1s:
    • If the 6th bit is 0: Strip the stuffed 0 and restore the data.
    • If the 6th bit is 1 and 7th is 0 (01111110): This is the legitimate Flag Delimiter.
    • If the 6th bit is 1 and 7th is 1 (01111111): Channel error or Abort Signal.
Original Data: 0 1 1 1 1 1 1 0 (Contains identical flag pattern!)
Transmitter Stuffs: 0 1 1 1 1 1 0 1 0 (Zero inserted after five 1s)
Receiver Strips: 0 1 1 1 1 1 1 0 (Original pattern restored!)

2. Byte Stuffing (Character-Oriented Framing):​

  • Uses special ASCII control characters: STX (Start of TeXt), ETX (End of TeXt), and DLE (Data Link Escape).
  • Sender Rule: If the binary payload contains the DLE character, the sender prepends an extra escape byte: DLE DLE.
  • Receiver Rule: When receiving DLE DLE, the receiver strips the first DLE and keeps the second as literal payload.

5. Data Center Optimization: Jumbo Frames & PMTUD​

FeatureStandard EthernetData Center Jumbo Frames
Payload MTU1500 bytes9000 bytes
Total Frame Size1544 bytes9044 bytes
Framing Efficiency97.15%99.51%
Interrupt Frequency for 10 Gbps~812,000 interrupts/sec~138,000 interrupts/sec (83% reduction!)
Common DeploymentWAN, Internet, General LANSAN storage (iSCSI, NFS), AI/ML GPU clusters

Path MTU Discovery (PMTUD) & Black Hole Routers:​

When a host transmits packets with the IP DF (Don't Fragment) bit set:

  • If a packet exceeds an intermediate router's MTU, the router drops the packet and returns an ICMP Type 3, Code 4 message ("Destination Unreachable: Fragmentation Needed and DF set"), specifying the next-hop MTU.
  • The PMTUD Black Hole Trap: Misconfigured firewalls that indiscriminately block all ICMP traffic discard these notifications. The sending host never receives the error, continues retransmitting oversized packets, and the TCP connection permanently freezes.

6. Runnable Python Implementation​

This script demonstrates bit stuffing and unstuffing, calculates framing efficiency, and optimizes payload size based on bit error rates.

"""
Framing & Optimization Simulator
Demonstrates Bit Stuffing/Unstuffing, Framing Efficiency,
and Analytical Goodput Optimization over Noisy Links.
"""
import math


def bit_stuff(data_bits: str) -> str:
"""Inserts a '0' after five consecutive '1's."""
stuffed = []
consecutive_ones = 0
for bit in data_bits:
stuffed.append(bit)
if bit == "1":
consecutive_ones += 1
if consecutive_ones == 5:
stuffed.append("0") # Stuff zero
consecutive_ones = 0
else:
consecutive_ones = 0
return "".join(stuffed)


def bit_unstuff(stuffed_bits: str) -> str:
"""Removes the stuffed '0' after five consecutive '1's."""
unstuffed = []
consecutive_ones = 0
i = 0
while i < len(stuffed_bits):
bit = stuffed_bits[i]
unstuffed.append(bit)
if bit == "1":
consecutive_ones += 1
if consecutive_ones == 5:
# Next bit must be the stuffed 0 (skip it)
if i + 1 < len(stuffed_bits) and stuffed_bits[i + 1] == "0":
i += 1 # Skip stuffed zero
consecutive_ones = 0
else:
consecutive_ones = 0
i += 1
return "".join(unstuffed)


def calculate_efficiency(payload_bytes: int, overhead_bytes: int) -> float:
"""Calculates framing efficiency percentage."""
return (payload_bytes / (payload_bytes + overhead_bytes)) * 100.0


def optimal_payload_size(overhead_bytes: int, ber: float) -> int:
"""Derives optimal payload size L* = sqrt(H / (8 * BER))."""
return int(math.sqrt(overhead_bytes / (8.0 * ber)))


if __name__ == "__main__":
# 1. Framing Efficiency Comparison
overhead = 44
eff_1500 = calculate_efficiency(1500, overhead)
eff_100 = calculate_efficiency(100, overhead)
print(f"Efficiency (1500-byte Payload): {eff_1500:.2f}%")
print(f"Efficiency (100-byte Payload): {eff_100:.2f}%")
assert round(eff_1500, 2) == 97.15
assert round(eff_100, 2) == 69.44

# 2. Bit Stuffing Demonstration
test_stream = "0111111001111101111110"
stuffed = bit_stuff(test_stream)
restored = bit_unstuff(stuffed)
print(f"\nOriginal Bitstream: {test_stream}")
print(f"Stuffed Bitstream: {stuffed}")
print(f"Restored Bitstream: {restored}")
assert test_stream == restored
assert "111111" not in stuffed # Six 1s can never occur in stuffed data!

# 3. Optimal Frame Sizing vs Bit Error Rate
print("\n--- Analytical Optimal Frame Size ---")
for ber in [1e-4, 1e-5, 1e-6, 1e-7]:
l_opt = optimal_payload_size(overhead, ber)
print(f"BER = {ber:.0e} -> Optimal Payload: {l_opt:,} bytes")

7. Concise Staff-Level Interview Answer​

"Framing efficiency is the ratio of useful payload to total frame size. For a 1,500-byte payload with 44 bytes of overhead (40-byte header, 4-byte trailer), the total size is 1,544 bytes, yielding an efficiency of 15001544≈97.15%\frac{1500}{1544} \approx 97.15\%. When the payload shrinks to 100 bytes, efficiency drops to 100144≈69.44%\frac{100}{144} \approx 69.44\%, illustrating how fixed protocol overhead penalizes small, chatty packets.

Protocols do not make frames infinitely large due to three physical constraints: retransmission cost under channel bit errors, serialization latency causing head-of-line blocking for latency-sensitive traffic, and switch DMA buffer limits. Mathematically, by maximizing goodput S(L)=LL+H(1−p)8(L+H)S(L) = \frac{L}{L+H}(1-p)^{8(L+H)}, we derive the optimal payload size L∗≈H8pL^* \approx \sqrt{\frac{H}{8p}}. For a typical BER of 10−610^{-6} and 44 bytes of overhead, L∗≈2,345 bytesL^* \approx 2,345\text{ bytes}, explaining why Ethernet's 1,500-byte MTU represents the sweet spot between high efficiency and low retransmission loss.

At Layer 2, protocols prevent data from mimicking control flags using bit stuffing (in HDLC, inserting a 0 after five consecutive 1s) or byte stuffing (escaping control bytes with DLE). In modern data centers, networks use Jumbo Frames (9000 bytes) to boost efficiency to 99.5% and cut CPU interrupt handling overhead by over 80%."