Note this is the older 1.0 version of the Session Provider. Please see here for a post regarding the new and improved 2.0 version.

One question I’ll often ask developers on a phone screen for an ASP.NET developer position is to discuss the different options for managing session state in an ASP.NET application.  I like this question because it gives me quick insight into what types of applications that developer has been working on and what role that developer has played on his or her team.

The memorization part of the question is clearly to know the different out-of-the-box providers.  Developers who work on smaller sites or who don’t have a production support role often know about in-process session state, but are unfamiliar with the out-of-process providers and the problems they solve.

Over the years, I’ve found that a state server was often sufficient for a reasonably high traffic site.  But that has a single, and obvious, point of failure.  Managing session state with SQL Server is durable, but clearly it’s going to be slower.  I’ve never been comfortable with increasing disk I/O on the database server for session management.

The right answer seems to be to use a backing store that isn’t I/O bound and doesn’t depend on the uptime of a single server.  That description is one that applies to Couchbase Server.  So if you want the speed of using an in-memory session state server with the durability of a SQL Server instance, you now have the option of using the Couchbase ASP.NET session state provider.

Freshly posted to the CouchbaseLabs projects on GitHub, is a port of the former Enyim Memcached session state provider.  There’s also a sample application included to get you up and running quickly.

Using the new provider is very simple.  Assuming you have your cluster configured, all you need to do is update your web.config with the settings for the Couchbase .NET Client Library and update the sessionState config section.

<section name=“couchbase” type=“Couchbase.Configuration.CouchbaseClientSection, Couchbase”/>
<couchbase>
<servers bucket=“default” bucketPassword=“”>
<add uri=“http://127.0.0.1:8091/pools/default”/>
>
>
<sessionState customProvider=“Couchbase” mode=“Custom”>
<providers>
<add name=“Couchbase” type=“Couchbase.AspNet.SessionState.CouchbaseSessionStateProvider, Couchbase.AspNet” />
>
>

Once these settings are in place, simply use the Session object (or ViewData, ViewBag and TempData in MVC) as you always have.

Session[“Message”] = “Couchbase is awesome!”;

Don’t forget that you’ll need to mark your own classes as Serializable in order to persist them into Couchbase Server.

[Serializable]
public class SessionUser
{
public string Username { get; set; }

public string Email { get; set; }
}

Please note that this provider is included as part of Couchbase Labs and is not an officially supported product.  You’re free to use the code however you’d like as it’s licensed under Apache License 2.0.

Posted by John Zablocki, NET. SDK Developer, Couchbase

19 Comments

  1. Nice post! Maintaining the session state process in ASP f or ASP NET programmers is very essential and for that they have to keep certain points in mind while working on any project or module or application.

  2. Is this compatible with Couchbase 2.0 or should we expect an upgrade to this as well?

    1. There\’s nothing breaking in the API that shouldn\’t allow it to work with 2.0 server, however SessionStateProvider project will have to be updated to use the 1.2 .NET client when you build it. I\’ll try to get an updated NuGet package out soon…

      1. Plus 1 Vote to have the session state provider updated on NuGet! Thanks a ton.

  3. Has the Couchbase ASP.NET SessionState Provider been tested with Couchbase 2.0 Server?

    1. At this time, it has not been updated for 2.0. That\’s not to say it won\’t work with 2.0, but I personally haven\’t run it against 2.0. I will aim to do that in the near future though…

  4. Can we set session timeout for couchbase less than 500 minutes in the Web.config?. Because I was set session timeout to 60 minutes but it was not working & when I set it to 500 minutes or nothing then it is working absolutely fine. I did not find any reason behind it.

    Is it related something to cloud cluster management or something else?

    In Web.config,
    <sessionstate customprovider=\”Couchbase\” mode=\”Custom\” timeout=\”60\”>
    <providers>
    <add name=\”Couchbase\” type=\”Couchbase.AspNet.SessionState.CouchbaseSessionStateProvider, Couchbase.AspNet\”/>
    </providers>
    </sessionstate>

    1. Could you describe what you mean by \”not working?\” Were you seeing errors?

  5. Hi John – have you done any performance testing on this for ASP Session State vs SQL Server State versus Couchbase State?

  6. AsianMoviePulse July 7, 2013 at 7:21 pm

    anyone tested this with couchbase 2.0 – this looks like a very interesting project?

    1. Matt Ingenthron July 8, 2013 at 6:44 am

      I\’ve heard of use of it with 2.0, but there haven\’t been any updates recently. Couchbase Server 2.0 and 2.1 are compatible with everything that worked with 1.8.

      1. Hi Matt
        Yes i\’ve run a test with 2+ and its working fine – great stuff mate

  7. I decided to give this a try, but I\’m not able to get the assembly to load. It\’s in the GAC of my Windows 2k8 server with IIS7 (.NET 4.5), however IIS still gives me \”Could not load file or assembly\”. There is an add assembly entry in the global web.config for the exact framework version my application uses. I\’ve verified with gacutil that it\’s actually in the gac.

    Also, there isn\’t any kind of installation instructions at all, not here and hot on github. Shouldn\’t such basic information be included in the readme?

  8. Using this successfully with couchbase 2.1.1 Enterprise.

  9. I have used the sample ASP.NET MVC project to peform a load test. My load test consists of performing a login POST, and then loading the homepage using the session. I have found that Couchbase Session State provider performs 4x slower than SQL Server Session Provider. When I use a 2 node Couchbase Cluster, with 4 vCPU, 8GB RAM each, a memcached bucket, I am able to generate only about 80 TPS. SQL Server gives me over 200 TPS! Then I used a single node to see if multi-node replication is bring it down or not. I got a mere 57 TPS. I can see from the couchbase dashboard, it is doing 531 ops/sec. So, almost 10x the ops compared to the ASP.NET page hit.
    Couchbase
    http://imgur.com/oF9Miwz

    SQL Server
    http://imgur.com/hhRIqiN

    1. @oazabir:disqus-

      Those numbers seem very low for Couchbase in general, so I am not sure what exactly is going wrong here. We are working on some improvements and should have a new release of the provider soon which may address some of the differences you have found.

      Curious, which version of the provider, SDK and Couchbase are you testing with? Is your cluster on-premise or cloud hosted?

      Thanks,

      Jeff

      1. `Thanks Jeff for responding. I have downloaded the 4.0 version of Couchbase and the latest SDK 4 days back. Also got the github codebase for the sample 4 days back.

  10. I would be interested as well how to install this on the webserver.

  11. […] addition to managing your ASP.NET session state with Couchbase Server, you can now use Couchbase Server as the backing store for your […]

Leave a reply