Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an optimal byte size for sending data over a network?

I assume 100 bytes is too small and can slow down larger file transfers with all of the writes, but something like 1MB seems like it may be too much. Does anyone have any suggestions for an optimal chunk of bytes per write for sending data over a network?

To elaborate a bit more, I'm implementing something that sends data over a network connection and show the progress of that data being sent. I've noticed if I send large files at about 100 bytes each write, it is extremely slow but the progress bar works out very nicely. However, if I send at say 1M per write, it is much faster, but the progress bar doesn't work as nicely due to the larger chunks being sent.

like image 316
intargc Avatar asked Nov 20 '08 03:11

intargc


People also ask

What is the maximum size a data packet can be to travel through a router?

The maximum size of a TCP packet is 64K (65535 bytes). Generally, the packet size gets restricted by the Maximum Transmission Unit (MTU) of network resources. MTU is the maximum size of the data transfer limit set by hardware in a network. The packet size should never exceed MTU.

What is the best packet size?

There's no one right answer as to which packet size represents the "best" result. Medium-size frames, such as the 512-byte packets used in this test, are often considered useful, because they're close to the "average" Internet size of roughly 300 to 400 bytes.

What is the maximum size of payload at the network layer and at the data link layer for Ethernet cable in the TCP IP model?

IEEE standards specify a maximum payload of 1500 octets. Non-standard jumbo frames allow for larger payloads on networks built to support them.

What is the maximum TCP payload size?

The limit for the Max TCP Syslog Payload Length field is 32,000 bytes.


2 Answers

No, there is no universal optimal byte size.

TCP packets are subject to fragmentation, and while it would be nice to assume that everything from here to your destination is true ethernet with huge packet sizes, the reality is even if you can get the packet sizes of all the individual networks one of your packets takes, each packet you send out may take a different path through the internet.

It's not a problem you can "solve" and there's no universal ideal size.

Feed the data to the OS and TCP/IP stack as quickly as you can, and it'll dynamically adapt the packet size to the network connections (you should see the code they use for this optimization - it's really, really interesting. At least on the better stacks.)

If you control all the networks and stacks being used and inbetween your clients/servers, though, then you can do some hand tuning. But generally even then you'd have to have a really good grasp of the network and the data your sending before I'd suggest you approach it.

-Adam

like image 103
Adam Davis Avatar answered Oct 15 '22 02:10

Adam Davis


If you can, just let the IP stack handle it; most OSes have a lot of optimization already built in. Vista, for example, will dynamically alter various parameters to maximize throughput; second-guessing the algorithm is unlikely to be beneficial.

This is especially true in higher-order languages, far from the actual wire, like C#; there are enough layers between you and actual TCP/IP packets that I would expect your code to have relatively little impact on throughput.

At worst, test various message sizes in various situations for yourself; few solutions are one-size-fits-all.

like image 39
technophile Avatar answered Oct 15 '22 00:10

technophile