ENTERPRISE

Windows Azure : Static reference data (part 2) - Performance disadvantages of a chatty interface & Caching static data

2/21/2011 8:55:59 AM

3. Performance disadvantages of a chatty interface

In the previous sections, we’ve discussed how you could store static data, such as shirt sizes and materials, in SQL Azure or the Table service. We’ll now look at the call sequences you’d need to make to render the web page (shown previously in figure 1) and discuss the pros and cons of each approach.

Synchronous Calls

To retrieve all the data required to display the product details page shown in figure 1, you’d need to make at least four calls to the storage provider:

  • Retrieve the product details

  • Retrieve the list of size types

  • Retrieve the list of materials

  • Retrieve the list of personage types (men, ladies, and so on)

When developing an ASP.NET web page, you should consider making asynchronous calls to improve performance, but most developers will typically write synchronous calls to retrieve the data. Figure 3 shows the synchronous call sequence for the product details web page.

Figure 3. Synchronous call sequence; each result must be returned before you can make the next call


As you can see, the synchronous nature of the page means you have to wait until you receive data back from the data provider (SQL Azure or the Table service) before you can process the next section of the page. Due to the blocking nature of synchronous calls and the latency involved in cross-server communication, the rendering of this page will be much slower than it needs to be.

Asynchronous Calls

Because size types, materials, and personage types are sets of data that are both independent of each other and independent of the returned product details, you could use asynchronous calls instead. Retrieving the data from the Table service asynchronously means you don’t have to wait for one set of data to be returned before retrieving the next set.

Figure 4 shows the same call sequence as in figure 3, but this time using asynchronous calls.

Figure 4. Asynchronous call sequence


As you can see in figure 4, you no longer have to wait for data to be returned before you process the next statement. Once all the data is returned, you can render the page.

Tip

Here we’ve used static data calls as our example. You can, however, use asynchronous calls whenever you don’t have any relationship between sets of data rendered on a page.


Now that you understand how you can store and retrieve static data, let’s take a look at how you can improve performance (and reduce the hosting bill) by using cached data instead.

4. Caching static data

Regardless of your chosen storage platform (SQL Azure or the Table service), you should consider caching static data rather than continually retrieving the same data from a table. This will bring large performance and cost benefits.

Because static reference data hardly ever changes and is usually a pretty small set of data, the in-process memory cache (such as the default ASP.NET cache) is a suitable caching choice. You can always use a cache dependency to keep the cache and the underlying backing store synchronized.

Let’s now take a look at how you can use the cache in the Hawaiian Shirt Shop website.

Populating the Cache

For frequently accessed static data, you should probably populate the web role cache when the application starts up. The following code, placed in the Global.asax file, will do this:

protected void Application_Start(object sender, EventArgs e)
{
var sizeTypeContext = new SizeTypeContext();
HttpRuntime.Cache["sizeTypes"] = sizeTypeContext.SizeTypeTable;
}

In this example we’ve populated the cache with data from the Table service, but the technique is the same for using SQL Azure. We’re using a cache because we’re working with static data, and we don’t want to hit the data source too often. You might want to consider populating the caching when your role instance starts, instead of when the ASP.NET application starts.

Populating the Drop-Down Lists

Now that you have your data in the cache, let’s take a look at how you can populate the drop-down lists with that data:

sizeDropDown.DataSource = (IEnumerable<SizeType>)Cache["sizeTypes" ];
sizeDropDown.DataTextField = "RowKey";
sizeDropDown.DataValueField = "SizeCode";
sizeDropDown.DataBind();

As you can see from this code, you no longer need to return to the data store to populate the drop-down list—you can use the cache directly.

Because the cache will be scavenged when memory is scarce, you can give static data higher priority than other cache items by using the cache priority mechanism (meaning that other items will be scavenged first):

var sizeTypeContext = new SizeTypeContext();
HttpContext.Current.Cache.Insert("sizeTypes", sizeTypeContext.SizeTypeTable, null, new DateTime(2019, 1, 1), Cache.NoSlidingExpiration, CacheItemPriority.High, null);


In the preceding code, the SizeTypes list will be stored in the cache with a High priority, and it will have an absolute expiration date of January 1, 2019. If the web role is restarted, the cache will be flushed, but if the process remains running, the data should remain in memory until that date.

If the static data might change in the future, you can set a cache dependency to keep the cache synchronized or manually restart the role when updating the data.

Protecting Your Code from an Empty Cache

Because cache data is volatile, you might wish to prevent the cached data from being flushed by checking that the data is cached prior to populating the drop-down list:

private IEnumerable<SizeType> GetSizeTypes()
{
if (Cache["sizeTypes"] == null)
{
var sizeTypeContext = new SizeTypeContext();
Cache["sizeTypes"] = sizeTypeContext.SizeTypeTable;
}
return (IEnumberable<SizeType>)Cache["sizeTypes"];
}

