# Tweaking TCP for Real-time Applications: Nagle's Algorithm and Delayed Acknowledgment

DevFeed: [Tweaking TCP for Real-time Applications: Nagle's Algorithm and Delayed Acknowledgment](<https://devfeed.tech/articles/tweaking-tcp-for-real-time-applications-nagle-s-algorithm-and-delayed-acknowledgment-24975.md>)

Original publisher: [Read original article](<https://codeahoy.com/2017/03/19/tweaking-tcp-for-real-time-applications-nagle-algorithm-and-delayed-acknowledgment/>)

Author: umer

Published: 2017-03-19T00:00:00Z

Content type: tutorial

Language: en

Sources: [Code Ahoy - Articles](<https://devfeed.tech/sources/code-ahoy-articles.md>)

Topics: [TCP/IP](<https://devfeed.tech/topics/tcp-ip.md>), [real-time](<https://devfeed.tech/topics/real-time.md>), [Protocol (disambiguation)](<https://devfeed.tech/topics/protocol.md>), [Algorithm](<https://devfeed.tech/topics/algorithm.md>)

Tags: [algorithm](<https://devfeed.tech/tags/algorithm.md>), [data-transmission](<https://devfeed.tech/tags/data-transmission.md>), [protocol](<https://devfeed.tech/tags/protocol.md>), [real-time](<https://devfeed.tech/tags/real-time.md>), [tcp-ip](<https://devfeed.tech/tags/tcp-ip.md>)

## AI overview

This tutorial explains how TCP buffering techniques affect real-time applications that send small messages. It focuses on Nagle's algorithm, which combines small messages to reduce packet overhead, and introduces TCP delayed acknowledgment. The article notes that Nagle's algorithm can create performance issues for multiplayer gaming servers.

## Source excerpt

TCP is a complex protocol. Don't get me wrong. It is a marvelous piece of engineering that gives us the reliable data transmission guarantee that other protocols don't provide. Reliable data transmission between two devices on the internet is no walk in the park and TCP uses a lot of magic under the hood to make things happen. Generally, it does a fine job of abstracting away low level details and its default settings work fine for most general purpose use cases. However, once in a while, things don't go according to plan and we need to pop open the hood and do some tweaking. It is in these situations, that some knowledge of TCP comes in very handy. By default, TCP uses two buffering techniques to optimize and minimize overhead for general purpose applications. However, if you are building applications that require real-time message delivery for small messages (e.g. chat or control messages), you must have some knowledge of these techniques. Nagle's algorithm TCP delayed acknowledgment Let's look at them in more detail. Nagle's Algorithm If there's no congestion, TCP tacks on a header and sends data out as soon as it gets it from the application. If the application is generating a lot of small messages, the headers can add a lot of overhead: TCP/IP headers are 40-byte, so 1-byte of data is sent as 41-byte packet on the network. A computer programmer named John Nagle came up with an algorithm to reduce the overhead by combining many small messages into a single message. Nagle's algorithm, named after its inventor, is a technique to make TCP more efficient by reducing the number of packets that are sent over the network. Here's the pseudo code for the algorithm: if there is new data to send if the window size >= MaximumSegmentSize and available data is >= MaximumSegmentSize send complete MaximumSegmentSize segment now else if there is unconfirmed data still in the pipe enqueue data in the buffer until an acknowledge is received else send data immediately end if end if