339

Since TCP guarantees packet delivery and thus can be considered "reliable", whereas UDP doesn't guarantee anything and packets can be lost. What would be the advantage of transmitting data using UDP in an application rather than over a TCP stream? In what kind of situations would UDP be the better choice, and why?

I'm assuming that UDP is faster since it doesn't have the overhead of creating and maintaining a stream, but wouldn't that be irrelevant if some data never reaches its destination?

Deep LF
  • 199
  • 1
  • 12
Jeff L
  • 6,108
  • 3
  • 23
  • 29
  • 63
    As well as suffering from possible packet loss, UDP does not guarantee that you'll only receive the packet once. If you have convoluted or badly configured networks, you can receive the same packet multiple times. Just a heads up since people tend to forget this! – Brian Agnew Jul 08 '09 at 18:28
  • 4
    It doesn't even guarantee packet ordering. – Mehrdad Afshari Jul 08 '09 at 18:50
  • 22
    TCP does not guarantee **delivery**, it just guarantees that if it is able to deliver the packets they will be in the same order that they were sent. – Chaim Geretz Mar 10 '11 at 17:03
  • 8
    BTW, I frequently see people equate reliability/in-order delivery to TCP retransmits. Those "experts" will tell you that to overcome transmission errors on UDP, you will reimplement TCP (badly) and therefore you might as well use TCP. **This is not true.** There are other error-recovery techniques besides retransmit, that do not suffer latency or exponentially degraded throughput as a result of small-but-non-zero error rates. – Ben Voigt Oct 09 '14 at 14:13
  • TCP also guarantee's that you'll know if the destination did not receive the packets. – csga5000 Nov 04 '15 at 01:14
  • 1
    A very similar [question](http://networkengineering.stackexchange.com/questions/24312) was asked on the [Network Engineering Stack Exchange](http://networkengineering.stackexchange.com/), I am linking my answer to it here to provide additional insight: **[How to know whether a protocol uses UDP or TCP?](http://networkengineering.stackexchange.com/a/24313/3675)** – Eddie Jun 14 '16 at 16:54
  • @csga5000: With TCP, you'll know that certain packets are received, and you'll know that any packets you didn't send won't be received, but there will be some cases where it will be impossible to know what was (or will be) received. – supercat Jul 08 '17 at 22:20
  • @supercat You're correct, my wording was wrong. It tells you when data is received. But in the world of networking it's impossible to know if a package "will" be received that has not yet, and it's impossible guarantee that you can receive a callback when a package is received. But it does help you know what packages were sent successfully and which ones may not be received. – csga5000 Jul 27 '17 at 23:18

24 Answers24

394

This is one of my favorite questions. UDP is so misunderstood.

In situations where you really want to get a simple answer to another server quickly, UDP works best. In general, you want the answer to be in one response packet, and you are prepared to implement your own protocol for reliability or to resend. DNS is the perfect description of this use case. The costs of connection setups are way too high (yet, DNS does support a TCP mode as well).

Another case is when you are delivering data that can be lost because newer data coming in will replace that previous data/state. Weather data, video streaming, a stock quotation service (not used for actual trading), or gaming data comes to mind.

Another case is when you are managing a tremendous amount of state and you want to avoid using TCP because the OS cannot handle that many sessions. This is a rare case today. In fact, there are now user-land TCP stacks that can be used so that the application writer may have finer grained control over the resources needed for that TCP state. Prior to 2003, UDP was really the only game in town.

One other case is for multicast traffic. UDP can be multicasted to multiple hosts whereas TCP cannot do this at all.

Akash Vartak
  • 167
  • 1
  • 8
drudru
  • 4,933
  • 1
  • 19
  • 19
  • Thanks for the interesting answer. We have a server currently doing everything in UDP (high bandwidth requirement) which is Ok because there is a single hop really (routing disabled, ...), but have noticed that packet reordering could become an issue on some faulty network cards. What user-mode TCP (or some other user-mode flow controlled) stack do you suggest? – dashesy Mar 13 '13 at 14:28
  • @dashesy - can you get rid of the ordering requirement? Is there a monotonically increasing number inside the payload that you can use? If so, you don't really need a full blown user land TCP stack. – drudru Mar 18 '13 at 05:17
  • @drudru- yes the sequence number is there, I may need to buffer and de-jitter myself. Thanks, eliminating one more option is always great. – dashesy Mar 19 '13 at 01:18
73

If a TCP packet is lost, it will be resent. That is not handy for applications that rely on data being handled in a specific order in real time.

Examples include video streaming and especially VoIP (e.g. Skype). In those instances, however, a dropped packet is not such a big deal: our senses aren't perfect, so we may not even notice. That is why these types of applications use UDP instead of TCP.

Stephan202
  • 59,965
  • 13
  • 127
  • 133
  • 26
    I think you have it backwards. TCP re-orders packets so that data is delivered in the sent order. UDP does not re-order and delivers data in whatever order it received it in. – Hans Malherbe Jul 08 '09 at 19:09
  • 2
    UDP does not guarantee the order, you can however number the packets and reorder them after retreiving them. – Kugel Dec 28 '09 at 11:32
  • 9
    @Stephan202: I think I would have to disagree about not noticing the dropped packets in Skype ;-) – Robert S. Barnes Feb 17 '10 at 12:53
  • 6
    @Kugel: Just beware that you might be implementing a new TCP stack. You're unlikely to do a better job than the OS at this. – erikkallen Feb 17 '10 at 13:00
  • 1
    @erikkallen: If one were using UDP to implement a higher-level protocol with the same requirements TCP was designed to meet, one would be unlikely to do much better than the existing protocols. On the other hand, some applications benefit from the addition of a few features to the protocol which a UDP wrapper could handle better than TCP. – supercat Dec 26 '15 at 21:30
  • @erikkallen: With some kinds of application, it's helpful to have a callback invoked just before a chunk data is actually sent, which can indicate whether the data in question is still useful; in some cases (such as with updating the screen for a remotely-executed application) data might not have a fixed useful lifetime, but may nonetheless be rendered obsolete by events that occur between the time a packet is enqueued for delivery and when it is actually sent. Even if one will need reliable delivery for all packets that aren't superseded, being able to discard packets... – supercat Dec 26 '15 at 21:38
  • 1
    ...which have been rendered useless by future events can greatly improve performance. – supercat Dec 26 '15 at 21:39
