I’ve started to use Azure Cosmos DB a bit more over the last couple weeks and I’m really enjoying it. The first real world scenario that I hit was needing to implement optimistic concurrency. This led me straight into I discovered two caching optimizations you can make for better performance accessing individual documents.
Sponsor: Do you build complex software systems? See how NServiceBus makes it easier to design, build, and manage software systems that use message queues to achieve loose coupling. Get started for free.
Caching SelfLinksIf you are using the .NET SDK, each document contains a unique
SelfLinkproperty. This is represented by the
_selfproperty in the JSON. They are guaranteed to be unique and most importantly immutable. Because the
SelfLinkis immutable we can cache it and and then use it to access the associated document. It is more efficient to access the document directly via the
SelfLinkrather than querying the collection and filtering by
ETagsEach document within Azure Cosmos DB also has has an ETag Property. This is the
_etagin the json document or when you are using the .NET SDK as the
ETagon your Document.
The ETag or entity tag is part of HTTP, the protocol for the World Wide Web. It is one of several mechanisms that HTTP provides for web cache validation, which allows a client to make conditional requests.You may be familiar with ETag’s related caching. A typical scenario is a user makes an HTTP request to the server for a specific resource. The server will return the response along with an ETag in the response header. The client then caches the response along with the associated ETag.
ETag: "686897696a7c876b7e"If they client then makes another request to the same resource, it will pass a If-Non-Match header with the ETag it received.
If-None-Match: "686897696a7c876b7e"If the resource has not changed and the ETag represents the current version, then the server will return a 304 Not modified status. If the resource has been modified, it will return the appropriate 200 status code along with the content new ETag header.
AccessConditionAzure Cosmos DB uses ETags for handling caching exactly as you would expect for caching. We can store
ETagwhen we retrieve our document and then subsequently use that
ETagwhen we need to fetch the same document again. We can do this by creating an
AccessConditionand specifying an
AccessConditionTypewhen we call
Cache ClientPutting it all together can look something like this. I’m using the
MemoryCacheto store our fetched documents. Since these documents contain the
SelfLinkwe can make any other request to that document directly. Also with the
ETagon the document, when we query the document directly, we can specify an
If-None-Matchfor the server to return us a 304 Not Modified. Since this is just a simple extension method on the DocumentClient, here are a couple of tests that verify that the document is from the cache when the server returns a 304.