Ajax GET or POST - which is best?

A while ago, we published a blog entry here about browser behaviour and performance where we told the story of how most browsers seem to transmit AJAX POST requests (i.e. HTTP POST requests originated from Javascript code) as two separate TCP packets, while a small number of browsers - most notably recent versions of Firefox - transmit the same request using only a single TCP packet.

This behaviour was discovered by a Yahoo developer originally, and later on investigated and confirmed by Joseph Scott on his blog. We went further and mapped a wide range of browsers on several different operating systems, to find out in what circumstances the request was sent as two packets. The results can be read in a PDF report we created, if you're interested. (If you are not, then you should probably stop reading now, because for non-geeks, this post is only going to get worse from here)

Because we are such mega nerds when it comes to performance stuff, we decided to dig deeper into this, and find out just how big a performance impact the behaviour had. I.e. we wanted to see how much slower a request would be, on average, when the browser transmitted two instead of one TCP packet. Logically, more packets mean more overhead and a bigger risk of one packet getting lost and having to be resent, causing extra delay. Yahoo actually advice people against using POST in Ajax requests because of this behaviour - see Yahoo's ySlow performance rule #15 - Use GET for Ajax requests - but they don't tell you what it means for performance if you do/do not use POST. To us, just like to Yahoo apparently, it seemed rather obvious that sending two packets was worse than sending one packet, but maybe it shouldn't be a general performance recommendation unless it has a significant impact (if not, then other recommendations would perhaps have been more important to get across to ySlow users than this #15).

So, we wanted to see how big the impact actually was. By sending a large number of AJAX requests using both the GET and POST methods, we reasoned that we should be able to see some statistical difference between the response times of the two. Especially if we also simulated some network delay and packet loss on the link, the difference had to be quite noticeable. Our goal was to be able to say e.g. that using GET instead of POST in an Ajax request can reduce average transaction time by up to X% or more, depending on network conditions.

We set up the test, and spent several days running hundreds of thousands of Ajax GET and POST requests using different browsers, and emulating different network characteristics (i.e. network delay and packet loss). In the end we found something we weren't prepared for - the tests showed that sending two packets seemed to slightly outperform sending one packet. (Again, read the PDF report if you want more details) 

After scratching our heads, asking around, double-checking the data, reading up on TCP and its most common implementations, then scratching our heads some more, we still couldn't understand how this came to be. There might have been problems with our setup somehow, but it seems far-fetched that any problems with our setup would give a 2-packet transaction an advantage over a 1-packet transaction. We decided to publish the results and see if anyone could explain what we were seeing, or tell us what we were doing wrong. We have since had a couple of people offer things such as "two packets allow faster resend if the first packet is lost" or "sending more packets might help TCP compute the RTT better". But none of the explanations seem to suggest anything that could outweigh the penalty you get from the fact that the 2-packet transaction suffers twice as much from packet loss as the 1-packet transaction. If it does - maybe that's the assumption we make that is wrong.

At the Velocity 2010 conference, I ran into a well known web performance guru and while talking to him a guy from the Yahoo ySlow team showed up, so I mentioned our findings to them both. The guru said he didn't believe me, and tried to change the subject. The Yahoo guy didn't say anything but walked off as soon as he got the chance. The obvious conclusion from this encounter is that I have to work on my people skills. Or maybe they were just stressed, or suffering from caffeine deficiency (it was a coffee break I think) - who knows? Maybe I was suffering from caffeine deficiency. I hope I don't come off as one of those annoying people who can't stop trying to find faults in stuff other people do, in an attempt to feel superior. Because I am not - I just can't help trying to improve things everywhere, all the time. It's like a drug to me. I have to figure out how things work, and then try to improve them. Have to. I can probably be a pain about it - when people do something really well and expect praise I will usually tell them "Nice! The only thing you might want to change for next time is [whatever]..." and many take that as a sign that I don't like what they've done. For me, constructive criticism is the finest form of praise!

Anyway, enough of the boring introspection and back to the important stuff - investigating the problem! I decided that if people don't believe me, and they don't have time to set up a test bed and run the tests for themselves, the obvious solution is that I set up a test bed they can run tests on remotely. If I can get the critics to observe the strange behaviour first-hand, I might force them to acknowledge that it does happen, if nothing else! Then more people will be likely to try and come up with an explanation. I would be happy to learn that there is some problem with our setup, for instance, because then I would at least know. Not knowing is frustrating. How can I work to improve something unless I understand it first?

So, I have now a test system up and running where anyone can try executing 50 Javascript GET, then 50 Javascript POST operations and see which are faster. I also set up the test on both Apache 2 and Lighttpd, in case this problem is webserver-dependent somehow. I thought, why not name it "Scott's problem", after the Joseph Scott who first bothered to describe it in detail. So the address to the test pages are:

On the page you can see the tests other people have executed using different browsers, and their results. For each unique user agent you will get a summary with average transaction times. Feel free to run multiple tests if you want, you will probably not skew the data as the system only uses the average transaction time seen from a certain IP and a certain browser, when calculating average transaction times for that particular browser. The machine is an Ubuntu server, in case anyone cares.

And yes, I know my web hacker skills are pretty awful. Don't look too closely at the javascript, or the HTML, please. You just might go blind.

