Caching is one of the common techniques that is used to
increase system and application performance. Caching is used by web
applications to cache frequently used web pages. Some of the key considerations
to caching data are if:
The data is expensive to create.
The data does not change very frequently.
The data is used frequently.
Caching usually holds data in memory. With caching also
comes the responsibility of ensuring that updated data is served as soon as
possible and also managing how much to cache. I recently put together an article that talks about using kernel mode caching in IIS.
Today, I will discuss another advanced caching technique provided by ASP.NET
2.0 using SQL Server 2005 using notification services. This new caching method
is known as SQL Cache dependency. SQL Cache dependency enables a developer to
cache pages that are dependent on data from SQL Server tables. This technique
has 2 methods - Polling based and Notification based cache
invalidation. In the polling based method, applications query if the
underlying data changed at specific time intervals. In notification based
services, SQL Server sends out a notification to the "subscriber" of
the data. The application can then take whatever action it wants.
Notification based technique is most likely a preferred
technique over the previous polling based method. With this technique, any
application that depends on data within a SQL Server 2005 database can
subscribe to be notified by SQL Server, when the underlying data changes. The
way it works is that the application queries the data from SQL server and puts
in a request to be notified when the data that was just queried changes. It
then caches the results of the query as long as no notification is received.
When a change notification is received by the application, it invalidates the
cache and then re-queries the data with a new change notification request. The
next request to be handled by the application will show the updated data. SQL
notification services can send timely and personalized messages to millions of
subscribers. These subscribers can be applications or devices.
One of the biggest advantages of this technique is that your
cached data is valid as long as the underlying data has not changed. Unlike in
earlier versions, the application need not poll the database with a specified
time interval to see if the data has changed and then update the cache.
I will focus first on how we can setup use notification
services with ASP.net 2.0 web applications. There is a lot of content out there
that describes this, but probably very few that explain how it works and how to
set this up and test. SQL Server 2005 Notification-based cache invalidation
uses the query change notification mechanism of SQL Server 2005 to detect
changes to the results of queries. Query notifications are sent over SQL
connections, just like ADO.NET queries. Think of query notifications like long
running queries that wait for a "notification" from SQL Server
indicating that the underlying data has changed. This allows the application to
invalidate the cache.
SQL Server 2005 System Requirements
SQL Server 2005 Standard or Enterprise Editions