sizeDropDown.DataSource = GetSizeTypes();
sizeDropDown.DataTextField = "RowKey";
sizeDropDown.DataValueField = "SizeCode";
sizeDropDown.DataBind();

In this code, before the drop-down list is populated, a check is run to make sure that the data is already stored in the cache. If the data isn’t held in cache, it repopulates that cache item.

By effectively caching static data, you can both reduce your hosting bill and improve the performance of your application. By using an in-memory cache for static data on the product details page, you now have one data storage call per application start up rather than four.

Using in-memory cache for static data also means that your presentation layer no longer needs to consider where the underlying data is stored.

Please be aware that the examples in this section aren’t production-level code and have been simplified to illustrate the concepts. If you’re implementing such a solution, you should also take the following guidelines into consideration:

  • Abstract your caching code into a separate caching layer

  • Don’t use magic strings (such as Cache["sizeTypes"])—use constants instead

  • Use cache dependencies

  • Prioritize your cache properly

  • Check that your cache is populated prior to returning data

  • Handle exceptions effectively

Other  
  •  Performing Granular Backup Using the SharePoint Central Administration
  •  Using SharePoint Central Administration for Backup and Restore
  •  Backing Up and Restoring a SharePoint Environment : Using the Recycle Bin for Recovery
  •  Using Non-Windows Systems to Access Exchange Server 2010 : Understanding Other Non-Windows Client Access Methods
  •  Using Non-Windows Systems to Access Exchange Server 2010 : Remote Desktop Connection Client for Mac
  •  Using Non-Windows Systems to Access Exchange Server 2010 : Configuring and Implementing Entourage for the Mac
  •  Using Non-Windows Systems to Access Exchange Server 2010 : Mac Mail, iCal, and Address Book
  •  Parallel Programming with Microsoft .Net : Futures - Variations
  •  Parallel Programming with Microsoft .Net : Futures - Example: The Adatum Financial Dashboard
  •  Parallel Programming with Microsoft .Net : Futures - The Basics
  •  Using Non-Windows Systems to Access Exchange Server 2010 : Outlook Express
  •  Using Non-Windows Systems to Access Exchange Server 2010 : Understanding Non-Windows–Based Mail Client Options
  •  Deploying the Client for Microsoft Exchange Server 2010 : Deploying with Microsoft System Center Configuration Manager 2007
  •  Deploying the Client for Microsoft Exchange Server 2010 : Pushing Outlook Client Software with Group Policies
  •  Deploying the Client for Microsoft Exchange Server 2010 : Installing the Outlook Client for Exchange Server
  •  Deploying the Client for Microsoft Exchange Server 2010 : Preparing the Deployment
  •  Parallel Programming with Microsoft .Net : Parallel Aggregation - Design Notes
  •  Parallel Programming with Microsoft .Net : Parallel Aggregation - Variations
  •  Leveraging and Optimizing Search in SharePoint 2010 : Uninstalling FAST Search Server 2010 for SharePoint
  •  Leveraging and Optimizing Search in SharePoint 2010 : Customizing the FAST Search User Interface
  •  
    Most View
    UML Essentials - UML at a Glance
    Canon EOS-1 DX (Part 1)
    AOC E2262VW - The Showy And Unique
    Green Gaming Goblin Exposed the Clutches
    SharePoint 2010 : Creating and Managing Workflows - Workflows in SharePoint 2010
    Strip HTML of Tags
    Windows Mobile Security - Permissions and User Controls
    AMD Radeon HD 7950 3GB vs. Nvidia GeForce GTX 660 Ti 2GB vs. Nvidia GeForce GTX 670 2GB (Part 2)
    10 Simple Steps To Glamour
    ASuS PadFone 2 - Two Times Is A Charm For This Phone-In-Tablet Combo
    Top 10
    ASP.NET 4 in VB 2010 : The Data Controls - Sorting and Paging the GridView
    Microsoft Content Management Server Development : A Date-Time Picker Placeholder Control (part 2)
    Microsoft Content Management Server Development : A Date-Time Picker Placeholder Control (part 1)
    Microsoft Content Management Server Development : Building SharePoint Web Parts - Configuring the Web Part, Debugging the Web Part
    Windows Server 2008 R2 networking : Planning and Deploying DNS (part 4) - Monitoring and troubleshooting DNS
    Windows Server 2008 R2 networking : Planning and Deploying DNS (part 3) - Setting up DNS zones
    Windows Server 2008 R2 networking : Planning and Deploying DNS (part 2) - Installing the DNS Server role, Configuring DNS Servers
    Windows Server 2008 R2 networking : Planning and Deploying DNS (part 1) - Designing a DNS infrastructure
    Windows Server 2008 R2 networking : Routing and Remote Access
    ADO.NET Programming : Microsoft SQL Server (part 4) - Working with Typed Data Sets