Data via sockets across the Internet: compress or not?

When sending data via socket across the Internet, there are pros and cons regarding compressing data before sending.

Pro of compressing: less data to send and receive.

Pros of NOT compressing:

  1. You can directly send the buffer that the caller gives you. You don't have to allocate your own buffer to hold the compressed data. This avoids the overhead of allocating the buffer.

  2. The sender saves CPU by not compressing, and the receiver saves CPU by not decompressing.

So, is it a good idea to compress? Or the underlying networking layer already compresses?

1 answer

  • answered 2021-11-29 00:39 Marxys

    There is no typical answer. It's a trade-off depending on the use case.

    When i was student, I had to make a video game and tried to create my own byte code protocol to reduce size of packets by 10. That was a wast of time since packets in string format was not too big.

    But if you want to steam video, It of course mandatory to compress the stream. The compression buffer is not really a problem since you can compress a stream and you don't especially need to keep all the data before. But maybe you prefer to improve your bandwidth than your CPU.

    You have to think about what is the bottleneck of your application ? Is it network, memory or CPU intensive ?

    Too many optimizations at earlier level make you loose time for less results. Once your application is done, monitor it and then find the bottlenecks and optimize them in order.

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum