Should I disable the Nagle algorithm??

Started by
10 comments, last by hplus0603 13 years, 4 months ago
hi umm sorry for asking such a question, but should it be disabled? do you know any MMO that didnt turn it off? anyway, I was writing a demo using plain TCP and sometimes I got a low ping time (around 20~40ms) while it's said that nagle delay send for 200ms. how come?

best regards,
Bow Vernon
Advertisement
What are you transferring?

If you are sending only one or two bytes every few frames, then you should probably consider disabling it.

As an example, if you are writing a real-time chat client where you want each individual keystroke to be transmitted immediately, that communication will be slightly delayed to avoid sending 40+ bytes of headers for your single byte of data. The system is protecting you from a horrible 1:40 signal-to-noise ratio, but in that rare situation it is the right decision.

Such low signal rates with an urgent transmission requirement is a fairly rare thing in games.

Most games end up sending a significant amount of traffic, enough that it will not be throttled or significantly delayed for accumulation.
When making a real-time game where you just want to treat the TCP stream more like it was a packet system just turn off nagling. Just build your packets up before you send them. Don't spam tons of small messages to your players.

Nagle's algorithm is mostly to allow small messages to be merged quickly. For instance someone might just send data whenever something happens in a piece of software. In order to stop tons of tiny messages from getting sent nagling quickly merges them before sending the streams underlying packet.
I was sending position and orientation for about 30 times a sec. the ping is 20~40 ms if the traffic is good and 2.5 when the traffic is bad (when opening up facebook.com took more than 8 secs). I think TCP has been much optimized lately, but I cant confirm that.
Quote:Original post by Bow_vernon
I was sending position and orientation for about 30 times a sec. the ping is 20~40 ms if the traffic is good and 2.5 when the traffic is bad (when opening up facebook.com took more than 8 secs). I think TCP has been much optimized lately, but I cant confirm that.

30 times per second is like extreme twitch FPS. Did you calculate how much data you're sending? Remember MTU is around 1400 bytes so if you send that much TCP isn't going to wait around queuing up more messages for a socket.

Though position and orientation is only 24 bytes per player at the worst per player. So lets say hypothetically 10 players are connected and you had a "nagle algorithm" being used. For one client you'd be sending roughly 24 * 10 bytes. So with that you could assume after 5 updates the nagle algorithm would decide to send the "packet" to the client. At 30 times a second those 5 packets represent 166 ms delay from the first packet. Remember that the client would be getting the most recent packet though fairly quickly. Possibly the previous ones are thrown away since they're older?

I recommend tracking on your server (assuming linux use high precision timers) the exact time a client update is received. Then on the client track which updates are read in at what time. You might be seeing something on the client like:

Packet 1: 100 ms
Packet 2: 100 ms
Packet 3: 100 ms
...
Packet 6: 133 ms
Packet 7: 133 ms
...
That is your updates are being nagled without you realizing it. You have to realize 30 updates a second is rather high for most games. Your game could probably play the same with 15 packets a second so you're not seeing the effect of the unnecessary packets.

Though I'm just guessing. I never use nagling with real-time games because of the possible delay.
Wow okay, I was just testing. now I lowered the send to 10per secs. Well so it's better to turn it off? and when do I call it? should I call for the listening socket? I never saw a proper example of how to do that. but in most forum they recommended to turn it off. Dunno why.
Quote:Original post by Bow_vernon
Wow okay, I was just testing. now I lowered the send to 10per secs. Well so it's better to turn it off? and when do I call it? should I call for the listening socket? I never saw a proper example of how to do that. but in most forum they recommended to turn it off. Dunno why.


It really depends on your game, if you have a low update rate and small packets you want to turn it off since it will wait the full duration very frequently (packets will be sent once enough time has passed or enough data is queued up, whichever happens first), the more data you send the less impact nagle will have on latency.

In your case however where you're only sending position and orientation updates you'd be far better off switching to UDP. (resending dropped packets are pointless if you're only interested in the most recent packet anyway)
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
umm no thx Ill stick to TCP(not sure if I can make a better one with UDP, plus it has a good congestion control). anyway I was just testing. it wont be a twitchy games, just a simple point n click rpg(ragnarok style). I will let nagle on for now and then switch it off when I think necessary...
Quote:Original post by Bow_vernon
umm no thx Ill stick to TCP(not sure if I can make a better one with UDP, plus it has a good congestion control). anyway I was just testing. it wont be a twitchy games, just a simple point n click rpg(ragnarok style). I will let nagle on for now and then switch it off when I think necessary...


IIRC Ragnarok sends updates once a second.
well konfusius, u confuse me :p how do you know that?

This topic is closed to new replies.

Advertisement