0

We are using Cosmos Changefeed listeners to update the edge cache in ephemeral java services. That means, all the arbitrary number of instances should receive every changefeed. We used UUID as the "hostname" but not all instances are getting the changefeed. I read somewhere there is leasePrefix. Will that work? If so how to do that on Java side of things?

so-random-dude
  • 15,277
  • 10
  • 68
  • 113

1 Answers1

1

Yes, Lease prefix will help you in this case. A scenario where you want to do multiple things whenever there is a new event in a particular Azure Cosmos container. If actions you want to trigger, are independent from one another, the ideal solution would be to create one listener for Cosmos DB per action you want to do, all listening for changes on the same Azure Cosmos container.

Given the requirements of the listeners for Cosmos DB, we need a second container to store state, also called, the leases container. Does this mean that you need a separate leases container for each Azure Function?

Here, you have two options:

Create one leases container per Listener: This approach can translate into additional costs, unless you're using a shared throughput database. Remember, that the minimum throughput at the container level is 400 Request Units, and in the case of the leases container, it is only being used to checkpoint the progress and maintain state.

Have one lease container and share it for all your Listeners: This second option makes better use of the provisioned Request Units on the container, as it enables multiple Listeners to share and use the same provisioned throughput.

Here is an example of Function App to implement this in Java Language: https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-cosmosdb-v2-trigger?tabs=java

Code for quick reference:

@FunctionName("cosmosDBMonitor")
    public void cosmosDbProcessor(
        @CosmosDBTrigger(name = "items",
            databaseName = "ToDoList",
            collectionName = "Items",
            leaseCollectionName = "leases",
            leaseCollectionPrefix = "prefix",
            createLeaseCollectionIfNotExists = true,
            connectionStringSetting = "AzureCosmosDBConnection") String[] items,
            final ExecutionContext context ) {
                context.getLogger().info(items.length + "item(s) is/are changed.");
            }
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Harshita Singh
  • 4,590
  • 1
  • 10
  • 13
  • Thank you. Whats the role of "HostName" ? – so-random-dude Sep 29 '20 at 17:54
  • Sorry, I did not get your question. What is it? – Harshita Singh Sep 29 '20 at 18:17
  • what I meant to ask is `new ChangeFeedProcessorBuilderImpl().hostName("someValue")` whats the significance of hostname here – so-random-dude Oct 01 '20 at 05:02
  • https://learn.microsoft.com/en-us/azure/cosmos-db/change-feed-processor 3.The host: A host is an application instance that uses the change feed processor to listen for changes. Multiple instances with the same lease configuration can run in parallel, but each instance should have a different instance name. – so-random-dude Oct 01 '20 at 05:04
  • Also whats this `WithInstanceName` mentioned here https://learn.microsoft.com/en-us/azure/cosmos-db/change-feed-processor . is it available in java too or only in c#? – so-random-dude Oct 01 '20 at 18:19
  • Pls refer to this tutorial for the Change feed implementation in Java - https://learn.microsoft.com/en-us/azure/cosmos-db/create-sql-api-java-changefeed – Harshita Singh Oct 05 '20 at 07:06