Blog Post

Introducing the Couchbase ASP.NET SessionState Provider

John Zablocki Published

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"/>    
    </servers>
</couchbase>
<sessionState customProvider="Couchbase" mode="Custom">
  <providers>
    <add name="Couchbase" type="Couchbase.AspNet.SessionState.CouchbaseSessionStateProvider, Couchbase.AspNet" />
  </providers>
</sessionState>

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.