Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sessions stored in a co-located Azure cache gets out of sync on multiple instances

For my MVC4 application, run in Azure, I store the sessions in a co-located cache. As described in this How-to that Microsoft provide.

I run two small instances, and everything seems to work fine. I can log in to the application, and I remain logged in when I browse around within the application. So the session seem to work on both instances.

However, when I update the session data something like this:

HttpContext.Current.Session["someVar"] = "new value";

That change only seem to have an effect on the instance that handle that particular request. Now as I browse around the application, sometimes I get the initial value and sometimes I get the updated value.

I haven't made any changes to the web.config, so it looks exactly as it do when it gets added by the Nuget package:

<sessionState mode="Custom" customProvider="AppFabricCacheSessionStoreProvider">
  <providers>
    <add name="AppFabricCacheSessionStoreProvider"
          type="Microsoft.Web.DistributedCache.DistributedCacheSessionStateStoreProvider, Microsoft.Web.DistributedCache"
          cacheName="default"
          useBlobMode="true"
          dataCacheClientName="default" />
  </providers>
</sessionState>

Do I need to handle sessions in another way when using the Azure cache, or is it something else I'm missing here?

like image 984
Christofer Eliasson Avatar asked Sep 11 '12 10:09

Christofer Eliasson


1 Answers

You need to assign an applicationName so that the distributed cache can view the shared state within the same application boundary. See MSDN forum post for reference.

<add name="AppFabricCacheSessionStoreProvider"
          type="Microsoft.Web.DistributedCache.DistributedCacheSessionStateStoreProvider, Microsoft.Web.DistributedCache"
          applicationName="azureMVC4App"
          cacheName="default"
          useBlobMode="true"
          dataCacheClientName="default" />

If you want to share cache state across application boundaries, you need to assign sharedId

like image 152
SliverNinja - MSFT Avatar answered Oct 31 '22 05:10

SliverNinja - MSFT