Braek
Braek

Reputation: 601

Using HTTP for REST API: automatically cacheable?

I was wondering, to make a "RESTful API" you need to satisfy the 6 architectural constraints listed below:

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

Is it safe to state that when you are creating a REST API over the HTTP protocol, the "cacheable" constraint is automatically satisfied? Because HTTP already provides a cache system "out-of-the-box" through HTTP headers: http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html

So no need anymore to worry about that?

Maybe sounds like a stupid question, but I want to be sure. :-)

Kind regards!

K.

Upvotes: 1

Views: 2395

Answers (2)

Jan Holthusen
Jan Holthusen

Reputation: 111

Let me expand a bit on the challenges of creating correct caching logic: Typically, the backend of the API is a database holding all kinds of little pieces of information. The typical presentation within a REST API can be an accumulated view (So, let's say, a users activity log, containing a list of the last user actions within a portal, something along those lines). Now, in order to know if your API URL /user/123/activity has changed (after the timestamp the client is sending you in the "If-modified-since"-header), you would have to check if there have been any additional activities after the last request. The overhead of doing that might be the same as simply fetching the result again. So, in a lot of cases, people just don't really bother, which is a shame, as proper caching can have a huge impact on Web App performance.

Maybe this gives a bit more detail, Jan

Upvotes: 2

Jan Holthusen
Jan Holthusen

Reputation: 111

you are correct, HTTP already gives you the means to identify cacheable elements, but as your API will be generated by some server-side logic, you will still need to make sure the code "behind" your API will se the right HTTP headers and be ready and able to react to "If-modified-since" requests in an ideal world. Creating a reliable "Last-modified" timestamp as well as checking against it reliably is actually quiet a feat ;-)

Hope this helps a bit, Jan

Upvotes: 2

Related Questions