http HEAD vs GET performance

后端 未结 8 777
无人共我
无人共我 2020-11-30 17:57

I am setting-up a REST web service that just need to answer YES or NO, as fast as possible.

Designing a HEAD service seems the best way to do it but I would like to

相关标签:
8条回答
  • 2020-11-30 18:35

    HEAD requests are just like GET requests, except the body of the response is empty. This kind of request can be used when all you want is metadata about a file but don't need to transport all of the file's data.

    0 讨论(0)
  • 2020-11-30 18:42

    A RESTful URI should represent a "resource" at the server. Resources are often stored as a record in a database or a file on the filesystem. Unless the resource is large or is slow to retrieve at the server, you might not see a measurable gain by using HEAD instead of GET. It could be that retrieving the meta data is not any faster than retrieving the entire resource.

    You could implement both options and benchmark them to see which is faster, but rather than micro-optimize, I would focus on designing the ideal REST interface. A clean REST API is usually more valuable in the long run than a kludgey API that may or may not be faster. I'm not discouraging the use of HEAD, just suggesting that you only use it if it's the "right" design.

    If the information you need really is meta data about a resource that can be represented nicely in the HTTP headers, or to check if the resource exists or not, HEAD might work nicely.

    For example, suppose you want to check if resource 123 exists. A 200 means "yes" and a 404 means "no":

    HEAD /resources/123 HTTP/1.1
    [...]
    
    HTTP/1.1 404 Not Found
    [...]
    

    However, if the "yes" or "no" you want from your REST service is a part of the resource itself, rather than meta data, you should use GET.

    0 讨论(0)
  • 2020-11-30 18:44

    I strongly discourage this kind of approach.

    A RESTful service should respect the HTTP verbs semantics. The GET verb is meant to retrieve the content of the resource, while the HEAD verb will not return any content and may be used, for example, to see if a resource has changed, to know its size or its type, to check if it exists, and so on.

    And remember : early optimization is the root of all evil.

    0 讨论(0)
  • 2020-11-30 18:44

    You could easily make a small test to measure the performance yourself. I think the performance difference would be negligable, because if you're only returning 'Y' or 'N' in the body, it's a single extra byte appended to an already open stream.

    I'd also go with GET since it's more correct. You're not supposed to return content in HTTP headers, only metadata.

    0 讨论(0)
  • 2020-11-30 18:45

    I found this reply when looking for the same question that requester asked. I also found this at http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html:

    The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response. The metainformation contained in the HTTP headers in response to a HEAD request SHOULD be identical to the information sent in response to a GET request. This method can be used for obtaining metainformation about the entity implied by the request without transferring the entity-body itself. This method is often used for testing hypertext links for validity, accessibility, and recent modification.

    It would seem to me that the correct answer to requester's question is that it depends on what is represented by the REST protocol. For example, in my particular case, my REST protocol is used to retrieve fairly large (as in more than 10K) images. If I have a large number of such resources being checked on a constant basis, and given that I make use of the request headers, then it would make sense to use HEAD request, per w3.org's recommendations.

    0 讨论(0)
  • 2020-11-30 18:48

    GET fetches head + body, HEAD fetches head only. It should not be a matter of opinion which one is faster. I don't undestand the upvoted answers above. If you are looking for META information than go for HEAD, which is meant for this purpose.

    0 讨论(0)
提交回复
热议问题