I am building a REST service, i am expecting a lot of requests (even DoS attacks). So thinking about network and cpu consumption which HTTP method would be better for my server (accepting POST or GET)?

I have made some tests... Using a VM Ubuntu 14.04 (1 core) as server, with apache and php

get.php:

$s = "G: ";
foreach ( $_GET as $key => $val){
    $s .= $val . ", ";
}
echo $s;

post.php:

$s = "P: ";
foreach ( $_POST as $key => $val){
    $s .= $val . ", ";
}
echo $s;

Test #1 (using ab):

ab -n 10000 'http://10.0.0.112/get.php?key1=val1&key2=val2&key3=val3'
Concurrency Level:      1
Time taken for tests:   9.080 seconds
Complete requests:      10000
Failed requests:        0
Total transferred:      2080000 bytes
HTML transferred:       210000 bytes
Requests per second:    1101.33 [#/sec] (mean)
Time per request:       0.908 [ms] (mean)
Time per request:       0.908 [ms] (mean, across all concurrent requests)
Transfer rate:          223.71 [Kbytes/sec] received

ab -n 10000 -p post.data -T application/x-www-form-urlencoded 'http://10.0.0.112/post.php'
Concurrency Level:      1
Time taken for tests:   9.526 seconds
Complete requests:      10000
Failed requests:        0
Total transferred:      2090000 bytes
Total body sent:        1860000
HTML transferred:       220000 bytes
Requests per second:    1049.72 [#/sec] (mean)
Time per request:       0.953 [ms] (mean)
Time per request:       0.953 [ms] (mean, across all concurrent requests)
Transfer rate:          214.25 [Kbytes/sec] received

I run the AB test several times, with same results: POST is faster but GET is lighter. So i thought in a second test (something more real).

Test #2 (using wget):

TIME_POST=0
TIME_GET=0
X1=100
X2=10

function fpost {
        START=$(date +%s.%N)
        i=0
        while [ $i -lt $X1 ]
        do
                wget -q -O out.file 'http://10.0.0.112/post.php' --post-data 'key1=val1&key2=val2&key3=val3'
                rm -rf out.file
                i=$[$i+1]
        done
        END=$(date +%s.%N)
        T=$(echo "$END - $START" | bc)
        echo "POST: $T"
        TIME_POST=$(echo "$TIME_POST + $T" | bc)
}

function fget {
        START=$(date +%s.%N)
        i=0
        while [ $i -lt $X1 ]
        do
                wget -q -O out.file 'http://10.0.0.112/get.php?key1=val1&key2=val2&key3=val3'
                rm -rf out.file
                i=$[$i+1]
        done
        END=$(date +%s.%N)
        T=$(echo "$END - $START" | bc)
        echo "GET:  $T"
        TIME_GET=$(echo "$TIME_GET + $T" | bc)
}

j=0
while [ $j -lt $X2 ]
do
        echo "#"$[$j+1]
        fpost
        fget
        echo
        j=$[$j+1]
done
echo "TIME POST: $TIME_POST"
echo "TIME GET: $TIME_GET"

mmmmm with the wget test.... GET is faster:

...
TIME POST: 54.707362313
TIME GET: 53.049255400

Which HTTP method has better performance?

Should i care about it?

Should i expect different results with nginx or nodejs (expess)?

share|improve this question
    
The answers so far are both good, but your question is about speed. You are likely testing the speed of ab and wget more than anything else here; POST and GET should not have any inherent, significant differences in speed. – Ed Cottrell Nov 14 '14 at 5:18

When dealing with an api, I think most developers these days are accustomed to using a RESTful interface. So it isn't so much a question of what is faster, but what is the purpose of the request. So when you say you are building a RESTful api, there are rules that supersede the question you are asking. That is, the question of speed is irrelevant.

http://en.wikipedia.org/wiki/Representational_state_transfer

In terms of the results you posted, GET moves less data and should always be a bit quicker.

share|improve this answer

As pretty much everything in life, it depends.

Normally I would not say that performance is highly impacted by the method but by the I/O, meaning that it is way easier to improve the slowest part first, and that is normally the Input/Output operations (database, files...) normally adding a cache layer and checking your queries will boost the performance of your api.

Also, from a "pure" point of view GET ~ Read and POST ~ write (not to mention PUT and DELETE).

Finally consider that some requests to your API would be a little bit to complicated to be done via GET (imagine for example a request where you want employees whose name start with J, are older than 35 and their salary is between 30 and 45k EUR...), in that scenario even for a reading operation, POST might (just might) be more convenient.

So to summarize, do not worry too much about the performance difference between GET and POST and worry more about the internals.

Uh! last advice, keep your API stateless.

share|improve this answer

GET might be slightly faster

Quoting from treehouse blog

When using XMLHttpRequest, browsers implement POST as a two-step process (sending the headers first and then the data).

share|improve this answer
    
I do not agree: 1) it will not be much slower... 2) I do not know what is 2 HTTP calls.. But it will be 2 network packets. 3) Firefox still will send only 1 packet.. 4) Some guys claims that Chrome 53 sends already 1 packet. – Maxim May 5 '17 at 4:46
    
Agreed. Corrected. – Shekhar May 19 '17 at 17:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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