Browser behaviour and performance

We here at Load Impact are always interested in learning more about browser behaviour, and when we saw Steve Souders new Browserscope project, we really liked it and decided to help out, if we could. Browserscope profiles web browsers, and tries to determine what browsers have support for new features, are suffering from known security issues, or have certain performance-affecting "quirks". It basically runs an extensive test suite on your browser and tells you how good your browser is. Go and check it out, we think it is very cool.

Naturally, we are most interested in the performance-affecting "quirks" browsers have. These mostly fall under the "Network" category in Browserscope (go to browsercope.org and click on the Network tab) and can be stuff like how many concurrent TCP connections a certain browser will open to one and the same host. Internet Explorer 7 will only open max 2 concurrent connections to a webserver, while Internet Explorer 8 will open up to 6 concurrent connections. That means that IE8 can download 6 files at the same time, while IE7 can only download 2 files at the same time. It definitely affects performance, and as such, is very interesting to us (note that the opening of many connections might be a good thing from a client perspective, but worse for the server that has to keep a ton of connections open - we don't advocate opening more connections as a universal way to increase performance...)

We have actually modeled our Web page analyzer on these performance-affecting characteristics that Browserscope has identified for us. Our web page analyzer can emulate different well-known browsers when it analyzes a web page, and the way it does that is by mimicking the behaviours you can read about under the Browserscope Network test category. It emulates browser-specific performance-affecting behavour such as the max number of concurrent TCP connections to open, etc. and thanks to this it can provide fairly accurate predictions of page load times in most major browsers.

Anyway, we decided to try and create a Browserscope test to see what browsers support HTTP trailing headers. This is a feature of HTTP 1.1 that very few browsers have implemented. It means that a server can transmit extra HTTP headers after the HTTP body has been sent. It is useful if, for example, a server generates a large PDF file dynamically and wants to include a checksum (using the Content-MD5 HTTP header), but wants to start sending the file immediately and avoid buffering the whole of the file in RAM. If the browser at the other end support HTTP trailing headers, the server can generate the file while sending it, and while computing the checksum on the fly. Then when the file has been transmitted, the server just add the Content-MD5 HTTP header at the end.

The new test is live at browserscope.org now. It lies under the Network category and is called "Headers in trailers". As you can see, it is only Opera (and Palm Pre!) that seem to support the feature

And now for something really strange

That was a case of finding out whether browsers support a standard HTTP feature or not, but there was also another, more exotic browser characteristic we wanted to dig into. The strange phenomenon that most modern browsers always send Ajax HTTP POST requests as two or more TCP packets. That is, even if the request is small enough to fit into a single packet.

This was apparently first observed by a Yahoo developer, then re-posted a couple of times until Joseph Scott tested various browsers to see which were affected, and found out that most browsers had this tendency, with Firefox being a notable exception. Many have speculated about the possible performance impact of this, and Yahoo have actually incorporated it into their Best Practises for Speeding Up Your Web Site as a separate rule/tip, where they advise people to use GET instead of POST requests in javascript code, because the POST requests may be transmitted using an extra TCP packet. But there is precious little hard data about what this browser behaviour actually does for performance, and we also thought the browser survey done by Joseph Scott was a bit incomplete, so we set out to investigate a bit more.

First, we performed a major survey where we learned that Sea Monkey, Galeon and most versions of Firefox (on various platforms), were the only browsers that sent short HTTP POST requests as a single TCP packet. The rest of the bunch always sent one TCP packet containing just HTTP headers, and one or more TCP packets containing the HTTP body, when a javascript performed an HTTP POST request (note that this  only applies to javascript-generated requests).

Then we wanted to find out how big the performance impact was. We imagined that an Ajax application performing a lot of small HTTP POST requests over a high-latency, high-packetloss link would see its performance deteriorate badly due to the extra time spent resending lost packets, if the user used one of the browsers always sending two packets instead of one. More packets equal more packets lost, which means more waiting for resend timeouts and then more resending of packets.

So, we set up a test bed where we simulated packet loss and network delay between client and server. Then we let the 1-packet browser Firefox compete against the 2-packet browser Safari, to see which one fared the worst. The results were a bit surprising. It turned out that while both browsers saw their average transaction times deteriorate substantially with increased network delay and increased packet loss, Safari did slightly better than Firefox overall. The increase in average transaction time due to packet loss in Safari were not as great as the same increase in Firefox. If you're having a slow day and want more detail, here is a PDF report from the tests.

And you people who aren't very interested in arcane TCP matters can probably stop reading now :-)

Why is sending two packets just as good as, or better than, sending one? I have been discussing this with some networking people on an email list recently, and two possible answers came up:

  • if packet #1 sent by Safari is lost, retransmission will occur faster if packet #2 gets delivered, as the receiver will ACK packet #2, whereupon the sender will see that receiver is still waiting for packet #1 and most likely also lower its retransmission timeout for packet #1.
  • sending more packets allows TCP to keep better track of network roundtrip times, which are used to set reasonable retransmission timeout values.

What is really interesting here is that the results seem to go against the Yahoo recommendations. There seems to be no discernible performance hit for using POST requests. At least not on the client side. We would love to hear from people who are more familiar with current TCP implementations and who can offer some explanations for this behaviour.

 

 

 1