Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL Server Notifications: Invalidating Cached Results Michael Pizzo Software Architect Microsoft Corporation Michael Pizzo Software.

Similar presentations


Presentation on theme: "SQL Server Notifications: Invalidating Cached Results Michael Pizzo Software Architect Microsoft Corporation Michael Pizzo Software."— Presentation transcript:

1 SQL Server Notifications: Invalidating Cached Results Michael Pizzo Software Architect Microsoft Corporation mikep@microsoft.com Michael Pizzo Software Architect Microsoft Corporation mikep@microsoft.com Session Code: Dat405

2 Agenda Building a Scalable Enterprise Whidbey SqlNotifications and Caching Implementing Summary Building a Scalable Enterprise Whidbey SqlNotifications and Caching Implementing Summary

3 Tools Client Application Model AvalonWindows Forms Web & Service Application Model ASP.NET / Indigo Win FS Compact Framework Yukon Mobile PC Optimized System.Help System.Drawing System.NaturalLanguageServices Data Systems Application Model Presentation Data Mobile PC & Devices Application Model Communication Command Line NT Service DataSet Mapping ObjectSpaces ObjectSpace Query Schema Item Relationship Media Audio Video Images System.Messaging System. Discovery System.DirectoryServices System.Remoting System.Runtime.Remoting Active Directory Uddi System.Web.Services Web.Service Description Discovery Protocols System.MessageBus Transport Port Channel Service Queue PubSub Router System.Timers System.Globalization System.Serialization System.Threading System.Text System.Design Base & Application Services Fundamentals System.ComponentModel System.CodeDom System.Reflection System.EnterpriseServices System.Transactions Security System.Windows. TrustManagement System.Web. Security System.Message Bus.Security Authorization AccessControl Credentials Cryptography System.Web.Configuration System.MessageBus.Configuration System.Configuration System.Resources System.Management System.Deployment System.Diagnostics ConfigurationDeployment/Management System.Windows System.Windows.Forms System.Console System.ServiceProcess System.Windows.Forms System.Web System.Storage System.Data.SqlServer Animation Controls Control Design Panel Controls Dialogs SideBar Notification System.Windows Documents Text Element Shapes Shape Ink UI Element Explorer Media System.Windows.Forms Forms Control Print Dialog Design System.Web.UI Page Control HtmlControls MobileControls WebControls Adaptors Design Ports InteropServices System.Runtime System.IO System.Collections Generic System.Search Annotations Monitoring Logging Relevance System.Data SqlClient SqlTypes SqlXML OdbcClient OleDbClient OracleClient Core Contact Location Message Document Event System.Storage System.Web Personalization Caching SessionState System.Xml Schema Serialization Xpath Query Permissions Policy Principal Token System.Security System.Collaboration RealTimeEndpoint TransientDataSession SignalingSession Media Activities HttpWebRequest FtpWebListener SslClientStream WebClient System.Net NetworkInformation Sockets Cache System.Web Administration Management Navigation Peer Group Policy Serialization CompilerServices Recognition System.Speech Synthesis

4 Agenda Building a Scalable Enterprise Scalable Data-Driven Enterprise Applications Scaling Solutions Web Caching Benefits Caching in ASP.NET 1.x Caching Dynamic Content Whidbey SqlNotifications and Caching Implementing Summary Building a Scalable Enterprise Scalable Data-Driven Enterprise Applications Scaling Solutions Web Caching Benefits Caching in ASP.NET 1.x Caching Dynamic Content Whidbey SqlNotifications and Caching Implementing Summary

5 Scaleable Data-Driven Enterprise Applications Data-Driven Enterprise Applications WebServices Dynamic Web Applications Product-driven storefront Search sites Informational sites (weather, traffic, news, …) Portals Internal lookup tables (zip codes, tax tables,…) Etc… Increased Scalability==Reduced Contention for shared resources In Data-Driven applications, data becomes the shared state that limits scalability Data-Driven Enterprise Applications WebServices Dynamic Web Applications Product-driven storefront Search sites Informational sites (weather, traffic, news, …) Portals Internal lookup tables (zip codes, tax tables,…) Etc… Increased Scalability==Reduced Contention for shared resources In Data-Driven applications, data becomes the shared state that limits scalability

6 Scaling Solutions Partitioning Scale out by partitioning the data across multiple servers Route request to the appropriate server Appropriate when: Results can be served from a single partition Appropriate server can be easily identified Replication Caching Normalized view is most appropriate Few results are common, so base data is candidate Base data too costly to invalidate, and must be maintained Rich query support required Web Application Cache Transformations/processing between normalized data in Database and objects in cache Many results (or candidate results) are common Construction of results are not too costly to be invalidated Cached objects can be adequately queried with ADO.NET or application code These types of caching are highly Complementary Partitioning Scale out by partitioning the data across multiple servers Route request to the appropriate server Appropriate when: Results can be served from a single partition Appropriate server can be easily identified Replication Caching Normalized view is most appropriate Few results are common, so base data is candidate Base data too costly to invalidate, and must be maintained Rich query support required Web Application Cache Transformations/processing between normalized data in Database and objects in cache Many results (or candidate results) are common Construction of results are not too costly to be invalidated Cached objects can be adequately queried with ADO.NET or application code These types of caching are highly Complementary

7 Web Caching Benefits Improve Response Time Reduce Code Execution Reduce process hops Reduce managed/unmanaged transitions Reduce Kernel/User mode transitions Reduce database round-trips Eliminate server hops Improve Scalability Database is generally bottleneck Shared state Reduce number of requests to Database Improve Response Time Reduce Code Execution Reduce process hops Reduce managed/unmanaged transitions Reduce Kernel/User mode transitions Reduce database round-trips Eliminate server hops Improve Scalability Database is generally bottleneck Shared state Reduce number of requests to Database

8 Caching In ASP.NET 1.x Object Caching ASP.NET Applications can add/remove objects from cache through dictionary functions or Cache APIs DataSet ds = (DataSet)Cache[ProductID]; if (null == ds) { ds = new DataSet(); SqlDataAdapter adapt = GetDataAdapter(); adapt.Fill(ds); Cache.Insert(ProductID, ds); } Response Caching Page Directives can instruct ASP.NET to cache responses Fragment Caching Page Directives can instruct ASP.NET to cache values from individual controls Entries invalidated based on Time (TTL, absolute time, time unused), File-event, or removal of other entries Object Caching ASP.NET Applications can add/remove objects from cache through dictionary functions or Cache APIs DataSet ds = (DataSet)Cache[ProductID]; if (null == ds) { ds = new DataSet(); SqlDataAdapter adapt = GetDataAdapter(); adapt.Fill(ds); Cache.Insert(ProductID, ds); } Response Caching Page Directives can instruct ASP.NET to cache responses Fragment Caching Page Directives can instruct ASP.NET to cache values from individual controls Entries invalidated based on Time (TTL, absolute time, time unused), File-event, or removal of other entries

9 Caching Dynamic Content Delay in publishing new Content i.e., new Products, Categories Delay in returning new information i.e., from a WebService Inconsistent results across WebFarm Refreshed at different intervals No ability to invalidate i.e., when prices change/invalid information Users not seeing their changes i.e., configuration settings in a Portal Workarounds Coordinated absolute time expiration Data that can accept latency Explicit invalidations Where application controls changes to data Delay in publishing new Content i.e., new Products, Categories Delay in returning new information i.e., from a WebService Inconsistent results across WebFarm Refreshed at different intervals No ability to invalidate i.e., when prices change/invalid information Users not seeing their changes i.e., configuration settings in a Portal Workarounds Coordinated absolute time expiration Data that can accept latency Explicit invalidations Where application controls changes to data

10 Agenda Building a Scalable Enterprise Whidbey SqlNotifications and Caching SQL Server Query Processor Notifications Receiving Notifications Object Caching in ASP.NET Result Caching in ASP.NET Implementing Summary Building a Scalable Enterprise Whidbey SqlNotifications and Caching SQL Server Query Processor Notifications Receiving Notifications Object Caching in ASP.NET Result Caching in ASP.NET Implementing Summary

