Your Game Doesn't Need UDP (Yet)

22 thoughts
last posted April 2, 2014, 5:28 a.m.
0
get stream as: markdown or atom
0

The Internet is full of articles like this one which advise (implicitly novice) game programmers to use UDP as a first step in designing their game's network protocol.

0

There are empirical studies that prove that this is the right thing to do, even!

0

I'm here to tell you that this advice is wrong (the data on performance notwithstanding).

Fundamentally there's one reason: UDP is an optimization, so any discussion of it should be at the late stages of implementation, once you have something you can optimize.

But it's more complicated than that, of course, and there are a lot of related reasons.

0

"Use UDP" advice typically comes to exactly the wrong sort of programmer: one who doesn't know anything about networking specifically (and even a limited amount about concurrency and message-passing generally), and is learning. A reliable-delivery transport is hard enough to understand; adding nondeterminism into the mix makes the up-front burden of learning that much greater.

0

UDP is particularly tricky and nuanced to learn, partially because it's non-deterministic, but mostly because it's actually totally deterministic right up until the point where it isn't. In testing, on your LAN, on lo0, when there's not a lot going on in the game, delivery is totally reliable, so the code path for recovering from missed datagrams is never covered, the usability where users are dealing with lost traffic is never stressed.

Which means that you never see any flaws in your broken, slow retransmission logic, until it's in front of real users actually playing it on the public internet.

0

You also need to learn how to use TCP for a game, for several reasons:

  1. Not all networks pass UDP. It's better to offer a potentially degraded experience than to cut users off from your game entirely.
  2. UDP is appropriate for a few very small classes of data, for a few specific genres of game. Not all MMOs necessarily have an open world that you physically run around in. Position-update and health-update messages only care about the latest state (and therefore can go over UDP), but other updates are less time-sensitive and transmitting the full state as an update for complex game systems would be more expensive than just processing a sequence of changes, which is what TCP lets you do.
  3. If you re-invent TCP on top of UDP without a thorough prior understanding of TCP, you will get it wrong.
  4. If you re-invent TCP on top of UDP without a thorough prior understanding of TCP, you will get it wrong.
1

If you're a game programmer who wants to start adding some networking to a game, just use TCP for now.

Then, learn about:

  1. latency
  2. fragmentation
  3. flow control
  4. framing
  5. security ([D]TLS)
  6. monitoring
  7. graceful degradation
  8. statistical profiling
  9. human time perception
  10. physical limitations of networking hardware
  11. market penetration of different latency/bandwidth uplinks in all the markets you care about
  12. performance analysis

When you are done with all of that, then it's time to start investigating UDP.

0

You may find, after all of that, that UDP won't even help you in the ways that it is supposed to.

The general principle here is that "if you can't measure it, you can't manage it", but there are more specific issues too.

0

When everything is working perfectly and there is no packet loss (assuming Nagle is disabled), UDP and TCP will perform approximately equivalently; data gets through immediately, it's delivered to the application, there's no need to retransmit. And on most modern consumer networks, packet loss is very low. You may never even see the conditions where UDP should perform better.

0

Even when UDP should perform better, the public internet is increasingly dominated by specialized, highly tuned hardware that knows about the semantics of TCP, that lets TCP cheat. It won't understand your special UDP protocol, so you won't get any speed boost from fancy cheater hardware on the path between you and your players.

0

One of the features in TCP which can make TCP “slow” is that TCP is sometimes intentionally slow. For example, TCP uses a startup algorithm called “slow start” and other congestion-control techniques to avoid congestive collapse, i.e. your network blowing itself up and becoming useless. TCP uses rate-limiting algorithms to intentionally avoid swamping networks to make them useless, and if your game naively blasts out traffic as fast as possible, that's exactly what it will do if a lot of people play it.

0

Especially for the really novice game developers out there, it's important to note that UDP isn't “faster”. There's a very specific thing that UDP does differently than TCP, and it has to do with what happens to the underlying physical network.

0

Physical networks fail.

They get unplugged. Squirrels chew through the wires. Squirrels partially chew through the wires and water leaks onto them. Your router will be misconfigured. Your ISP's router will be misconfigured. Power will fail. Power will surge. Copper corrodes. Fiber decays.

It is because of the pervasive nature of this failure that TCP even exists.

0

You see, TCP makes a promise to you. The promise is that if you use a TCP connection between application X and application Y, and you send A, then, B, then C, A will get there before B gets there, and B will get there before C gets there.

Notice I didn't say that TCP promises that A will get there at all. Maybe a vindictive garden gnome cut your ethernet cable with pinking shears. Maybe your router caught fire. Who knows; sometimes the traffic doesn't get through. TCP will simply fail in those cases, but it will promise that B will not get through unless A does first. It might have to break up A into multiple pieces, so you might get A[0] A[1] A[2] B C, but you'll never get A C or C A B.

0

UDP, on the other hand, may deliver A then B then C (which is what will happen when you're testing your game on your LAN) or it may deliver C then A then A then C (which might happen if you're in a low-bandwidth internet cafe). That's right; it could duplicate your traffic, put it out of order, and drop it. It makes no promises except a weak offer that probably the contents of C will be the same on one end of the wire and the other (and that it won't break up C into pieces).

0

So what makes UDP “faster” is that if you are sending a sequence of updates, and you only care about the most recent one, then if your network is dropping packets you can catch up to real time a bit faster.

In order to do take advantage of this property, you don't just need to “use UDP”. You need to use it in a way where it will help.

0

First, you need to classify all of the information you're sending to each player according to whether losing updates is OK. Is it a rarely-updated statistic, like how much money a player has? Probably best to send it via TCP, since a missed update means they might have a wrong number on their screen for a long time. Is it a constantly-updated value like their <x,y,z> position? You're going to be sending a constant stream of these when they're moving, and it would be fine to send a periodic pulse when they're standing still, and only the latest value matters, so UDP might be OK.

0

Then you have to carefully build a protocol on top of UDP. You need to be careful to include all the relevant information that will need to be used to contextualize the information that's received in a datagram. At the very least this means a datagram number, so that you can determine which one was sent the most recently, since they might arrive out of order.

0

The thing is, once you've classified all your types of information and designed your messages, you can actually do a form of this optimization with TCP too.

0

The issue is what happens when there's packet loss. If you blindly blast out all of your player's movements with UDP, then when there is some packet loss, their movements will start to appear jerky, they'll pause periodically, and they'll take impossible paths, but, assuming their network recovers, they'll generally end up in the right place.

If you blindly send your players movements over TCP, then when there's packet loss, they'll pause, then rapidly catch up, but take an accurate path.

0

But your operating system's TCP stack provides feedback mechanisms to determine that there has been packet loss – or, for that matter, other types of network slowdown. While UDP is indeed optimal for real-time protocols if it's very carefully used, you can preserve many of the properties of TCP (for example, not needing to implement your own congestion control) without blindly sending all of your players’ movements across the network.

0

For example, let’s say that your game server sends out a position update every 20 milliseconds, and it does this all over an individual TCP connection to each player.

If one client starts to lag, the server can notice that those updates are piling up on that client’s send queue, and stop sending updates to that client. Then, as soon as the client’s buffer starts to be consumed, it can send whatever the most recent state is, which compensates for the most serious problem of TCP with respect to real time protocols, which is that stale updates clog the connection and leave a player experiencing some temporary packet loss far out of date.