Index ¦ Archives  ¦ Atom  ¦ RSS

SSH on Spotty WiFi

If you've ever been a place where the WiFi isn't great and tried to SSH into a computer, you've likely noticed the high latency (lag) between keypresses and results. This is because each packet takes a while to reach the target and come back, usually being sent multiple times by your computer to the router (this is how WiFi attempts to not lose packets that got drowned out by noise). This is likely because you're far away from the router or there are up to hundreds of people talking to the same router as you; I've seen this on university campuses, corporate networks, shuttles, and public WiFi hotspots.

I recently got fed up and looked up the cause and possible remedies. One remedy I've found works is to disable TCP_NODELAY (explained below) only for these connections, which enables Nagle's algorithm and makes your connection quite a bit faster.

The reason Nagle's algorithm is useful here

Poor Nagle implemented a great algorithm in the TCP stack that tries to combine multiple small packets together, combining more with higher latencies. On a spotty WiFi, the router is inundated with everyone's various packets and resubmitted packets (because it didn't WiFi-ACK fast enough); so when you're on SSH and sending a tiny packet (40-50 bytes for 1-10 bytes of real data) every hundred milliseconds or so, the router doesn't like this and becomes even slower and latency increases. However, if you use Nagle's algorithm by disabling TCP_NODELAY, you can decrease the perceived latency even though you're actually slowing your packets individually.

If you look closely with TCP_NODELAY on and off, you'll see that when it's disabled (Nagle's is enabled) a bunch of characters show up at a time when keyboard-spamming; this is the effect of combining the various packets. If you hit the keyboard when Nagle's is off (the default mode), you'll see each key show up separately, sometimes with huge delay between each key. This is because each packet is being resent between your computer and the router, and then again between the router and the server until the appropriate ACKs are given.

Enabling in PuTTY

This is done by unchecking the "Disable Nagle's algorithm (TCP_NODELAY option)" under Connection in a session's settings. I save these as shuttle-* versions of my normal sessions so I don't have to keep doing it.

© Fahrzin Hemmati. Built using Pelican. Theme by Giulio Fidente on github.