I am looking into using Azure Redis Cache for one of our applications. We need to store pure JSON, which the best practices section clearly recommends against, under certain circumstances. I am very new to Redis, and I am researching possible patterns for us to fulfill our requirements, and I am unsure if my approach violates those best practices.
The pattern I want to implement is this:
- A change occurs to an item in a Cosmos database
- The change feed is parsed for updated items
- The updated item is serialized and written to Redis
- When the system is later queried, first look for a cached item. Retrieve and de-serialize if present. If not present, query Cosmos.
The SET and the GET would take place in two completely separate parts of the application. There would never be a scenario where I would retrieve, modify and set, since a Cosmos document acts as an intermediate and can never trigger simultaneous writes. When querying the system, the expectation is always that the cache is up to date if a document exists there.
What I can’t really figure out is if my approach runs the risk of data loss mentioned:
Worse yet, many applications would just GET the entire JSON string, deserialize it, manipulate it, re-serialize and SET it again at the application. This is an anti-pattern. There is a very real risk of losing data with this method.
They go on to suggest RedisJSON, which must be activated on the database level.
Unfortunately, this does not seem to be possible with Azure Redis Cache, or at least I can’t find any documentation or any option to do this whilst creating the resource nor while it has been created.
- Is it possible to use RedisJSON with Azure Redis Cache?
- If not #1, are there any alternative recommended approaches to store JSON data with Redis, or is my suggested approach a possible solution?
Update
I solved this by listening to change feeds of the containers I wanted to cache in an Azure Function. This pattern has one single application instance ever writing one unique document, and the reading happens completely separately and never changes the cached value.
The change feed receives the entire document if it is inserted or updated, but not deleted. Thanks to receiving the entire document it is easy to invalidate the cache under certain conditions or adding a soft-delete value to remove it entirely.
3
Answers
You can easily create Redis with RedisJSON on Azure using Redis Cloud see:
https://redis.com/redis-enterprise-cloud/pricing/
I think it is not supported by Azure Redis
https://learn.microsoft.com/en-us/answers/questions/709524/when-can-expected-redis-json-module-support-with-a.html
I am also implementing a solution for azure Redis cache to save JSON documents , I saved the escaped json string to Redis value
FYI, RedisJSON is available now in Azure Cache for Redis: https://azure.microsoft.com/en-us/updates/public-preview-redisjson-available-in-azure-cache-for-redis-enterprise/