11 SQL Server QP Notifications User Requests Notifications on Query Sets as command properties Specifies SQL Service Broker queue Specifies message to post on change QP generates a “notification template” Supports common Selects, Stored procs, Batches containing notifiable queries Supports same queries as indexed views Must be deterministic Must not be overly complex Well suited for small number of queries with many parameter values NOT Designed for large number of ad-hoc queries Posts to SQL Service Broker when results change Durable; doesn't require continuous connection Provides activation model for processing message Supported in SQL Server “Yukon” User Requests Notifications on Query Sets as command properties Specifies SQL Service Broker queue Specifies message to post on change QP generates a “notification template” Supports common Selects, Stored procs, Batches containing notifiable queries Supports same queries as indexed views Must be deterministic Must not be overly complex Well suited for small number of queries with many parameter values NOT Designed for large number of ad-hoc queries Posts to SQL Service Broker when results change Durable; doesn't require continuous connection Provides activation model for processing message Supported in SQL Server “Yukon”

12 Using QP Query Notifications Create a Command Create a SqlNotificationRequest Specify message, message queue, timeout Associate the SqlNotificationRequest with one or more commands Execute the command(s) Execution registers for notifications Use ExecuteNonQuery to register without retrieving results Query SSB message queue for your message Poll or blocking “waitfor” query Synchronous or Asynchronous Create a Command Create a SqlNotificationRequest Specify message, message queue, timeout Associate the SqlNotificationRequest with one or more commands Execute the command(s) Execution registers for notifications Use ExecuteNonQuery to register without retrieving results Query SSB message queue for your message Poll or blocking “waitfor” query Synchronous or Asynchronous

13 Application Query Processor Notifications SSB Client Machine Database Server SqlNotificationRequest SqlCommand Template Execute() SqlCommand Execute() Products

14 SqlDependency SqlDependency uses QP Notifications to raise a client side event when results change Registers with a runtime listener upon creation Listens on HTTP or TCP port Can be associated with one or more commands Creates a SqlNotificationRequest under the covers Specifies routing message and default message queue Exposes an OnChanged Event Notification Dispatcher fires notification from database Activated upon delivery of message to queue Uses information in message to determine destination SqlDependency uses QP Notifications to raise a client side event when results change Registers with a runtime listener upon creation Listens on HTTP or TCP port Can be associated with one or more commands Creates a SqlNotificationRequest under the covers Specifies routing message and default message queue Exposes an OnChanged Event Notification Dispatcher fires notification from database Activated upon delivery of message to queue Uses information in message to determine destination

15 Using SqlDependency Create a SqlDependency Register for the OnChanged event Associate SqlDependency with one or more commands Execute the command(s) Event raised when results change Create a SqlDependency Register for the OnChanged event Associate SqlDependency with one or more commands Execute the command(s) Event raised when results change public DataSet getData() { SqlDependency depend = new SqlDependency(); SqlDependency depend = new SqlDependency(); depend.OnChanged += new EventHandler(ChangeEventHandler); depend.OnChanged += new EventHandler(ChangeEventHandler); ds = new DataSet(); ds = new DataSet(); SqlDataAdapter adapt = getDataAdapter(); SqlDataAdapter adapt = getDataAdapter(); depend.AddCommandDependency(adapt.SelectCommand); depend.AddCommandDependency(adapt.SelectCommand); adapt.Fill(ds); adapt.Fill(ds); return ds; return ds;} static void ChangeEventHandler(Object Sender, EventArgs e) { MessageBox.Show("Results Changed"); }

16 Query Processor Notifications Notification Delivery Service SSB Application DependencyListener Client Machine Database Server SqlCommand SqlDependency Template OnChange AddCommandDependency() Execute() Invalidate(ID) SqlNotificationRequest Products

17 SqlDependency

18 Object Caching With Data Invalidations Objects placed in ASP.NET Object Cache can be dependent on database results Use new SqlCacheDependency Supports QP Notifications Pass command into constructor Creates a SqlDependency under the covers Also supports polling for pre-Yukon databases Specify database and tablename in constructor Database and tables must be configured to support listener tables, triggers and stored procedures Caching information added to web server configuration file Object invalidated when any change is made to the dependent table See WS330 for more information Objects placed in ASP.NET Object Cache can be dependent on database results Use new SqlCacheDependency Supports QP Notifications Pass command into constructor Creates a SqlDependency under the covers Also supports polling for pre-Yukon databases Specify database and tablename in constructor Database and tables must be configured to support listener tables, triggers and stored procedures Caching information added to web server configuration file Object invalidated when any change is made to the dependent table See WS330 for more information

