Redis cache with Azure function

Suresh Thakur, Kirti 86 Reputation points
2023-11-28T06:28:14.5733333+00:00

I have implemented a Timer Trigger Az function app.

This fn app will run every 30 mins

The fn app will call the cosmoscache service and will bring latest value from cosmos.

Now the challenge

If the fn app runs at 10 Am (assumption) and transaction happenes at 10.10 AM the transaction will have latest value but the transaction with executed before 10 AM doesnt have latest values from cosmos.

How to handle this scenario.

Also I am scaling my fn app to 5 instances at one call but this will not happen in when there is no load on system, so how to keep cache updated even in the under loaded system.

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,635 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,613 questions
Azure Cache for Redis
Azure Cache for Redis
An Azure service that provides access to a secure, dedicated Redis cache, managed by Microsoft.
232 questions
Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,533 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Pramod Valavala 20,611 Reputation points Microsoft Employee
    2023-11-28T21:33:21.4466667+00:00

    @Suresh Thakur, Kirti If I understand correctly, you are trying to cache values from CosmosDB to a Redis store, which your function app later uses. Assuming that is correct, I would try to design your solution in a different way.

    Instead of having a timer-based sync function, you should move this functionality into the same service (like an API or batch job) that feeds data into CosmosDB instead. Whenever there is an update to CosmosDB, update the cache as well, either synchronously in the same function or asynchronously by using a separate message triggered function via a Message Broker like Azure Service Bus.

    Or another approach would be to have your function app that processes the transaction to get the value from CosmosDB and add it to the cache when not present. You can either set a TTS on the cache value or invalidate it when CosmosDB receives an update, depending on how critical the latest message having is.