63

The "unreliability" of UDP is a formalism. Transmission isn't absolutely guaranteed. As a practical matter, they almost always get through. They just aren't acknowledged and retried after a timeout.

The overhead in negotiating for a TCP socket and handshaking the TCP packets is huge. Really huge. There is no appreciable UDP overhead.

Most importantly, you can easily supplement UDP with some reliable delivery hand-shaking that's less overhead than TCP. Read this: http://en.wikipedia.org/wiki/Reliable_User_Datagram_Protocol

UDP is useful for broadcasting information in a publish-subscribe kind of application. IIRC, TIBCO makes heavy use of UDP for notification of state change.

Any other kind of one-way "significant event" or "logging" activity can be handled nicely with UDP packets. You want to send notification without constructing an entire socket. You don't expect any response from the various listeners.

System "heartbeat" or "I'm alive" messages are a good choice, also. Missing one isn't a crisis. Missing half a dozen (in a row) is.

Jarvis
  • 8,494
  • 3
  • 27
  • 58
S.Lott
  • 384,516
  • 81
  • 508
  • 779
  • 7
    "As a practical matter, they almost always get through". Highly depends on lower network layers reliability. – m0skit0 Apr 17 '16 at 10:25
  • besides, is there a difference between planning for "a few" packet loss and "too much" packet loss? loss is loss. you have to plan for it anyway. – Sedat Kapanoglu Sep 21 '16 at 21:24
32

I work on a product that supports both UDP (IP) and TCP/IP communication between client and server. It started out with IPX over 15 years ago with IP support added 13 years ago. We added TCP/IP support 3 or 4 years ago. Wild guess coming up: The UDP to TCP code ratio is probably about 80/20. The product is a database server, so reliability is critical. We have to handle all of the issues imposed by UDP (packet loss, packet doubling, packet order, etc.) already mentioned in other answers. There are rarely any problems, but they do sometimes occur and so must be handled. The benefit to supporting UDP is that we are able to customize it a bit to our own usage and tweak a bit more performance out of it.

