Anyone who has written a web application has probably experienced the cost of doing a database round trip for fetching a result set everytime a web user loads a page. In fact, handling a page request may involve more than one queries (imagine 20 drop-down boxes, each of which must be populated). A fix to this problem is caching frequently requested result sets in memory to avoid the database round trip. The cache can be invalidated periodically get a more recent version of the actual data. This technique seems to work fine if the data is not updated frequently and the application does not really need the result sets to be fresh. What would be ideal in this case is for the database to notify the cache when it gets dirty so that the application can fetch the updated result set on demand. Most database engines have the infrastructure for making this possible and it is used for maintaining materialized views. SQL Server 2005 provides a mechansim to hook into this plumbing in order to allow external applications to be notified when views are invalidated. The feature is called Query Notification.
The figure above shows typical interactions for the desired behaviour:
- Gerald sends a web-request to the web-server.
- Page handler performs a SELECT query on the database.
- Database returns a result set.
- a. Web-server generates a response page and sends it back to Gerald.
b. Web-server stores the result set in its cache.
- Ivan updates the table which would affect the result of the SELECT query above.
- Database fires a query notification.
- Web-server issues the SELECT query again.
- Database returns the new result set.
- Web-server updates the cache with new result set.
- Gus requests the same web-page.
- Page handler finds cached version of the result set.
- Web-server generates a response page and sends it back to Gus.
Query notifications use Service Broker underneath to send notification messages to a subscribed service. You can subscribe for query notifications and have a custom message be sent back in the body of the notification when the query's results are invalidated. The notification message looks something like this:
Type="change" Source="data" Info="insert">
The <qn:Message/> element contains the custom payload and could be a URL, a GUID or anything you like. I tried to create a sample web application that caches result sets and uses query notifications for maintaining coherency. I have built web-apps in the past using PHP and Java Servlets, but ASP.NET is completely new territory for me. So I'm sure there are better ways of doing this. While looking at ASP.NET and ADO.NET, I realized there are at least half a dozen ways of using query notifications -- from the simplest that require no lines of code, just some declarations:
<form id="form1" runat="server">
<asp:GridView ID="grdItems" Runat="server"
<asp:SqlDataSource ID="SqlDataSource1" Runat="server"
ConnectionString="<%$ ConnectionStrings:mySqlServer %>"
SelectCommand="SELECT i.Name as Item, i.UnitPrice as UnitPrice, c.Name as Category FROM dbo.Item i JOIN dbo.Category c ON i.CatId=c.CatId ORDER BY i.Name"
to actually implementing the service that receives the notifications. This article by Bob Beauchemin goes into the details of using Query Notifications in ADO.NET 2.0.
void Application_Start(Object sender, EventArgs e)
using (SqlConnection conn = new SqlConnection(
// Construct the command to get Items data
SqlCommand cmd = new SqlCommand(
"SELECT i.Name as Item, i.UnitPrice as UnitPrice, " +
"c.Name as Category FROM dbo.Item i JOIN " +
"dbo.Category c ON i.CatId=c.CatId ORDER BY i.Name",
// Create a dependency on this query.
SqlDependency dependency = new SqlDependency(cmd);
// Register the event handler which will be called
// when the base tables are updated.
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = cmd;
DataSet ds = new DataSet("MyDataSet");
Application.Contents["ds"] = ds;
// To ensure that object is not garbage collected
Application.Contents["dep"] = dependency;
private void Dependency_OnChanged(object sender,
The key idea is to create a SqlDependency object with the appropriate event handler to bind to the SqlCommand. When the command is executed, the user subscribes to the query notification. SqlDependency uses a default stored proc that is internally activated. The stored proc uses a TCP or HTTP channel to actually fire an event in the client process. In our event handler, we re-fetch the result set and register another SqlDependency.
<%@ Page Language="C#" ClassName="qncache_aspx" Debug="true" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data" %>
private DataSet ds;
public void Page_Load()
ds = (DataSet)Application.Contents["ds"];
if (ds != null)
grdItems.DataSource = ds.Tables;
<title>Output Cached Items</title>
<form id="form1" runat="server">
<asp:GridView ID="grdItems" Runat="server" />