DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Data Engineering
  3. Databases
  4. Cosmos DB as Key Object Store

Cosmos DB as Key Object Store

In this article, readers will gain hands-on experience with Cosmos DB as a potential key object store.

Srivatsan Balakrishnan user avatar by
Srivatsan Balakrishnan
·
Feb. 05, 23 · Tutorial
Like (1)
Save
Tweet
Share
2.50K Views

Join the DZone community and get the full member experience.

Join For Free

 Cosmos DB can be a good candidate for a key-value store. Cosmos DB is a multimodal database in Azure that supports schema-less storage. By default, Cosmos DB containers tend to index all the fields of a document uploaded. We can limit the index properties only to id and partitionkey to make the container a pure key-value store. Prohibiting other fields to be indexed increases performance and reduces RU to query point records. (Cosmos DB cost is measured in RU.) For key object storage, RU tends to be less, but it still depends on the payload size. Performance tests conducted on 100 partitions and 100K records gave a P95 between 25-30ms for read and write.

Let's dive in and implement Cosmos DB as a pure key-value store in C#.

Prerequisites:

  1. Have an account in the Azure portal
  2. Provision Cosmos DB with a SQL account.

There are a number of ways to hook up the cache into a .NET app. Let's define a generic distributed cache interface with some extensions.

C#
 
interface IDistributedObjectCache
    {
        Task<bool> SetObjectAsync<T>(string key, T value, CancellationToken cancellationToken = default);
        Task<bool> SetObjectAsync<T>(string key, string pk1, T value, CancellationToken cancellationToken = default);

        Task<T> GetObjectAsync<T>(string key, CancellationToken cancellationToken = default);
        Task<T> GetObjectAsync<T>(string key, string pk1, CancellationToken cancellationToken = default);
    }


Compared to IDistributedCache from the .NET framework, IDistributedObjectCache gives the extensibility to add a partitionkey as per application needs.

Let's predefine all the connection parameters needed for the CosmosClient connection

C#
 
public class DbConstants
    {
        public const string ConnectionString = "<CosmosDb connection string>";

        public const string CacheDatabaseId = "ObjectCacheDb";

        public const string CacheContainerId = "CacheContainer";
    }


Let's define a CosmosClientManager which helps us to manage the CosmosClient connection object. You can change ApplicationRegion to your desired location.

C#
 
public class CosmosClientManager : IDisposable
    {
        public CosmosClientManager()
        {
            if (!string.IsNullOrWhiteSpace(DbConstants.ConnectionString))
            {
                CosmosClient = new CosmosClient(DbConstants.ConnectionString, new CosmosClientOptions
                {
                    ConnectionMode = ConnectionMode.Direct,
                    IdleTcpConnectionTimeout = TimeSpan.FromMinutes(30),
                    ApplicationRegion = Regions.WestUS2,
                });
            }
        }

        public CosmosClient? CosmosClient { get; } = null;

        public void Dispose()
        {
            CosmosClient?.Dispose();
        }
    }


Let's define an object model on how records in a cache container will look like

C#
 
public class ObjectContainer<TPayload>
    {
        [Required]
        [JsonProperty("id")]
        public string PrimaryKey { get; set; }

        [Required]
        public string PartitionKey { get; set; }

        public TPayload Payload { get; set; }

        [Required]
        public string ContainerId => DbConstants.CacheContainerId;

        public string PartitionKeyPath => $"/{nameof(PartitionKey)}";

        [JsonProperty("_etag")]
        public string ETag { get; }

        [JsonProperty("_ts")]
        public long DateTimestamp { get; set; }
    }


I created a simple IWarmup to initialize components in a background thread.

C#
 
internal interface IWarmUp
    {
        Task<bool> WarmUpAsync();
    }


Let's define the core CosmosDistributedObjectStore that implements all needed interfaces.

C#
 
public class CosmosDistributedObjectStore : IDistributedObjectCache, IWarmUp
    {
        private readonly CosmosClientManager cosmosClientManager;

        public CosmosDistributedObjectStore(
            CosmosClientManager cosmosClientManager
            )
        {
            this.cosmosClientManager = cosmosClientManager;
        }

        public Task<T> GetObjectAsync<T>(string key, CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();

            return GetObjectAsync<T>(key, key, cancellationToken);
        }

        public async Task<T> GetObjectAsync<T>(string key, string pk1, CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var container = GetCurrentContainer();

            if (container == null) return default;

            var record = await container.ReadItemAsync<ObjectContainer<T>>(key, new PartitionKey(pk1), cancellationToken: cancellationToken);

            return record != null ? record.Resource.Payload : default;
        }

        public Task<bool> SetObjectAsync<T>(string key, T value, CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();

            return SetObjectAsync<T>(key, key, value, cancellationToken);

        }

        public async Task<bool> SetObjectAsync<T>(string key, string pk1, T value, CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var container = GetCurrentContainer();

            if (container == null) return false;

            var record = new ObjectContainer<T>
            {
                PartitionKey = key,
                PrimaryKey = key,
                Payload = value,
            };

            await container.CreateItemAsync(record);

            return true;

        }

        public async Task<bool> WarmUpAsync()
        {
            var entry = new ObjectContainer<dynamic>();
            var containerProperties = new ContainerProperties(entry.ContainerId, entry.PartitionKeyPath);

            // optimize container as a pure keyvalue store. Disable indexding other than id column
            containerProperties.IndexingPolicy.ExcludedPaths.Add(new ExcludedPath() { Path = "/*" });

            var db = await cosmosClientManager.CosmosClient?.CreateDatabaseIfNotExistsAsync(DbConstants.CacheDatabaseId, 1000);
            var container = await db?.Database?.CreateContainerIfNotExistsAsync(containerProperties, 1000);

            return true;
        }

        private Container? GetCurrentContainer()
        {
            return cosmosClientManager?.CosmosClient?.GetContainer(DbConstants.CacheDatabaseId, DbConstants.CacheContainerId);
        }
    }


I would like to emphasize WarmUpAsync() and how to fine-tune container creation to the index id and primaryKey fields only in order to make the container work as a pure Key Object Store.

            containerProperties.IndexingPolicy.ExcludedPaths.Add(new ExcludedPath() { Path = "/*" }); 

Let's write a simple caller to test our new key object store.

C#
 
static async Task Main(string[] args)
    {
        var cosmosDistObjectStore = new CosmosDistributedObjectStore(new CosmosClientManager());


        IWarmUp components = cosmosDistObjectStore;
        IDistributedObjectCache objectCache = cosmosDistObjectStore;

        await components.WarmUpAsync();

        var studentKey = "alpha";
        var studentObject = new { Name = "alpha", Age = 10, Class = "Grade-V" };

        // set cache
        await objectCache.SetObjectAsync(studentKey, studentObject);

        // get student grade details
        var alphaDetails = await objectCache.GetObjectAsync<dynamic>(studentKey, studentObject.Class);

        // get all cache
        var alpha = await objectCache.GetObjectAsync<dynamic>(studentKey);
    }


We now have a robust alternate NoSQL key object store with Azure Cosmos DB that can partition and scale on demand.

Cosmos DB Key-value database Performance improvement

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • DevOps for Developers: Continuous Integration, GitHub Actions, and Sonar Cloud
  • Solving the Kubernetes Security Puzzle
  • mTLS Everywere
  • Stop Using Spring Profiles Per Environment

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: