New: ASP.NET Session State Provider for SQL Server In-Memory OLTP
Microsoft SQL Server 2014 brings new performance and scalability gains by introducing In-Memory OLTP. In-Memory OLTP contains tables and indexes optimized for in memory. Transactions execute under lock-free algorithms to provide linear scalability and Transact-SQL stored procedures can be compiled in native machine code for maximum efficiency in processing.
Working with SQL Server customers on In-Memory OLTP engagements, a common pattern emerged around the desire for increased performance and scalability when using ASP.NET session state. Some early adopters modified their SQL Server objects to take advantage of In-Memory OLTP for ASP.NET session state, with great success. To learn more, read the bwin.party case study “Gaming site can scale to 250,000 requests per second and improve player experience”. To further enhance this scenario, we have created a new provider to make it easier for customers to take advantage of SQL Server In-Memory OLTP when using ASP.NET session state.
This ASP.NET session state provider is fully optimized for In-Memory OLTP by calling natively compiled Transact-SQL stored procedures and by creating all tables as memory-optimized. The functionality of the provider was tested both internally and by external customers. The results showed the implementation was able to provide some significant gains at scale levels which would have previously exhibited a bottleneck on the database.
NOTE: While some testing has been done before the release, we recommend executing your own testing and validation to understand how this implementation behaves in your specific environment.
Getting Started
Setting up the provider requires two steps, installing the provider into the ASP.NET application and creating the In-Memory OLTP database and objects in Microsoft SQL Server 2014.
The provider and scripts can be accessed in two ways:
1. The package has been uploaded to NuGet: https://www.nuget.org/packages/Microsoft.Web.SessionState.SqlInMemory/
2. The source code is also accessible through CodePlex: https://msftdbprodsamples.codeplex.com/releases/view/125282
NuGet Installation
Download the ASP.NET Session State Provider for SQL Server In-Memory OLTP from the NuGet gallery by running the following command from the Visual Studio Package Manager Console:
PM> Install-Package Microsoft.Web.SessionState.SqlInMemory
More information about the NuGet package can be found here:
https://www.nuget.org/packages/Microsoft.Web.SessionState.SqlInMemory/
Installing the package will do the following things:
- Add references to the ASP.NET Session State Provider assembly.
- Add to the web.config file a customProvider equals to “SqlInMemoryProvider”, where the connectionString attribute needs to be updated.
<?xml version="1.0" encoding="utf-8"?> <configuration> <system.web> <sessionState mode="Custom" customProvider="SqlInMemoryProvider"> <providers> <add name="SqlInMemoryProvider" type="Microsoft.Web.SessionState.SqlInMemoryProvider" connectionString="data source=sqlserver;initial catalog=ASPStateInMemory;User ID=user;Password=password;" /> </providers> </sessionState> </system.web> </configuration>
- Adds an ASPStateInMemory.sql file that includes the script for creating the SQL Server database configured to support In-Memory OLTP.
Setting up In-Memory OLTP Database and objects
Open the T-SQL script file “ASPStateInMemory.sql” and update the ‘CREATE DATABASE’ statement to replace the ‘FILENAME’ attributes to specify a path that will exist in your SQL Server machine where the memory-optimized filegroup should exist. For further considerations on placement of this filegroup see Books Online section Creating and Managing Storage for Memory-Optimized Objects
CREATE DATABASE [ASPStateInMemory] ON PRIMARY ( NAME = ASPStateInMemory, FILENAME = 'D:\SQL\data\ASPStateInMemory_data.mdf' ), FILEGROUP ASPStateInMemory_xtp_fg CONTAINS MEMORY_OPTIMIZED_DATA ( NAME = ASPStateInMemory_xtp, FILENAME = 'D:\SQL\data\ASPStateInMemory_xtp' ) GO
After updating the ‘FILENAME’ attributes, run the entire script for creating the In-Memory tables and the natively compiled stored procedures.
Additionally, create a periodic task in SQL Server to run the stored procedure ‘dbo.DeleteExpiredSessions’. This procedure removes the expired sessions and frees up the memory consumed.
NOTE: The memory-optimized tables are created with a durability of SCHEMA_ONLY to optimize for performance. If session data durability is required, then change the ‘DURABILITY’ attribute from ‘SCHEMA_ONLY’ to ‘SCHEMA_AND_DATA’. More information can be found in Books Online sections Defining Durability for Memory-Optimized Objects and Durability for Memory-Optimized Tables.
Conclusion
SQL Server In-Memory OLTP has shown to greatly improve the performance of ASP.NET session state applications. This provider allows customers to optimize ASP.NET web farms to take advantage of SQL Server In-Memory OLTP using a packaged solution with ease.
For further considerations on session state with In-Memory OLTP, along with other solution patterns which have shown success with SQL Server In-Memory OLTP, please reference the whitepaper: In-Memory OLTP – Common Workload Patterns and Migration Considerations.
Download the Microsoft SQL Server 2014 Evaluation and see how in-memory processing built into SQL Server 2014 delivers breakthrough performance.