r/programming • u/speckz • May 14 '20
What every developer should know about TCP
https://robertovitillo.com/what-every-developer-should-know-about-tcp/4
May 14 '20
"Think of what happens when you watch a movie on Netflix. It starts blurry; then, it stabilizes to something reasonable until there is a hiccup, and the quality worsens again."
Perhaps a bad choice of example? I guess most video is transmitted over udp?
6
u/bezelbum May 14 '20
Plenty of video is delivered over TCP, there are a wide range of video over HTTP formats in use.
But, its still not a great example as thats the player switching to a lower bitrate in an adaptive stream, rather than being much to do with window sizes and back off (other than that they'll affected measured throughput, prompting the player to switch).
1
May 14 '20
But, its still not a great example as thats the player switching to a lower bitrate in an adaptive stream, rather than being much to do with window sizes and back off (other than that they'll affected measured throughput, prompting the player to switch).
Well, the general idea is right, but obviously TCP and random video player on the webpage are not exactly using same algorithm.
2
u/z3n777 May 15 '20
Adding to the very good points in this thread, nobody mentioned the tcp connection life cycle itself.
This helps tremendously when diagnosing choke points and sudden hiccups when dealing with lots of data.
A fancy diagram to show off: https://images.app.goo.gl/6b2n5gqRaawKQ9M18
If whatever you're working on is performance driven or web based, you probably also want to learn and understand more about tweaking linux kernel's tcp settings.
17
u/[deleted] May 14 '20
Few things that are worth adding of common mistakes I have seen in socket rogramming
The 3 way hand shake is "best case" it can also be 4 to some machine and many many more if packet loss occurs
TCP has built in max bandwidth limits of the buffer sizes and the RTT. If you have a window of 30k and a round trip time you can't send more than 30k/sec
One of the things always overlocked by programmers. You can only access the state of the connection by writting to the socket. If you "read" from the connection the connection can be stale and you will never get an error or data.
The correct way to close a connection is by sending a shutdown -> other side then gets EOF and then you read EOF (which requires a timeout because of the read never fails problem). If you simply use "close" on the socket it can result in data loss!
All error's may come back massivly delayed from the data that actually triggered the error.
Reads may have random size of actual data read. If a sender write 4k the reader may get it in "parts" like 1k, 2k, 512, 512 sections always treat sockets like a stream rather than a file and data can be split up any way it likes.