Every network is going to be different, but the UDP communication protocol is generally a little bit faster for us. The skeptical reader will rightly question whether we implemented everything correctly. Plus, what can you expect from a guy with a 2 digit rep? Nonetheless, I just now ran a test out of curiosity. The test read 1 million records (select * from sometable). I set the number of records to return with each individual client request to be 1, 10, and then 100 (three test runs with each protocol). The server was only two hops away over a 100Mbit LAN. The numbers seemed to agree with what others have found in the past (UDP is about 5% faster in most situations). The total times in milliseconds were as follows for this particular test:

  1. 1 record
    • IP: 390,760 ms
    • TCP: 416,903 ms
  2. 10 records
    • IP: 91,707 ms
    • TCP: 95,662 ms
  3. 100 records
    • IP: 29,664 ms
    • TCP: 30,968 ms

The total data amount transmitted was about the same for both IP and TCP. We have extra overhead with the UDP communications because we have some of the same stuff that you get for "free" with TCP/IP (checksums, sequence numbers, etc.). For example, Wireshark showed that a request for the next set of records was 80 bytes with UDP and 84 bytes with TCP.

Mark Wilkins
  • 40,729
  • 5
  • 57
  • 110
  • What if you'd developed it for TCP only and buy better hardware instead of 5x more coding effort? – inf3rno Jun 02 '17 at 22:55
  • 2
    Thanks for the concrete numbers! 5% improvement is a bit disappointing for the complexity it adds. – Sergei Jul 03 '17 at 16:51
  • 1
    Probably the 5% is because is sent in a local network (two hopes away)? My guess is that the farther it is, the higher the difference is. – lepe Dec 29 '17 at 03:35
24

There are already many good answers here, but I would like to add one very important factor as well as a summary. UDP can achieve a much higher throughput with the correct tuning because it does not employ congestion control. Congestion control in TCP is very very important. It controls the rate and throughput of the connection in order to minimize network congestion by trying to estimate the current capacity of the connection. Even when packets are sent over very reliable links, such as in the core network, routers have limited size buffers. These buffers fill up to their capacity and packets are then dropped, and TCP notices this drop through the lack of a received acknowledgement, thereby throttling the speed of the connection to the estimation of the capacity. TCP also employs something called slow start, but the throughput (actually the congestion window) is slowly increased until packets are dropped, and is then lowered and slowly increased again until packets are dropped etc. This causes the TCP throughput to fluctuate. You can see this clearly when you download a large file.

Because UDP is not using congestion control it can be both faster and experience less delay because it will not seek to maximize the buffers up to the dropping point, i.e. UDP packets are spending less time in buffers and get there faster with less delay. Because UDP does not employ congestion control, but TCP does, it can take away capacity from TCP that yields to UDP flows.

UDP is still vulnerable to congestion and packet drops though, so your application has to be prepared to handle these complications somehow, likely using retransmission or error correcting codes.

The result is that UDP can:

  • Achieve higher throughput than TCP as long as the network drop rate is within limits that the application can handle.
  • Deliver packets faster than TCP with less delay.
  • Setup connections faster as there are no initial handshake to setup the connection
  • Transmit multicast packets, whereas TCP have to use multiple connections.
  • Transmit fixed size packets, whereas TCP transmit data in segments. If you transfer a UDP packet of 300 Bytes, you will receive 300 Bytes at the other end. With TCP, you may feed the sending socket 300 Bytes, but the receiver only reads 100 Bytes, and you have to figure out somehow that there are 200 more Bytes on the way. This is important if your application transmit fixed size messages, rather than a stream of bytes.

In summary, UDP can be used for every type of application that TCP can, as long as you also implement a proper retransmission mechanism. UDP can be very fast, has less delay, is not affected by congestion on a connection basis, transmits fixed sized datagrams, and can be used for multicasting.