19 Using Object Caching With Data Invalidations Generate the object to be cached Associate a SqlCacheDependency with each command (or table) used to generate the object Execute the command(s) to generate the response Add the object to the cache, specifying the SqlCacheDependency Generate the object to be cached Associate a SqlCacheDependency with each command (or table) used to generate the object Execute the command(s) to generate the response Add the object to the cache, specifying the SqlCacheDependency DataSet ds = (DataSet)Cache[ProductID]; if (null==ds) { ds = new DataSet(); SqlDataAdapter adapt = getDataAdapter(); SqlCacheDependency cacheDep = new SqlCacheDependency(adapt.SelectCommand); adapt.Fill(ds); Cache.Insert(ProductID, ds, cacheDep); }

20 Response Caching With Data Invalidations ASP.NET Responses can be dependent on database results Explicitly associate SqlCacheDependency(s) with Response Or Specify as part of caching page directive SqlDependency=“CommandNotification” For query notifications, specify: SqlDependency=“CommandNotification” ASP.NET generates a SqlDependency, associates it with the response, and adds the dependency to the application context Commands automatically enlist in context SqlDependency=“database:tablename” For table dependencies, specify: SqlDependency=“database:tablename” When used with II6.0, responses are pushed to Kernel Mode Response cache! ASP.NET Responses can be dependent on database results Explicitly associate SqlCacheDependency(s) with Response Or Specify as part of caching page directive SqlDependency=“CommandNotification” For query notifications, specify: SqlDependency=“CommandNotification” ASP.NET generates a SqlDependency, associates it with the response, and adds the dependency to the application context Commands automatically enlist in context SqlDependency=“database:tablename” For table dependencies, specify: SqlDependency=“database:tablename” When used with II6.0, responses are pushed to Kernel Mode Response cache!

21 Using Response Caching With Data invalidations Set up the dependency manually Specify that the page is to be cached Generate the page Create a SqlCacheDependency for each command used to generate the response Add the SqlCacheDependency(s) to the Response Set up the dependency manually Specify that the page is to be cached Generate the page Create a SqlCacheDependency for each command used to generate the response Add the SqlCacheDependency(s) to the Response <%@ OutputCache Duration=3600 VaryByParam=“none” SqlDependency=“CommandNotification" %> Response.Cache.SetExpires(DateTime.MaxValue); SqlDataAdapter adapt = getDataAdapter(); SqlCacheDependency cacheDep = new SqlCacheDependency(adapt.SelectCommand); adapt.Fill(ds);Response.AddCacheDependency(cacheDep); Or use the outputcache directive Specify: SqlDependency="CommandNotification" Set Command.NotificationAutoEnlist to false for any commands you don’t want to notify on

22 ASP.NET Application/ WebService <% @ OutputCache Duration=3600 VaryByParam=“none” VaryByParam=“none” SqlDependency= “CommandNotification” %> Response Caching In ASP.NET Notification Delivery Service SSB Http.Sys Middle Tier Database Server SqlCommand Change Detection Execute() Invalidate(ID) Response Cache SqlCommand Execute() IIS Products

23 Web Application/ WebService Data Caching

24 Agenda Building a Scalable Enterprise Whidbey SqlNotifications and Caching Implementing Deciding What to Cache SqlClient Notification Support Summary Building a Scalable Enterprise Whidbey SqlNotifications and Caching Implementing Deciding What to Cache SqlClient Notification Support Summary

25 Deciding What To Cache Data Caching Cache queryable data (i.e., DataSet) in Object Cache Appropriate when some subset of data is commonly useful across requests, objects Object Caching Cache common objects based on results Appropriate when same object may be used by multiple requests Added benefit if object creation/transformations is expensive HTTP Response Caching Specify that entire Response should be cached Appropriate when entire response is common to multiple users Fragment (Partial Page) Caching Specify that some subset of response is to be cached Appropriate when a subset of response is common, but also contains dynamic content Data Caching Cache queryable data (i.e., DataSet) in Object Cache Appropriate when some subset of data is commonly useful across requests, objects Object Caching Cache common objects based on results Appropriate when same object may be used by multiple requests Added benefit if object creation/transformations is expensive HTTP Response Caching Specify that entire Response should be cached Appropriate when entire response is common to multiple users Fragment (Partial Page) Caching Specify that some subset of response is to be cached Appropriate when a subset of response is common, but also contains dynamic content

