Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpWebRequest.CachePolicy: questions about caching

If I set my HttpWebRequest.CachePolicy as follows:

var webRequest = (HttpWebRequest) WebRequest.Create(url);
var policy = new HttpRequestCachePolicy( HttpCacheAgeControl.MaxAge,
                                         TimeSpan.FromMinutes(1) );
webRequest.CachePolicy = policy;

and make two async requests for the same URL at exactly the same moment, what happens to the second request? Does the second complete only when the first has cached, or will 2 requests be issued because nothing is in cache at the time of issue?

Also, in this context, what is the cache? Where does it live? Do we get more control over it?

like image 751
spender Avatar asked Nov 20 '10 02:11

spender


2 Answers

Two such requests in the .NET code will cause two such HTTP requests, which can be checked quite easily by just building something that does so, running it, and then testing what happens at the server.

This is appropriate because it may be that the two requests will receive a different response, especially considering that one of them might suffer an error that the other doesn't. There are other reasons (e.g. the server may send a response that differs every time along with instructions that it shouldn't be cached).

However, there can be an exception. There is a default limit on the number of requests that will simultaneously be sent to the same domain, which is configurable but defaults to two (this is something often complained about because it is inappropriate in some use-cases - however two requests per server does give the highest total throughput in most cases, so it's there for a good reason).

Because of this, it's quite possible that one of the two requests will be delayed as it is queued due to this rule. Even if the default limit is increased, it's possible that that limit was still surpassed.

Now, as Marc notes a response can be cached once its response stream has been read to the end*, and this may have happened by the time the second request begins, which would lead to it using the cached response if applicable (the response was cacheable and there were no errors in downloading it).

So, on balance we would expect there to be two separate downloads and we should be glad of that (in case one has an error), but there are possible conditions in which there will be only one download as the two "simultaneous" requests are forced to not actually be simultaneous.

*Actually, while the documentation says the stream has to be read to the end, it actually has to be read to the end and closed, whether manually, by disposing it (e.g. from using) or from its finaliser being executed.

like image 80
Jon Hanna Avatar answered Oct 06 '22 16:10

Jon Hanna


Firstly, HttpWebRequest is documented as only being able to perform a single async request at a time (raising InvalidOperationException), so you would need two such requests. If you raised two such requests at the same time I would fully expect both to go to the server - there would be no reason not to. In particular:

A copy of a resource is only added to the cache if the response stream for the resource is retrieved and read to the end of the stream. So another request for the same resource could use a cached copy, depending on the cache policy level for this request.

So at the point of making the request, we should assume (from the question) that the data has not yet been read - so nothing will be cached locally yet.

Depending on whether both requests are routed to the same server, the server might queue the requests, and might be configured to cache the result, but in all likelihood the server(s) will just process everything here twice.

like image 23
Marc Gravell Avatar answered Oct 06 '22 16:10

Marc Gravell