9

In html, I know there is a chunked encoding. Anyone knows its advantage?

2 Answers 2

18

It's not part of HTML - it's part of HTTP.

It means that you can start writing the content to the output stream before you know exactly how large the output is going to be. This means you don't have to buffer the whole page (or whatever you're delivering) in memory or on disk before you start transmitting.

2
  • 1
    few more benefits explained here en.wikipedia.org/wiki/Chunked_transfer_encoding
    – Adeel
    Commented Feb 20, 2011 at 9:42
  • And so it's clearly useful when you are (for example) generating the content "on the fly". In this way the Web Server doesn't have to generate the full content, see the length, and send it. Instead it can stream it.
    – xanatos
    Commented Feb 20, 2011 at 9:45
12

You need to be aware of its disadvantages, too. Some firewalls/antiviruses want to download the complete response so they can inspect it, and will therefore block any incomplete response chunks from reaching the client.

A lot of firewalls have chunked encoding set to block by default, especially on corporate networks. If you want people to be able to reach your web service from their work computers, you need to either https the whole website (since https traffic cannot be inspected), or avoid chunked transfers.

The only situation that I can think of where that downside is justified is https streaming. If you don't need streaming, it's not worth it, in my opinion.

Responding to comment below here because I think it's important.

Network problems with chunked responses are very common. In my current project (B2B so every customer is behind a corporate network) I estimate roughly 3/4 customers experience issues.

To prove/disprove, I set up a test that sends 2 identical responses, one regular and one chunked (streaming chunks of html in 1 sec intervals for 3 minutes). Chunked response was consistently blocked (customer observed blank page) or accumulated (customer observed blank page for 3 minutes followed by complete rendering of html). Same chunked response was fine over https (customer observed incremental rendering of html in 1 sec intervals). I ran this on different customers/firewalls.

This is a known issue. You can read up on websockets, part of their appeal is that they help overcome these very common firewall/proxy problems.

3
  • 3
    I have never ever had the problems you mentioned. Can you supply some evidence? Commented Dec 12, 2012 at 18:45
  • 1
    @RADA do you think this warning is still valid in today's networking environments (2017)? I have been told that Transfer-Encoding: chunked is becoming the norm for dealing with TCP Slow Start on mobile networks in spotty service environments, like a subway.
    – Todd
    Commented May 20, 2017 at 18:08
  • @Todd Transfer-Encoding is a part of HTTP protocol and TCP protocol is something completely different
    – Dawid D
    Commented Apr 21, 2021 at 18:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.