nairware
  • 3,090
  • 9
  • 37
  • 58
Andy
  • 2,469
  • 1
  • 25
  • 25
  • 1
    When networks get sufficiently congested as to cause packet loss, TCP tries to minimize its impact on other users of the network, while many UDP-based implementations don't. This lets them grab a bigger share of a diminishing pie, but also reduces the total amount of *useful* bandwidth available period (e.g. as a consequence of unnecessary retransmission in cases where data would in fact be delivered but the sender wouldn't realize that) – supercat Jul 08 '17 at 22:43
  • First of all, thank you for the great answer, I really learned a lot from it! But I have a question: doesn't segmentation happens on the layer 3(IP) because of the Ethernet adapter limitations for all the packets received from layer 4(both TCP and UDP)? Do you mean any other kind of segmentation which happens in TCP but does not happen in UDP? I'd really appreciate if you could explain it to me. – ruslash Feb 03 '18 at 16:07
  • 1
    @Freezy. You are right, fragmentation of packets that exceed the MTU of the link (layer 2) happens at layer 3-IP. However, TCP is a stream based protocol and it treats data as a stream of bytes. TCP do send its data in segments in order to fit inside IP packets, which are sized according to the MSS, so segmentation do happen in TCP as well. How much data TCP puts in a segment, or much data your socket reads varies by many factors though; it can be 1 byte or MSS bytes. With UDP, the receiver always gets the exact number of bytes the transmitter sent, if the packet is not lost en route. – Andy Feb 03 '18 at 21:03
18

UDP is a connection-less protocol and is used in protocols like SNMP and DNS in which data packets arriving out of order is acceptable and immediate transmission of the data packet matters.

It is used in SNMP since network management must often be done when the network is in stress i.e. when reliable, congestion-controlled data transfer is difficult to achieve.

It is used in DNS since it does not involve connection establishment, thereby avoiding connection establishment delays.

cheers

nairware
  • 3,090
  • 9
  • 37
  • 58
Arnkrishn
  • 29,828
  • 40
  • 114
  • 128
15

UDP does have less overhead and is good for doing things like streaming real time data like audio or video, or in any case where it is ok if data is lost.

TWA
  • 12,756
  • 13
  • 56
  • 92
11

One of the best answer I know of for this question comes from user zAy0LfpBZLC8mAC at Hacker News. This answer is so good I'm just going to quote it as-is.

TCP has head-of-queue blocking, as it guarantees complete and in-order delivery, so when a packet gets lost in transit, it has to wait for a retransmit of the missing packet, whereas UDP delivers packets to the application as they arrive, including duplicates and without any guarantee that a packet arrives at all or which order they arrive (it really is essentially IP with port numbers and an (optional) payload checksum added), but that is fine for telephony, for example, where it usually simply doesn't matter when a few milliseconds of audio are missing, but delay is very annoying, so you don't bother with retransmits, you just drop any duplicates, sort reordered packets into the right order for a few hundred milliseconds of jitter buffer, and if packets don't show up in time or at all, they are simply skipped, possible interpolated where supported by the codec.

