BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News New AWS .NET Distributed Cache Provider for DynamoDB in Preview

New AWS .NET Distributed Cache Provider for DynamoDB in Preview

Recently AWS announced the preview release of the AWS .NET Distributed Cache Provider for DynamoDB. This library enables Amazon DynamoDB to be used as the storage for ASP.NET Core’s distributed cache framework. 

The AWS .NET Distributed Cache Provider for DynamoDB is the successor to the AWS DynamoDB Session State Provider, offering compatibility with both .NET Framework and ASP.NET Core applications. While the session state provider library, released in 2012, is limited to .NET Framework applications, the new distributed cache provider also extends its functionality to support ASP.NET Core.

Alex Shovlin, software development engineer at AWS, explains in an AWS Developer Tools blog post the benefit of caching:

A cache can improve the performance of an application; an external cache allows the data to be shared across application servers and helps to avoid cache misses when the application is restarted or redeployed.

To use the library, developers can install the AWS.AspNetCore.DistributedCacheProvider package from NuGet.org. For instance, when they are building an application that requires sessions in a distributed webapp, .NET's session state middleware looks for an implementation of IDistributedCache to store the session data. They can accomplish that by directing the session service to use the DynamoDB distributed cache implementation through dependency injection.

The company recommends configuring the cache provider with the following options for production applications:

  • A Tablename, the name of the DynamoDB table that will store the cache data
  • A PartitionKeyName, the name of the DynamoDB table’s partition key
  • A TTLAttributeName, a DynamoDB’s Time To Live (TTL) feature, which removes cache items from the table
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddAWSDynamoDBDistributedCache(options =>

{
    options.TableName = "session_cache_table";
    options.PartitionKeyName = "id";
    options.TTLAttributeName = "cache_ttl";
});

builder.Services.AddSession(options =>

{
    options.IdleTimeout = TimeSpan.FromSeconds(90);
    options.Cookie.IsEssential = true;
});

var app = builder.Build();
...

In addition, Shovlin points out in the blog post that:

  • The partition key must be of a type string without a sort key required for the table to prevent exceptions during cache operations
  • Enable DynamoDB's Time to Live feature for the table to ensure expired cached entries are automatically deleted
  • Prefix the partition keys of cached items with a configurable value (default prefix is "dc:") to avoid collisions and enable fine-grained access control using IAM policy conditions
  • Additionally, consider using CreateTableIfNotExists for development or testing purposes, as it allows automatic table creation but may add latency to the first cache operation

Azure Cosmos DB, a comparable managed service like DynamoDB on Azure, can also serve as a cache for the session state. A Microsoft Caching Extension using Azure Cosmos DB package contains an implementation of IDistributedCache. This package can be leveraged in ASP.NET Core as a Session State Provider.

Alternatively, developers can use Amazon Relational Database Service (Amazon RDS) for SQL Server and Amazon ElastiCache for Redis for caching regarding ASP.NET Core applications. Both have an implementation of IDistributedCache: Microsoft.Extensions.Caching.SqlServer and Microsoft.Extensions.Caching.StackExchangeRedis.

The authors of a Microsoft Workloads on AWS blog post last year concluded:

AWS provides several managed services that can serve as a managed cache for your ASP.NET Core web applications. We examined how Amazon RDS for SQL Server and Amazon ElastiCache for Redis can be used as a distributed cache layer for your applications. They are managed services that are simple to set up and scale, with low operational overhead.

With AWS .NET Distributed Cache Provider for DynamoDB library, there is an additional now with Amazon DynamoDB.

The Microsoft documentation shows more details on using an IDistributedCache implementation to save session state data. In addition, the .NET on AWS page provides information on learning about building, deploying, and developing .NET applications on AWS.

About the Author

Rate this Article

Adoption
Style

BT