Course contentsShow
← Embedded Systems & Computer Architecture
Lesson 868 of 93612. IoT Connectivity and SecurityFree lesson

Optimizing TCP Window Sizes for Embedded Throughput

Tune TCP window sizes and segment parameters to maximize data throughput while respecting the limited memory buffers available for retransmission queues.

In this lesson, you will learn how to balance TCP receive window sizes to maximize data throughput without exhausting your embedded device’s limited SRAM.

The Balancing Act of TCP Windows

In embedded systems, network throughput is often capped by the amount of memory allocated to the TCP stack. The TCP Receive Window (RWIN) tells the sender how much data it can transmit before requiring an acknowledgment. If your RWIN is too small, the sender spends significant time idling, waiting for your device to process the packet and clear the buffer—this is known as "bandwidth-delay product" starvation.

Conversely, setting RWIN too large allows the sender to flood your system, potentially filling the retransmission queue beyond your device's physical memory capacity, leading to packet drops, buffer overflows, and kernel panics.

Calculating the Sweet Spot

To find the optimal window size, you must consider the round-trip time (RTT) and your available heap. If your device has a maximum memory overhead M dedicated to networking, and your average RTT is T, your maximum window size is:

Window_Size = (Bandwidth * RTT) / 8

However, in embedded memory-constrained environments, you must often cap this at the size of your statically allocated buffer pool. Using LwIP as an example, you would configure this via the TCP_WND macro:

/* Limit TCP window to 4KB to protect against buffer exhaustion 
   in low-memory IoT sensors */
#define TCP_WND (4 * 1024) 

/* Ensure MSS is aligned with the buffer size */
#define TCP_MSS 1460 

Tuning the Segment Size (MSS)

The Maximum Segment Size (MSS) also dictates efficiency. While a larger MSS reduces protocol overhead, it increases the risk of memory fragmentation because the TCP/IP stack must allocate a contiguous buffer for each incoming segment. By ensuring MSS is a sub-multiple of your page size or pre-allocated pool size, you prevent the fragmentation that eventually kills long-running IoT devices.

By tuning TCP_WND to reflect your actual memory budget and setting a conservative MSS, you ensure that the stack remains stable during bursts of high-frequency data.

Key Takeaway: Optimize your TCP window size to be just large enough to keep your communication pipe full, ensuring it never exceeds the total pre-allocated memory pool to prevent critical system instability.