Also, a major part of TCP is flow control, to make sure you get as much througput as possible, but without overloading the network (which is kinda redundant, as an overloaded network will drop your packets, which means you'd have to do retransmits, which hurts throughput), UDP doesn't have any of that - which makes sense for applications like telephony, as telephony with a given codec needs a certain amount of bandwidth, you can not "slow it down", and additional bandwidth also doesn't make the call go faster.

In addition to realtime/low latency applications, UDP makes sense for really small transactions, such as DNS lookups, simply because it doesn't have the TCP connection establishment and teardown overhead, both in terms of latency and in terms of bandwidth use. If your request is smaller than a typical MTU and the repsonse probably is, too, you can be done in one roundtrip, with no need to keep any state at the server, and flow control als ordering and all that probably isn't particularly useful for such uses either.

And then, you can use UDP to build your own TCP replacements, of course, but it's probably not a good idea without some deep understanding of network dynamics, modern TCP algorithms are pretty sophisticated.

Also, I guess it should be mentioned that there is more than UDP and TCP, such as SCTP and DCCP. The only problem currently is that the (IPv4) internet is full of NAT gateways which make it impossible to use protocols other than UDP and TCP in end-user applications.

Shital Shah
  • 63,284
  • 17
  • 238
  • 185
10

Video streaming is a perfect example of using UDP.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
8

UDP has lower overhead, as stated already is good for streaming things like video and audio where it is better to just lose a packet then try to resend and catch up.

There are no guarantees on TCP delivery, you are simply supposed to be told if the socket disconnected or basically if the data is not going to arrive. Otherwise it gets there when it gets there.

A big thing that people forget is that udp is packet based, and tcp is bytestream based, there is no guarantee that the "tcp packet" you sent is the packet that shows up on the other end, it can be dissected into as many packets as the routers and stacks desire. So your software has the additional overhead of parsing bytes back into usable chunks of data, that can take a fair amount of overhead. UDP can be out of order so you have to number your packets or use some other mechanism to re-order them if you care to do so. But if you get that udp packet it arrives with all the same bytes in the same order as it left, no changes. So the term udp packet makes sense but tcp packet doesnt necessarily. TCP has its own re-try and ordering mechanism that is hidden from your application, you can re-invent that with UDP to tailor it to your needs.

UDP is far easier to write code for on both ends, basically because you do not have to make and maintain the point to point connections. My question is typically where are the situations where you would want the TCP overhead? And if you take shortcuts like assuming a tcp "packet" received is the complete packet that was sent, are you better off? (you are likely to throw away two packets if you bother to check the length/content)

old_timer
  • 69,149
  • 8
  • 89
  • 168
  • TCP does have a delivery guarantee: chunk A will be delivered to an application before chunk B, therefore if an application acknowledges (at a application level) chunk B you know it got chunk A. But this also happens at the TCP handling level also. – Simeon Pilgrim Jul 08 '09 at 22:24
  • In TCP, one can safely delimit chunks of data by simply prefixing each chunk with its length. Depending upon the application, one may prefix each chunk with a fixed one-byte, two-byte, or four-byte length, or one may prefix each chunk of size 128^N or less with an N-byte length. Not exactly a huge overhead. Such a design would be bad with protocols that don't guarantee in-order delivery without gaps, but when using TCP such a design is just fine. – supercat May 25 '13 at 04:06
  • if looking for fixed length amounts of data you dont even need the length you just count the bytes as they come in... – old_timer May 25 '13 at 13:54
  • 1
    @supercat. You are absolutely right. This also means you are adding complexity to your application; complexity which is actually needed in UDP. For this reason, TCP is better for transferring streams, such as files. But I do exactly what you do when I want reliability of chunked data, and perhaps extra security of TLS on top TCP. – Andy Dec 26 '15 at 21:23
5

Network communication for video games is almost always done over UDP.

Speed is of utmost importance and it doesn't really matter if updates are missed since each update contains the complete current state of what the player can see.

17 of 26
  • 27,121
  • 13
  • 66
  • 85
  • 5
    normal not the complete state at all, but a delta since the last acknowledgement, therefore the updates get progressively bigger. – Simeon Pilgrim Jul 08 '09 at 22:20
5

The key question was related to "what kind of situations would UDP be the better choice [over tcp]"

There are many great answers above but what is lacking is any formal, objective assessment of the impact of transport uncertainty upon TCP performance.

With the massive growth of mobile applications, and the "occasionally connected" or "occasionally disconnected" paradigms that go with them, there are certainly situations where the overhead of TCP's attempts to maintain a connection when connections are hard to come by leads to a strong case for UDP and its "message oriented" nature.

Now I don't have the math/research/numbers on this, but I have produced apps that have worked more reliably using and ACK/NAK and message numbering over UDP than could be achieved with TCP when connectivity was generally poor and poor old TCP just spent it's time and my client's money just trying to connect. You get this in regional and rural areas of many western countries....

3

In some cases, which others have highlighted, guaranteed arrival of packets isn't important, and hence using UDP is fine. There are other cases where UDP is preferable to TCP.

One unique case where you would want to use UDP instead of TCP is where you are tunneling TCP over another protocol (e.g. tunnels, virtual networks, etc.). If you tunnel TCP over TCP, the congestion controls of each will interfere with each other. Hence one generally prefers to tunnel TCP over UDP (or some other stateless protocol). See TechRepublic article: Understanding TCP Over TCP: Effects of TCP Tunneling on End-to-End Throughput and Latency.

Brian M. Hunt
  • 81,008
  • 74
  • 230
  • 343
2

We know that the UDP is a connection-less protocol, so it is

  1. suitable for process that require simple request-response communication.
  2. suitable for process which has internal flow ,error control
  3. suitable for broad casting and multicasting

Specific examples:

  • used in SNMP
  • used for some route updating protocols such as RIP
ronalchn
  • 12,225
  • 10
  • 51
  • 61
vikki
  • 21
  • 1
2

UDP can be used when an app cares more about "real-time" data instead of exact data replication. For example, VOIP can use UDP and the app will worry about re-ordering packets, but in the end VOIP doesn't need every single packet, but more importantly needs a continuous flow of many of them. Maybe you here a "glitch" in the voice quality, but the main purpose is that you get the message and not that it is recreated perfectly on the other side. UDP is also used in situations where the expense of creating a connection and syncing with TCP outweighs the payload. DNS queries are a perfect example. One packet out, one packet back, per query. If using TCP this would be much more intensive. If you dont' get the DNS response back, you just retry.

RC.
  • 27,409
  • 9
  • 73
  • 93
2

UDP when speed is necessary and the accuracy if the packets is not, and TCP when you need accuracy.

UDP is often harder in that you must write your program in such a way that it is not dependent on the accuracy of the packets.

bgw
  • 2,036
  • 1
  • 19
  • 28
2

It's not always clear cut. However, if you need guaranteed delivery of packets with no loss and in the right sequence then TCP is probably what you want.

On the other hand UDP is appropriate for transmitting short packets of information where the sequence of the information is less important or where the data can fit into a single packet.

It's also appropriate when you want to broadcast the same information to many users.

Other times, it's appropriate when you are sending sequenced data but if some of it goes missing you're not too concerned (e.g. a VOIP application).

Some protocols are more complex because what's needed are some (but not all) of the features of TCP, but more than what UDP provides. That's where the application layer has to implement the additional functionality. In those cases, UDP is also appropriate (e.g. Internet radio, order is important but not every packet needs to get through).

Examples of where it is/could be used 1) A time server broadcasting the correct time to a bunch of machines on a LAN. 2) VOIP protocols 3) DNS lookups 4) Requesting LAN services e.g. where are you? 5) Internet radio 6) and many others...

