Migrating from Azure Caching service to Dedicate Cache

1. Introduction:

This post is mainly targeted at helping users in migrating their existing cloud project that run with Azure caching service to dedicate Cache. To get an introduction on what’s new with dedicated cache please visit Distributed Cache section in Scottgu’s blog post – here

Two things to do before starting

1. First thing to do before starting the migration plan is to decide which of the two deployment modes would you like to use – either Enable cache in one of your existing roles which makes cache to co-exist with your app (let’s call this is Co-located) or host cache in a separate role (let’s call this Dedicated Role for purpose of our discussion).

2. Install/Upgrade to latest 1.7 SDK.

2. Enabling and using Cache in different deployment modes

OK. Now you have decided on which dedicated model to use. This section talks about enabling and using cache in different modes.

2.1 Setting up the cache service:

Open Visual Studio. Open your existing cache cloud project, under the “Roles” folder, right click on a role select its properties.

Here you can find a new “Caching” Tab (This would show up only if 1.7 SDK is installed). It looks like below.

In co-located role model, enable cache for your role of interest by selecting “Enable Caching” in the “Caching” tab. Once you have enabled cache, you can choose what percentage of memory on your role can be used by cache by choosing “Co-located role” option and accordingly adjusting the size as per your requirements.

In order to have a dedicated role, right click on the “Roles” section -> Add -> “New Worker Role”. Choose a “Cache Worker Role” to add to the project. In the newly added Cache Worker Role’s properties, go to Caching tab and you can observe “Dedicate Role” option is selected.

Note: Before you deploy your application to Azure, specify Storage account credentials. Clicking on the button to specify storage account brings up this dialog box where you can specify the storage account where cache runtime information is logged.

2.2 Creating caches:

When you enable caching, by default you will have a “default” cache created. New named caches can be created quickly in the Visual Studio IDE. To create named caches, go to the “Caching” Tab (as mentioned in the previous section), under the “Named Cache Settings” click on “Add Named Cache” to add a cache, clicking on “Remove Named Cache” would remove a selected cache from the list. Cache settings are defined at the end of the section.

2.3 Consuming the cache from client application

Now let’s look into what changes would be required to your existing application’s client configuration to use dedicated cache models. A pre-requisite for the next steps is to install nuget

1. Firstly remove the following dlls from your client role references

–        Microsoft.ApplicationServer.Caching.Client
–        Microsoft.ApplicationServer.Caching.Core
–        Microsoft.Web.DistributedCache
–        Microsoft.WindowsFabric.Common
–        Microsoft.WindowsFabric.Data.Common

2. Right click on cache client role, select “Manage Nuget Packages”. Search for “Windows Azure Caching Preview” package and Install the package. This would add all the necessary dlls to your references.

3. If the client configuration is in app.config, below changes are required.

This is what is currently present in your app.config.

<configSections>
<section name=”dataCacheClient type=”Microsoft.ApplicationServer.Caching.DataCacheClientSection, Microsoft.ApplicationServer.Caching.Core” allowLocation=”true” allowDefinition=”Everywhere”/>
</configSections>
<dataCacheClient>
<hosts>
<host name=”cacheName.cache.windows.net” cachePort=”22233″ />
</hosts>
<securityProperties mode=”Message”>
<messageSecurity authorizationInfo=”ACSToken”>
</messageSecurity>
</securityProperties>
</dataCacheClient>

This is what you need to update to

<configSections>
<section name=”dataCacheClients” type=”Microsoft.ApplicationServer.Caching.DataCacheClientsSection, Microsoft.ApplicationServer.Caching.Core” allowLocation=”true” allowDefinition=”Everywhere” />
</configSections> 
<dataCacheClients>
<tracing sinkType=”DiagnosticSink” traceLevel=”Error” />
<dataCacheClient name=”default”>
<autoDiscover isEnabled=”true” identifier=”[cache cluster role name]” />
</dataCacheClient>
</dataCacheClients>

Notice the difference in the above configurations. The words that are bolded above indicate that the section name in configSection and the data cache client(s) tag name should be same.

You would no longer need <hosts> and <securityProperties>.Instead of <hosts> you need to mention an identifier, which is the role name in which the cache enabled.

You wouldn’t need any security settings here because your application and cache service are in the same deployment and hence secured.

4. If client configuration is programmatic, do the below changes