26 SqlClient Notification Support New SqlNotificationRequest Object Used to specify Message, Message Queue, Timeout New SqlDependency Object AddCommandDependency() Method Sets Notification information on a Command OnChanged Event Raised when any results change Delivery properties Protocol, Port, Timeout,… New SqlCommand Properties New Notification Property Used for setting SqlNotificationRequest object NotificationAutoEnlist Property Allows SqlCommand not to be enlisted in Notification Context New SqlNotificationRequest Object Used to specify Message, Message Queue, Timeout New SqlDependency Object AddCommandDependency() Method Sets Notification information on a Command OnChanged Event Raised when any results change Delivery properties Protocol, Port, Timeout,… New SqlCommand Properties New Notification Property Used for setting SqlNotificationRequest object NotificationAutoEnlist Property Allows SqlCommand not to be enlisted in Notification Context

27 Summary Of Microsoft Web Caching Architecture Developer has a choice! Cache transformed data or objects in application cache Easy to use SqlDependency, ASP.NET Page Directives Explicit control Use Page Directives Enlist commands by default Specify commands that shouldn’t be enlisted Specify dependent tables Support for pre-Yukon Servers Create explicit SqlCacheDependencies Create SqlDependencies Specify Transport, Port, alternate message queue Associate with individual command(s) Use SqlNotificationRequest Specify custom message queue Query queue directly using blocking or unblocking, synchronous or asynchronous queries Developer has a choice! Cache transformed data or objects in application cache Easy to use SqlDependency, ASP.NET Page Directives Explicit control Use Page Directives Enlist commands by default Specify commands that shouldn’t be enlisted Specify dependent tables Support for pre-Yukon Servers Create explicit SqlCacheDependencies Create SqlDependencies Specify Transport, Port, alternate message queue Associate with individual command(s) Use SqlNotificationRequest Specify custom message queue Query queue directly using blocking or unblocking, synchronous or asynchronous queries

28 Questions?

29 SQL Server Resources Week long SQL Server “Ask the Experts” lounge in: Foyer outside Room 309 Support for SQL Server “Yukon” PDC Preview at SQLJunkies http://www.sqljunkies.com/forums SQL Server “Yukon” FAQ Blog at SQLTeam http://yukonblog.sqlteam.com/ Other Key Resources http://www.microsoft.com/sql/community http://msdn.microsoft.com/sqlserver/ 34 world wide user groups, http://msdn.microsoft.com/usergroups/find.asp Our most active SQL Server newsgroups, Microsoft.public.sqlserver.programming Microsoft.public.sqlserver.server Microsoft.public.sqlserver.dts Microsoft.public.sqlserver.olap Microsoft.public.sqlserver.setup Microsoft.public.sqlserver.replication Microsoft.public.sqlserver.msde Week long SQL Server “Ask the Experts” lounge in: Foyer outside Room 309 Support for SQL Server “Yukon” PDC Preview at SQLJunkies http://www.sqljunkies.com/forums SQL Server “Yukon” FAQ Blog at SQLTeam http://yukonblog.sqlteam.com/ Other Key Resources http://www.microsoft.com/sql/community http://msdn.microsoft.com/sqlserver/ 34 world wide user groups, http://msdn.microsoft.com/usergroups/find.asp Our most active SQL Server newsgroups, Microsoft.public.sqlserver.programming Microsoft.public.sqlserver.server Microsoft.public.sqlserver.dts Microsoft.public.sqlserver.olap Microsoft.public.sqlserver.setup Microsoft.public.sqlserver.replication Microsoft.public.sqlserver.msde

30 © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

31


Download ppt "SQL Server Notifications: Invalidating Cached Results Michael Pizzo Software Architect Microsoft Corporation Michael Pizzo Software."

Similar presentations


Ads by Google