Should I use TCP or UDP to run a web server
Solution 1
Web servers work with the HTTP (and HTTPS) protocol which is TCP based.
As a general rule, if people neglect to specify whether they mean TCP/UDP/SomethingElse then they probably mean TCP.
Solution 2
TCP establishes a connection and UPD just sends packets.
You will have packet loss with UDP. Sites like youtube.com use UDP for video streaming because it doesn't matter if you miss a few frames. youtube.com uses UDP because it's faster than TCP because that connection isn't established and you probably wouldn't notice missing frames anyway.
You want to use TCP because you don't want packet loss.
Related videos on Youtube
Roman
Updated on September 17, 2022Comments
-
Roman over 1 year
I have just installed Apache web server on my computer. I have managed to use it locally (I can open index.php from my computer using my web browser). But I would like to make my web site available publicly. I found out that for that I need to open port 80. I started to do it and now I have to specify to which protocol I need to apply these rules (TCP or UDP). Can anybody, pleas, help me?
-
user1686 about 14 yearsWhen a video stream uses UDP, it also does so with a completely different protocol (such as RTSP). And HTTP - the website itself - always runs over TCP.
-
joschi about 14 yearsWhile your answer is correct from a practical point of view, you might want to note that the HTTP specification actually doesn't specify which transport protocol has to be used. So HTTP over UDP or HTTP over SCTP is as valid as HTTP over TCP, which is commonly used.
-
Axel about 14 yearsInteresting thought. UDP could make small requests (where the request and response each fit into a packet or two like many AJAX requests, small gifs and css files, ...) quicker by reducing latency, possibly making more difference than keep-alive connections. The presence of unreliability in the network could quickly kill the bonus though, and browsers would have to deal with the added complication of re-requesting lost packets. I wonder if anyone has tried it...
-
Daniel Papasian about 14 yearsNo, HTTP over UDP is not as valid as HTTP over TCP. From RFC2616 "HTTP only presumes a reliable transport; any protocol that provides such guarantees can be used" UDP does not presume a reliable transport.
-
Axel about 14 years@Daniel: Aye, that is why the browser (and server) would need to be aware of and account for packet loss in my thought-dump (essentially reinventing part of what TCP does for you, but perhaps in a way that might be more efficient in the average case for small requests/responses)
-
Daniel Papasian about 14 yearsDavid, in which case you'd be implementing HTTP over [whatever protocol you invented over UDP] and not merely HTTP over UDP.
-
Admin almost 11 yearsYoutube does not use UDP, because UDP does not have any flow or congestion control mechanisms. Youtube would literally crash the traffic of the whole Internet if they used UDP! Streaming is done with TCP.
-
kleinfreund almost 7 years@user179697 This is not entirely correct. Google developed QUIC which is based on UDP and it’s being used on Youtube when using Google Chrome. Why would UDP crash the traffic? What does this even mean?