Thursday, July 18, 2013

IIS 7 Page Caching May Cause Your Dynamic Page Not To Execute

I just ran into an interesting situation, which a dynamic page did not update at all even though the web call succeeded. This really puzzled us for a bit of time this afternoon, fortunately I realized what was actually happening and solved the problem. The point of this article is that if you did not have any idea what is happening under the hood, it would have puzzled you even the longest time and even make you go down the wrong path.

So, first what I was trying to do.

I wrote a very simple ASP.NET REST type application to log errors and such and the logging message came only in the URL. I tested it and worked fine.

What has happened though is that once it was put in production my colleague has noted that if the identical log message was sent repeatedly and subsequently, it stopped logging the second message and the later ones.

The program is really simple, it just parsed the URL and anything after ?m= parameter it logged that information to an SQL database along with the time the request came in. For example ?m="Process X called"

The root cause of this was that if a same message was sent then the URL would have been identical the second time. Now IIS caching mechanism thinks that since this is the same URL, it would not execute the actual ASP.NET program, but just show the page that was already generated. So in effect it did not log subsequent messages. Initially we though the SQL server has some mechanism that would prevent the same message from being logged, though that' would be totally absurd.

Then I thought that this must be due to some sort of cache optimization on the server side.

Sure enough, we have confirmed that this is actually the case, as unique messages do log, and since as soon as we added ?t= in the URL in which a sequence number incremented and then every message logged OK.

Now I have confirmed that the Server Side Page Caching is a feature in IIS and that it can be controlled in many ways. This behavior is also enabled by default.

Knowing this behavior exists will help you understand why some page update do not happen as you expect. This is especially true for dynamically generated pages.

For further information on this topic, searching for IIS Cache on your favorite search engine or on the MSDN web site.