Windows Azure Caching: Difference between revisions

Content deleted Content added
Rothja (talk | contribs)
Added code examples and the table at the end with related caching technologies.
Removing link(s) to "AppFabric Caching": Removing links to deleted page AppFabric Caching.
 
(34 intermediate revisions by 14 users not shown)
Line 1:
an in-memory, distributed caching feature designed for Microsoft Azure applications. Caching was available as a part of the Windows Azure SDK. The Azure Managed Cache and In-Role Cache services were retired, and Microsoft recommended migration to Azure Redis Cache.
{{Userspace draft}}
'''Windows Azure Caching''' is an in-memory, [[Distributed cache|distributed caching]] feature designed for [[Windows Azure]] applications<ref name=CachingMain>{{cite web|title=Caching in Windows Azure|url=http://msdn.microsoft.com/en-us/library/gg278356.aspx|publisher=Microsoft|accessdate=12 February 2013}}</ref>. Caching is currently available as a part of the Windows Azure SDK.
<ref name="AzureMain">{{cite web|url=http://msdn.microsoft.com/en-us/library/dd163896.aspx|work=MSDN Library|publisher=Microsoft|title=Windows Azure Overview and Feature Areas|accessdate=12 February 2013}}</ref>
 
==Architecture==
Windows Azure Caching allows a cloud service to host Caching on a Windows Azure role.<ref name="CachingMain">{{cite web |title=Caching in Windows Azure |url=http://msdn.microsoft.com/en-us/library/gg278356.aspx |accessdate=12 February 2013 |work=MSDN Library |publisher=Microsoft}}</ref> The cache is distributed across all running instances of that role. Therefore, the amount of available memory in the cache is determined by the number of running instances of the role that hosts Caching and the amount of physical memory reserved for Caching on each instance.<ref name=CachingCapacityPlanning>{{cite web|title=Capacity Planning Considerations for Windows Azure Caching|url=http://msdn.microsoft.com/en-us/library/hh914129.aspx|work=MSDN Library|publisher=Microsoft|accessdate=13 February 2013}}</ref>
 
There are two deployment topologies for Caching:
* ''Dedicated''<ref name="AzureMain"CachingDedicated>{{cite web|title=Windows Azure Caching on Dedicated Roles|url=http://msdn.microsoft.com/en-us/library/dd163896hh914140.aspx|work=MSDN Library|publisher=Microsoft|title=Windows Azure Overview and Feature Areas|accessdate=1213 February 2013}}</ref>
* Dedicated
* ''Co-located''<ref name=CachingColocated>{{cite web|title=Windows Azure Caching on Existing Roles|url=http://msdn.microsoft.com/en-us/library/hh914128.aspx|work=MSDN Library|publisher=Microsoft|accessdate=13 February 2013}}</ref>
* Co-located
 
===Dedicated Topologytopology===
In the dedicated topology, you define a worker role that is dedicated to Caching. This means that all of the worker role's available memory is used for the Caching and operating overhead.
 
The following diagram shows Caching in a dedicated topology. The cloud service shown has three roles: Web1, Worker1, and Cache1. There are two running instances of each role. TheIn this example, the cache is distributed across all instances of the dedicated Cache1 role.
 
[[File:Windows Azure Caching (Dedicated).jpg|Type=frame|Location=none|300px|Represents a running Windows Azure cloud service that uses Caching with a dedicated topology.]]
 
A dedicated topology has the advantage of scaling the caching tier independently of any other role in the cloud service.<ref Itname=CachingCacheCluster>{{cite alsoweb|title=About Windows Azure Caching, Cache Cluster|url=http://msdn.microsoft.com/en-us/library/hh914161.aspx#Concept_CacheClusters|work=MSDN Library|publisher=Microsoft|accessdate=13 February 2013}}</ref> For the best Caching performance, a dedicated topology is recommended forbecause cachesthe role instances do not share their resources with higherother capacityapplication orcode throughputand requirementsservices.<ref name=CachingDedicated/>
 
===Co-located Topologytopology===
In a co-located topology, you use a percentage of available memory on existing web or worker roles for Caching.<ref name=CachingColocated/>
 
The following diagram shows Caching in a co-located topology. The cloud service has two roles: Web1 and Worker1. There are two running instances of each role. TheIn this example, the cache is distributed across all instances of the Web1 role. Because this role also hosts the web front-end for the cloud service, the cache is configured to use only a percentage of the physical memory on each instance of the Web1 role.
 
[[File:Windows Azure Caching (Co-located).jpg|Type=frame|Location=none|250px|Represents a running Windows Azure cloud service that uses Caching with a co-located topology.]]
 
A co-located cache is a cost-effective way to make use of existing memory on a role within a cloud service.<ref name=CachingColocated/>
 
==Examples==
The following sections show Windows Azure Caching configuration and code examples.
 
===Configuration Exampleexample===
In [[Visual Studio]], Caching on roles is configured in the Caching tab of the properties of the role that hosts Caching.<ref name=CachingHowToGuide>{{cite web|title=How to Use Windows Azure Caching|url=http://www.windowsazure.com/en-us/develop/net/how-to-guides/cache/|work=Windows Azure Website|publisher=Microsoft|accessdate=13 February 2013}}</ref> This makes underlying changes to the ServiceConfiguration.cscfg file.<ref name=CachingCsCfg>{{cite web|title=Windows Azure Caching Role Configuration Settings (ServiceConfiguration.cscfg)|url=http://msdn.microsoft.com/en-us/library/jj658975.aspx|work=MSDN Library|publisher=Microsoft|accessdate=13 February 2013}}</ref> These settings determine the topology used (dedicated or co-located) and the number of named caches<ref name=NamedCaches>{{cite web|title=About Windows Azure Caching, Named Caches|url=http://msdn.microsoft.com/en-us/library/hh914161.aspx#Concept_NamedCaches|work=MSDN Library|publisher=Microsoft|accessdate=13 February 2013}}</ref> and their settings.
 
Other roles must be configured to use Caching.<ref name=CacheClientConfig>{{cite web|title=Getting Started with Development for Windows Azure Caching, Configure the Clients|url=http://msdn.microsoft.com/en-us/library/hh914150.aspx|work=MSDN Library|publisher=Microsoft|accessdate=13 February 2013}}</ref> One way to do this is with a [[NuGet]] package. This includes modifying the web.config to contain a properly configured dataCacheClients<ref name=dataCacheClients>{{cite web|title=Windows Azure Caching Client Configuration Settings (Web.config), dataCacheClients|url=http://msdn.microsoft.com/en-us/library/jj658973.aspx#dataCacheClients|work=MSDN Library|publisher=Microsoft|accessdate=13 February 2013}}</ref> section. The following example dataCacheClients section specifies that the role that hosts Caching is named “CacheWorker1”.
 
<syntaxhighlight lang=XML>
Line 44 ⟶ 42:
</syntaxhighlight>
 
===Code Exampleexamples===
''Note that the code samples in this section are shown in [[C Sharp (programming language)|C#]].''
When hosting Caching on roles, the DataCache constructor can be used to specify both the named cache and the dataCacheClient section for the cache client settings. The following code shows these variations.
 
When hosting Caching on roles, the '''DataCache''' class constructor can be used to specify both the named cache and the '''dataCacheClient''' section for the cache client settings. The following code shows thesehow to create a named cache, ''NamedCache2'', using the settings from a '''dataCacheClient''' section named variations''customClient''.
<syntaxhighlight lang=CSharp>
 
DataCache cache = new DataCache();
<syntaxhighlight lang=CSharp"csharp">
cache = new DataCache("default");
var cache = new DataCache("NamedCache1NamedCache2", "defaultcustomClient");
cache = new DataCache("NamedCache2", "customClient");
</syntaxhighlight>
 
The following method shows how to use the ''Cache'' object to retrieve data from the cache. In this example, a user identifier (''userid'') is the key for the associated user information object. The code first attempts to get this user information from the cache using the ''userid'' key. If that does not succeed, the code retrieves the information with a database query and then stores the returned user data in the cache. The next time the same code is run, the user information will be returned from the cache rather than the database. This assumes that the cached data has not been expired or evicted.
 
<syntaxhighlight lang=CSharp"csharp">
dataType GetUserData(string userId)
{
dataType data = null;
 
// Attempt to retrieve the user data from the cache:
object dataObject = Cache.Get(userId);
 
if (dataObject != null)
data = (dataType)cache.Get(userid) dataObject;
else
{
// If it doesn't exist in the cache, retrieve it from the database:
data = db_selectGetUserDataFromDatabase("SELECT * FROM users WHERE useriduserId = ?@userId", useriduserId);
 
// Put the returned data in the cache for future requests:
<syntaxhighlight lang=CSharp>
Cache.Add(useriduserId, data);
function get_foo(int userid) {
/* first try the cache */
data = (dataType)cache.Get(userid) ;
if (!data) {
/* not found : request database */
data = db_select("SELECT * FROM users WHERE userid = ?", userid);
/* then store in cache until next get */
Cache.Add(userid, data);
}
 
return data;
}
</syntaxhighlight>
 
The following method shows how to update data that is already in the cache.
 
<syntaxhighlight lang=CSharp"csharp">
void UpdateUserData(string userId, dataType data)
function update_foo(int userid, string dbUpdateString) {
{
/* first update database */
// Update the user information in the database:
result = db_execute(dbUpdateString);
result = UpdateUserDataInDatabase(userId, data);
if (result) {
/* database update successful : fetch data to be stored in cache */
if (!dataresult) {
data = db_select("SELECT * FROM users WHERE userid = ?", userid);
{
/* the previous line could also look like data = createDataFromDBString(dbUpdateString); */
/*/ thenIf storesuccessfully inupdated, cacheupdate untilthe next get */cache:
Cache.Put(useriduserId, data);
}
}
Line 88 ⟶ 94:
The following call removes the item from the cache.
 
<syntaxhighlight lang=CSharp"csharp">
Cache.Remove(useriduserId);
</syntaxhighlight>
 
==Shared Caching==
Windows Azure Shared Caching provides caching as a managed service.<ref name=SharedCaching>{{cite web|title=About Windows Azure Shared Caching|url=http://msdn.microsoft.com/en-us/library/hh697519.aspx|work=MSDN Library|publisher=Microsoft|accessdate=13 February 2013}}</ref> Unlike co-located or dedicated topologies, the cache is not hosted on Windows Azure roles in a single cloud service deployment. Instead, the cache is provided as a multitenant service with usage quotas.<ref name=SharedCachingQuotas>{{cite web|title=Understanding Quotas for Windows Azure Shared Caching|url=http://msdn.microsoft.com/en-us/library/gg185683.aspx|work=MSDN Library|publisher=Microsoft|accessdate=13 February 2013}}</ref> The service is divided into tiers that range from 128 MB to 4 GB.<ref name=SharedCachingFAQ>{{cite web|title=Windows Azure Shared Caching FAQ|url=http://msdn.microsoft.com/en-us/library/hh697522.aspx|work=MSDN Library|publisher=Microsoft|accessdate=13 February 2013}}</ref> In addition to storage capacity, each tier provides increasing processor and network capabilities.<ref name=SharedCachingFAQ/> Shared Caching provides a way for multiple cloud services to access the same cache.
 
==History==
Windows Azure Caching has its roots in an on-premises technology, [[AppFabric]]. It was originally released as one of several Windows Azure AppFabric services, but the AppFabric designation in Windows Azure has since been abandoned. Many of the assembly names, namespaces, and APIs are identical between theWindows twoAzure productsCaching and AppFabric Caching.<ref name=CachingOnPremisesAndCloud>{{cite web|title=Differences Between Caching On-Premises and in the Cloud|url=http://msdn.microsoft.com/en-us/library/gg185678.aspx|work=MSDN Library|publisher=Microsoft|accessdate=13 February 2013}}</ref> The first release of Caching for Windows Azure in April 2011 provided caching as a managed service in Windows Azure.<ref name=Caching2011Release>{{cite web|title=Introducing the Windows Azure Caching Service|url=http://msdn.microsoft.com/en-us/magazine/gg983488.aspx|work=MSDN Magazine|publisher=Microsoft|accessdate=13 February 2013}}</ref> This offering is now called Shared Caching.
 
In October 2012, support was added for hosting Caching on roles within a cloud service deployment.<ref name=CachingRelNotes>{{cite web|title=Windows Azure Caching Release Notes (October 2012)|url=http://msdn.microsoft.com/library/jj651667.aspx|work=MSDN Library|publisher=Microsoft|accessdate=13 February 2013}}</ref> This is now called WindowWindows Azure Caching.
 
==Related Cachingcaching Technologiestechnologies==
Windows Azure Caching is related to other Microsoft caching technologies. These technologies share similar features, such as the assembly name, namespace, and types.<ref name=CachingOnPremisesAndCloud/> However, there are some differences. The table below describes these technologies.
 
{| class="wikitable"
Line 107 ⟶ 113:
! Caching Technology !! Target !! Installed By !! Description
|-
| [[AppFabric_Caching_Service|AppFabric Caching]] || [[On-premises_softwarepremises software|On-Premises]] || [[AppFabric]] || Distributed on-premises cache that uses servers that the user provisions and manages.
|-
| [[Windows Azure Caching#Architecture|Windows Azure Caching]] || [[Cloud_computingCloud computing|Cloud]] || [http://www.windowsazure.com/en-us/develop/net/#/ Windows Azure SDK] || Caching is distributed across the instances of a single role in a Windows Azure cloud service deployment.
|-
| [[Windows Azure Caching#Shared Caching|Windows Azure Shared Caching]] || [[Cloud_computingCloud computing|Cloud]] || [http://www.windowsazure.com/en-us/develop/net/#/ Windows Azure SDK] || Caching is provided through several tiers as a multitenant service for use by Windows Azure cloud applicationsservices.
|}
 
==References==
{{Reflist}}
 
==External links==
* [http://msdn.microsoft.com/en-us/library/gg278356.aspx Windows Azure Caching Documentation]
* [http://www.windowsazure.com/en-us/develop/overview/ Windows Azure Developer Center]
* [http://www.windowsazure.com/en-us/develop/net/how-to-guides/cache/ How to Guide for Windows Azure Caching]
* [http://msdn.microsoft.com/en-us/library/jj189876.aspx Windows Azure Caching Samples]
* [http://msdn.microsoft.com/en-us/library/windowsazure/microsoft.applicationserver.caching Windows Azure Caching Class Library Reference]
 
{{Microsoft Azure Services Platform}}
 
[[Category:Microsoft cloud services]]
[[Category:Database caching]]
[[Category:Cache (computing)]]