Your existing code:

string hostName = “[Cache endpoint without port]”;
int cachePort;
cachePort = sslEnabled ? 22243 : 22233; // Default port
List<DataCacheServerEndpoint> server = new List<DataCacheServerEndpoint>();
server.Add(new DataCacheServerEndpoint(hostName, cachePort));
DataCacheFactoryConfiguration config = new DataCacheFactoryConfiguration();
string authenticationToken = “[InsertAuthenticationTokenHere]”;
SecureString secureAuthenticationToken = GetSecureString(authenticationToken);
config.SecurityProperties = new DataCacheSecurity(secureAuthenticationToken, sslEnabled);
config.Servers = server;
DataCacheFactory myCacheFactory = new DataCacheFactory(config);
myDefaultCache = myCacheFactory.GetDefaultCache();

Below is what you need to update to:

To continue with Default cache:

Co-located mode:

DataCacheFactoryConfiguration cfg = new DataCacheFactoryConfiguration();
cfg.AutoDiscoverProperty = new DataCacheAutoDiscoverProperty(true);
DataCacheFactory dcf = new DataCacheFactory(cfg);
DataCache dc = dcf.GetDefaultCache();

Dedicated mode:

DataCacheFactoryConfiguration cfg = new DataCacheFactoryConfiguration();
cfg.AutoDiscoverProperty = new DataCacheAutoDiscoverProperty(true,”[cache role name]”);
DataCacheFactory dcf = new DataCacheFactory(cfg);
DataCache dc = dcf.GetDefaultCache();

In order to use named caches, everything remains the same except DataCache object initialization which would be:

DataCache dc = dcf.GetCache(“[namedCache]”);

As mentioned earlier, you can observe that DataCacheServerEndpoint and DataCacheSecurity are not required in dedicated Cache, instead of these we used AutoDiscoveryProperty.

2.4 Enabling Diagnostics for the cache service:

Cache Diagnostics, enabling crash dumps and performance counters for cache service would be discussed in a different article that talk about troubleshooting tips and tricks.

2.5 Cache Settings description:

Name – indicates the cache name

Backup copies – is to enable High Availability which would create a copy of the data on the number of nodes specified

Notifications – will enable notifications for this cache

Eviction Policy – specifies the policy to use when you want to evict certain objects from cache

Time To Live – default time for an object to live in cache

Expiration Type – indicated the expiration type for an object in cache.

3. Advanced cache configuration Options:

Enabling Local cache:

–        with  Notifications:

  • The local cache items get invalidated using notification based mechanism. In order to have notifications support, the cache should be enabled with notifications while creating a named as mentioned in the earlier section.

<dataCacheClient name=”default”>
<localCache isEnabled=”true” sync=”NotificationBased” objectCount=”100000″ ttlValue=”300″ />
</dataCacheClient>

–        with TTL:

  • The local cache items get invalidated using TTL based mechanism here

<dataCacheClient name=”default”>
<localCache isEnabled=”true” sync=”TimeoutBased” objectCount=”100000″ ttlValue=”300″ />
</dataCacheClient>

4. Simulation in Development Fabric

Development fabric (Dev fabric) can be used for simulation before deploying to Azure. Start debugging your project in Visual Studio for a Dev fabric run. The cache server process for Dev fabric is emulated on the machine in this case.

To exit Dev fabric gracefully, use “Exit” from its menu options or from system tray instead of killing any process.

5. Deploy Application to Azure

Now you are ready to deploy your application to Azure. Right click on the cloud project, you can find option to either “Publish” the project to one of your slots or create a “Package” and upload the .cspkg and .cscfg from the Azure portal.

Windows Azure Caching: New Capabilities

This talk by Shyam Seshadri, present you the new capabilities in Windows Azure Caching.

Overview : Windows Azure Caching is a simple to use distributed cache that can help make your applications perform faster and be more scalable. In this session we will quickly get you up to speed on Caching and show you what it can do and how it works. The majority of the session will focus on new capabilities we are delivering in the next release – Windows Azure Caching (Preview) – that redefines caching on Windows Azure. This new release brings additional features, performance improvements and new deployment options that result in a significantly improved and flexible caching model for Windows Azure.

http://channel9.msdn.com/Events/TechEd/NorthAmerica/2012/AZR309/player?w=960&h=544