title: What is the workaround for TCP delayed acknowledgment?
body: <p>I have shipped an online (grid-based) videogame that uses the TCP protocol to ensure reliable communication in a server-client network topology. My game works fairly well, but suffers from higher than expected latency (similar TCP games in the genre seem to do a better job at keeping latency to a minimal). </p>
  
  <p>While investigating, I discovered that the latency is only unexpectedly high for clients running <em>Microsoft Windows</em> (as opposed to <em>Mac OS X</em> clients). Furthermore, I discovered that if a Windows client sets <a href="http://lifeandcode.net/2009/05/reduce-game-network-latency-in-windows-7-or-vista/"><code>TcpAckFrequency=1</code> in the registry</a> and restarts their machine, their latency becomes normal.</p>
  
  <p>It would appear that my network design did not take into account delayed acknowledgement:</p>
  
  <blockquote>
    <p>A design that does not take into account the interaction of <strong>delayed acknowledgment</strong>, the Nagle algorithm, and Winsock buffering can drastically effect performance.
    (<a href="http://support.microsoft.com/kb/214397">http://support.microsoft.com/kb/214397</a>)</p>
  </blockquote>
  
  <p>However, I'm finding it nearly impossible to take into account delayed acknowledgement in my game (or any game). According to MSDN, the Microsoft TCP stack uses the following criteria to decide when to send one ACK on received data packets:</p>
  
  <blockquote>
    <ul>
    <li>If the second data packet is received before the delay timer expires (200ms), the ACK is sent.</li>
    <li>If there are data to be sent in the same direction as the ACK before the second data packet is received and the delay timer expires,
    the ACK is piggybacked with the data segment and sent immediately.</li>
    <li>When the delay timer expires (200ms), the ACK is sent.</li>
    </ul>
    
    <p>(<a href="http://support.microsoft.com/kb/214397">http://support.microsoft.com/kb/214397</a>)</p>
  </blockquote>
  
  <p>Reading this, one would presume that the workaround for delayed acknowledgement on Microsoft's TCP stack is as follows:</p>
  
  <ol>
  <li>Disable the Nagle algorithm (TCP_NODELAY).</li>
  <li>Disable the socket's send buffer (<code>SO_SNDBUF</code>=0), so that a call to <code>send</code> can be expected to send a packet.</li>
  <li>When calling <code>send</code>, if no further data is expected to be sent immediately, call <code>send</code> again with a single-byte of data that will be discarded by the receiver.</li>
  </ol>
  
  <p>With this approach, the second data packet will be received by the receiver at around the same time as the previous data packet. As a result, the <code>ACK</code> should get sent immediately from the receiver to the sender (emulating what <code>TcpAckFrequency=1</code> does in the registry).</p>
  
  <p>However, from my testing, this improved latency only by about a half of what the registry edit does. What am I missing?</p>
  
  <p><hr>
  <b>Q: Why not use UDP?</b></p>
  
  <p><b>A:</b> I chose TCP because every packet I send needs to arrive (and be in order); there are no packets that arn't worth retransmitting if they get lost (or become unordered). Only when packets can be discarded/unordered, can UDP be faster than TCP!</p>