Skip to content

Commit bb7232b

Browse files
author
Matias Quaranta
committed
Updating article
1 parent a71190e commit bb7232b

File tree

2 files changed

+20
-34
lines changed

2 files changed

+20
-34
lines changed

articles/cosmos-db/change-feed-functions.md

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,60 +4,46 @@ description: Use Azure Cosmos DB change feed with Azure Functions
44
author: rimman
55
ms.service: cosmos-db
66
ms.topic: conceptual
7-
ms.date: 11/06/2018
7+
ms.date: 04/12/2019
88
ms.author: rimman
99
ms.reviewer: sngun
1010
---
1111

12-
# Trigger Azure Functions from Azure Cosmos DB
12+
# Serverless event-based flows with Azure Functions and Azure Cosmos DB
1313

14-
If you're using Azure Functions, the simplest way to connect to change feed is to add an [Azure Cosmos DB trigger](../azure-functions/functions-bindings-cosmosdb-v2.md#trigger) to your Azure Functions app. When you create a Cosmos DB trigger in an Azure Functions app, you select the Cosmos container to connect to and the function is triggered whenever you change something in the container.
14+
Azure Functions provides the simplest way to connect to the Change Feed. You can create small reactive Functions that will be automatically triggered on each new event in your Azure Cosmos DB container’s Change Feed.
1515

16-
Triggers can be created in the Azure Functions portal or in the Azure Cosmos DB portal or programmatically. For more information, see [serverless database computing using Azure Cosmos DB and Azure Functions](serverless-computing-database.md).
16+
![Serverless event-based Functions working with the Azure Cosmos DB Trigger](./media/change-feed-functions/functions.png)
1717

18-
## Frequently asked questions
18+
With the [Azure Cosmos DB Trigger](../azure-functions/functions-bindings-cosmosdb-v2.md#trigger), you can leverage the Change Feed Processor scalability and reliable event detection without the need to maintain any worker infrastructure. Just focus on what you want your Function to do and leave the heavy lifting to us. You can even mix the Trigger with any other [Azure Functions bindings](../azure-functions/functions-triggers-bindings.md#supported-bindings).
1919

20-
### How can I configure Azure functions to read from a particular region?
20+
> [!NOTE]
21+
> Currently, the Azure Cosmos DB trigger is supported for use with the SQL(Core) API only.
2122
22-
It is possible to define the [PreferredLocations](https://docs.microsoft.com/dotnet/api/microsoft.azure.documents.client.connectionpolicy.preferredlocations?view=azure-dotnet#Microsoft_Azure_Documents_Client_ConnectionPolicy_PreferredLocations) when using the Azure Cosmos DB trigger to specify a list of regions. It's same as you customize the ConnectionPolicy, to make the trigger read from your preferred regions. Ideally, you want to read from the closest region where your Azure Functions is deployed.
23+
## Requirements
2324

24-
### What is the default size of batches in Azure Functions?
25+
To implement a serverless event-based flow, you need:
2526

26-
The default size is 100 items for every invocation of Azure Functions. However, this number is configurable within the function.json file. Here is complete [list of configuration options](../azure-functions/functions-bindings-cosmosdb-v2.md#trigger---configuration). If you are developing locally, update the application settings within the local.settings.json file.
27+
* **The monitored container**: The monitored container has the data from which the change feed is generated. Any inserts and changes to the monitored container are reflected in the change feed of the container.
28+
* **The lease container**: The lease container maintains state across multiple and dynamic serverless Function instances and enables dynamic scaling. This lease container can be pre-created or automatically created by the Azure Cosmos DB Trigger if you set the *CreateLeaseCollectionIfNotExists* flag in the [configuration](../azure-functions/functions-bindings-cosmosdb-v2.md#trigger---configuration). Partitioned lease containers are required to have a `/id` Partition Key definition.
2729

28-
### I am monitoring a container and reading its change feed, however I don't get all the inserted documents, some items are missing?
30+
## Creating your Azure Cosmos DB Trigger
2931

30-
Make sure that there is no other Azure Function reading the same container with the same lease container. The missing documents are processed by the other Azure Functions that are also using the same lease.
32+
Creating your Function with an Azure Cosmos DB Trigger is now supported across all Azure Functions IDE and CLI integrations:
3133

32-
Therefore, if you are creating multiple Azure Functions to read the same change feed, they must use different lease containers or use the "leasePrefix" configuration to share the same container. However, when you use change feed processor library you can start multiple instances of your Azure Function and the SDK will divide the documents between different instances automatically for you.
34+
* [Visual Studio Extension](../azure-functions/functions-develop-vs.md) for Visual Studio users.
35+
* [Visual Studio Core Extension](https://code.visualstudio.com/tutorials/functions-extension/create-function) for Visual Studio Code users.
36+
* And finally [Core CLI tooling](../azure-functions/functions-run-local.md#create-func) for a cross-platform IDE agnostic experience.
3337

34-
### Azure Cosmos item is updated every second, and I don't get all the changes in Azure Functions listening to change feed?
38+
## Running your Azure Cosmos DB Trigger locally
3539

36-
Azure Functions polls the change feed for changes continuously, with a maximum default delay of 5 seconds. If there are no pending changes to be read, or if there are changes pending after the trigger is applied, the function will read them right away. However, if there are no pending changes, the function will wait 5 seconds and poll for more changes.
40+
You can run your [Azure Function locally](../azure-functions/functions-develop-local.md) with the [Azure Cosmos DB Emulator](./local-emulator.md) to create and develop your serverless event-based flows without an Azure Subscription.
3741

38-
If your document receives multiple changes in the same interval that took the Trigger to poll for new changes, you might receive the latest version of the document and not the intermediate one.
39-
40-
If you want to poll change feed for less than 5 seconds, for example, for every second, you can configure the polling time "feedPollDelay", see [the complete configuration](https://docs.microsoft.com/dotnet/api/microsoft.azure.documents.client.connectionpolicy.preferredlocations?view=azure-dotnet#Microsoft_Azure_Documents_Client_ConnectionPolicy_PreferredLocations). It is defined in milliseconds with a default of 5000. Polling for less than 1 second is possible but it's not advised because you will start using more CPU memory.
41-
42-
### Can multiple Azure Functions read one container’s change feed?
43-
44-
Yes. Multiple Azure Functions can read the same container’s change feed. However, the Azure Functions needs to have a separate “leaseCollectionPrefix” defined.
45-
46-
### If I am processing change feed by using Azure Functions, in a batch of 10 documents, and I get an error at seventh Document. In that case the last three documents are not processed how can I start processing from the failed document (i.e, seventh document) in my next feed?
47-
48-
To handle the error, the recommended pattern is to wrap your code with try-catch block and, if you are iterating over the list of documents, wrap each iteration in its own try-catch block. Catch the error and put that document on a queue (dead-letter) and then define logic to deal with the documents that produced the error. With this method if you have a 200-document batch, and just one document failed, you do not have to throw away the whole batch.
49-
50-
In case of error, you should not rewind the check point back to beginning else you will can keep getting those documents from change feed. Remember, change feed keeps the last final snap shot of the documents, because of this you may lose the previous snapshot on the document. change feed keeps only one last version of the document, and in between other processes can come and change the document.
51-
52-
As you keep fixing your code, you will soon find no documents on dead-letter queue. Azure Functions is automatically called by change feed system and check point is maintained internally by Azure Function. If you want to roll back the check point and control every aspect of it, you should consider using change feed Processor SDK.
53-
54-
### Are there any extra costs for using the Azure Cosmos DB Trigger?
55-
56-
The Azure Cosmos DB Trigger leverages the Change Feed Processor library internally. As such, it requires an extra collection, called Leases collection, to maintain state and partial checkpoints. This state management is needed to be able to dynamically scale and continue in case you want to stop your Azure Functions and continue processing at a later time. To learn more, see [how to work with change feed processor library](change-feed-processor.md).
42+
And if you want to test live scenarios, you can [Try Cosmos DB for free](https://azure.microsoft.com/try/cosmosdb/) without any credit card or Azure subscription required.
5743

5844
## Next steps
5945

60-
You can now proceed to learn more about change feed in the following articles:
46+
You can now continue to learn more about change feed in the following articles:
6147

6248
* [Overview of change feed](change-feed.md)
6349
* [Ways to read change feed](read-change-feed.md)
32.6 KB
Loading

0 commit comments

Comments
 (0)