On unix you can type grep udp /etc/services to get a list of UDP protocols implemented today... there are hundreds.

hookenz
  • 36,432
  • 45
  • 177
  • 286
2

Look at section 22.4 of Steven's Unix Network Programming, "When to Use UDP Instead of TCP".

Also, see this other SO answer about the misconception that UDP is always faster than TCP.

What Steven's says can be summed up as follows:

  • Use UDP for broadcast and multicast since that is your only option ( use multicast for any new apps )
  • You can use UDP for simple request / reply apps, but you'll need to build in your own acks, timeouts and retransmissions
  • Don't use UDP for bulk data transfer.
Community
  • 1
  • 1
Robert S. Barnes
  • 39,711
  • 30
  • 131
  • 179
  • 1
    A bit more information on that last point, for anyone that comes along. TCP works for bulk data transfer, but if you don't care that your data arrives in start-to-end order, you can write a protocol over UDP that might be faster - a lot faster in very specific pathological cases. It's not that you can't do bulk transfer in UDP, it's not that it's always a worse performer; it's just such a pain in the arse to implement that it's rarely worth the bother. – ijw Dec 09 '10 at 16:06
  • 1
    Yes, you can use UDP for bulk transfer, and you have to implement your own control mechanism. If its a pain in the arse or not depends on your programming skills, but it's definitely not always a worse performer. You have to know what you are doing; if you don't then yes, you may suffer. – Andy Dec 26 '15 at 21:16
