Iowa15
Iowa15

Reputation: 3079

TCP or UDP for simple service

For a service which just returns a small number when queried such as 30, or 10, but would have to handle up to 5 or so requests at any instance, would TCP or UDP be a better protocol? I am leaning towards UDP, but I wanted some expert opinions. I am looking for relatively quick reply times as well. Could you tell me what the advantages of each would be for a service like this? Thanks.

Upvotes: 0

Views: 172

Answers (1)

Jonathon Reinhart
Jonathon Reinhart

Reputation: 137547

TCP is a reliable connection-based protocol. So, you are guaranteed that data is sent/received - the packets are automatically re-sent if they are not verified to be received on the other end. However, there is the overhead of the three-way handshake for establishing the connection.

TCP is used for protocols like HTTP where there is a one-time exchange of information (the HTTP Request and Reply).


UDP is an unreliable connection-less protocol. So you can simply send / receive a packet but you have no (automatic, OS stack-provided) way of verifying that the other end got your message. If you care, you have to implement some kind of ACK yourself.

UDP is used often for more continuous, "streaming" type protocols. For example, many online multiplayer games use UDP to exchange game information to/from the host. They do this on a continual, periodic basis. So if a packet is lost, it's not really a big deal, because another update is just around the corner. It would be far worse for the gameplay if you had to wait for that (now stale) update to be re-transmitted.

DNS is also implemented over UDP.


Ultimately the choice is yours. I would probably default to TCP for most cases, and only use UDP in a scenario like I described.

Upvotes: 3

Related Questions