views:

77

answers:

2

Imagine the following use case:

I use an AJAX request for getting some info about Item and use this URL: http://domain/items/show/1

In my database all items have a field called modified_at where we store the moment when this item was previously modified.

How can Last-Modified server HTTP header in response can minimize load/reduce requests/increase responsiveness if we need to process this request every time on the server side? It looks like we don't reduce the number of HTTP requests with that response and we don't reduce the load on server.

Who needs this anyway?

Am I right, that it's used mostly for the purpose of saving bandwidth?

+1  A: 

If your implementation will always require a db query, chances are implementing 304 won't do much good. You might only be saving the resources required for rendering the response. If your rendering will require a lot of processing, it might still be worth it, even if you require a db query.

However, if you have a mechanism for mapping a request uri to an expires date without using a database, you might notice bigger improvement in response times and saved server resources.

How I've implemented a similar scenario, was by caching every request to disk. The first line in the file (named in a way that required no scanning) contains meta data, such as etag and ttl. Based on the modified time for the file, and the ttl stored in it, I can, by reading only one line from disk, decide if I should send a 304 response (returning client), the contents of the cache file (new client, or returning client that hasn't seen recent rendering), or process the request normally caching the refreshed result at the same time.

See this question for further information about implementing 304 responses. http://stackoverflow.com/questions/2021882/is-my-implementation-of-http-conditional-get-answers-in-php-is-ok

nikc
+3  A: 

The purpose is to save bandwidth, not on your server but on the client. Uncachable AJAX requests will likely make the UI incredibly slow for your visitors, not having to transfer data over and over again drastically improves performance in the client's browser.

If you want to reduce the number of reqs you should set an explicit Expires header on the response. The client won't request the resource until the time set by Expires has run out.

Martin