2

Comparing TCP with UDP, connection-less protocols like UDP assure speed, but not reliability of packet transmission. For example in video games typically don't need a reliable network but the speed is the most important and using UDP for games has the advantage of reducing network delay.

enter image description here

Jorgesys
  • 124,308
  • 23
  • 334
  • 268
1

You want to use UDP over TCP in the cases where losing some of the data along the way will not completely ruin the data being transmitted. A lot of its uses are in real-time applications, such as gaming (i.e., FPS, where you don't always have to know where every player is at any given time, and if you lose a few packets along the way, new data will correctly tell you where the players are anyway), and real-time video streaming (one corrupt frame isn't going to ruin the viewing experience).

Zachary Murray
  • 1,220
  • 9
  • 16
  • 1
    Well one corrupt frame is going to ruin that part of the viewing, but you don't want it to stall all the latter frames, while waiting for it, if the later frames have more value than the lost frame. – Simeon Pilgrim Jul 08 '09 at 22:22
  • @SimeonPilgrim's answer contains the key insight! WHEN a packet is lost, which would you prefer? RETRANSMIT or FORGET about it and get a newer packet. That is why keeping track of positions in a real-time game can be handled well by UDP: once a packet is lost, we would prefer to know the NEXT position rather than pontificate about the exact history of how we got there. – ProfDFrancis Jan 21 '23 at 15:58
1

We have web service that has thousands of winforms client in as many PCs. The PCs have no connection with DB backend, all access is via the web service. So we decided to develop a central logging server that listens on a UDP port and all the clients sends an xml error log packet (using log4net UDP appender) that gets dumped to a DB table upon received. Since we don't really care if a few error logs are missed and with thousands of client it is fast with a dedicated logging service not loading the main web service.

softveda
  • 10,858
  • 6
  • 42
  • 50
0

I'm a bit reluctant to suggest UDP when TCP could possibly work. The problem is that if TCP isn't working for some reason, because the connection is too laggy or congested, changing the application to use UDP is unlikely to help. A bad connection is bad for UDP too. TCP already does a very good job of minimizing congestion.

The only case I can think of where UDP is required is for broadcast protocols. In cases where an application involves two, known hosts, UDP will likely only offer marginal performance benefits for substantially increased costs of code complexity.

SingleNegationElimination
  • 151,563
  • 33
  • 264
  • 304
  • One application where you'll get better results from UDP is in through put test, if an intermediate node is performing traffic policing, as you can control the packet rate easier, verse TCP which will push the packets fast, and the interaction of the TCP windowing negatively interacts with the policing. – Simeon Pilgrim Jul 08 '09 at 22:28
  • This is still an optimization, which should follow from actual testing. My argument is that you should still try to use TCP first, and only try alternatives when you discover that TCP isn't working for some reason. Choosing UDP because it theoretically supports better bandwidth use is a form of premature optimization. – SingleNegationElimination Jul 09 '09 at 02:08
  • Oh agree on the optimisation front. But knowledge of when TCP might be the problem verse something else helps when your trying to solve that performance problem. – Simeon Pilgrim Jul 09 '09 at 21:25
-1

Only use UDP if you really know what you are doing. UDP is in extremely rare cases today, but the number of (even very experienced) experts who would try to stick it everywhere seems to be out of proportion. Perhaps they enjoy implementing error-handling and connection maintenance code themselves.

TCP should be expected to be much faster with modern network interface cards due to what's known as checksum imprint. Surprisingly, at fast connection speeds (such as 1Gbps) computing a checksum would be a big load for a CPU so it is offloaded to NIC hardware that recognizes TCP packets for imprint, and it won't offer you the same service.

Pavel Radzivilovsky
  • 18,794
  • 5
  • 57
  • 67
-2

UDP is perfect for VoIP addressed where data packet has to be sent regard less its reliability... Video chatting is an example of UDP (you can check it by wireshark network capture during any video chatting).. Also TCP doesn't work with DNS and SNMP protocols. UDP does not have any overhead while TCP have lots of Overhead