Bird
Raised Fist0
Azurecloud~20 mins

Functions with Cosmos DB integration in Azure - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Azure Functions Cosmos DB Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
service_behavior
intermediate
2:00remaining
How does an Azure Function trigger with Cosmos DB change feed behave?

An Azure Function is set to trigger on changes in a Cosmos DB container using the change feed. What happens when multiple changes occur rapidly in the container?

AThe function batches multiple changes and processes them together, while preserving order within the batch.
BThe function processes each change individually and sequentially, ensuring order is preserved.
CThe function triggers only once per minute regardless of changes, processing all changes at once.
DThe function triggers only for the first change and ignores subsequent changes until manually reset.
Attempts:
2 left
💡 Hint

Think about how change feed triggers optimize processing for multiple changes.

Configuration
intermediate
2:00remaining
Which configuration setting controls the lease container for Cosmos DB trigger in Azure Functions?

You want to configure an Azure Function with a Cosmos DB trigger. Which app setting specifies the container used to store leases for tracking processed changes?

AAzureWebJobsCosmosDBLeaseContainerName
BCosmosDBLeaseContainer
CCosmosDBTriggerLeaseContainerName
DAzureWebJobsCosmosDBLeasesContainerName
Attempts:
2 left
💡 Hint

Look for the exact app setting name used by Azure Functions runtime for Cosmos DB leases.

Architecture
advanced
3:00remaining
Designing a scalable Azure Function with Cosmos DB input binding

You need to design an Azure Function that reads documents from Cosmos DB using an input binding. The function must scale efficiently when many requests come in simultaneously. Which architecture choice best supports this?

AUse a single function instance with a large RU/s provisioned on Cosmos DB to handle all requests sequentially.
BUse a timer-triggered function that reads all documents periodically and caches results for all requests.
CUse a function with input binding that queries Cosmos DB without specifying partition key to simplify code.
DUse multiple function instances with input bindings that query Cosmos DB by partition key to distribute load evenly.
Attempts:
2 left
💡 Hint

Consider how Cosmos DB partitioning affects scalability and function instance distribution.

security
advanced
3:00remaining
Securing Azure Functions with Cosmos DB output binding

You have an Azure Function that writes data to Cosmos DB using an output binding. What is the best practice to secure the connection string used by the function?

AStore the Cosmos DB connection string directly in the function code as a constant string.
BStore the connection string in Azure Key Vault and reference it via managed identity in the function app settings.
CStore the connection string in a public GitHub repository for easy access by developers.
DHardcode the connection string in the function app settings without encryption.
Attempts:
2 left
💡 Hint

Think about secure storage and access of secrets in Azure.

🧠 Conceptual
expert
3:00remaining
Understanding consistency levels impact on Azure Function with Cosmos DB trigger

An Azure Function is triggered by Cosmos DB change feed. The Cosmos DB account uses Session consistency. How does this consistency level affect the function's visibility of changes?

AThe function sees changes only after a full global replication completes, causing high latency.
BThe function sees all committed changes immediately and in order across all regions.
CThe function sees changes in order per session, but changes from other sessions may be delayed or out of order.
DThe function cannot use change feed with Session consistency and will fail to trigger.
Attempts:
2 left
💡 Hint

Recall what Session consistency guarantees about read order and visibility.

Practice

(1/5)
1. What is the main benefit of using Azure Functions with Cosmos DB integration?
easy
A. Automatically run code when data changes in Cosmos DB
B. Manually trigger code only through HTTP requests
C. Store large files directly in Cosmos DB
D. Replace Cosmos DB with Azure Blob Storage

Solution

  1. Step 1: Understand Azure Functions with Cosmos DB

    Azure Functions can be triggered automatically by changes in Cosmos DB data.
  2. Step 2: Identify the main benefit

    This automatic trigger saves resources by running code only when needed, without manual calls.
  3. Final Answer:

    Automatically run code when data changes in Cosmos DB -> Option A
  4. Quick Check:

    Functions trigger on data changes = A [OK]
Hint: Functions run on data change events automatically [OK]
Common Mistakes:
  • Thinking functions run only on HTTP triggers
  • Confusing Cosmos DB with file storage
  • Assuming manual triggers are required
2. Which of the following is the correct binding direction for a Cosmos DB input binding in an Azure Function?
easy
A. direction: "out"
B. direction: "both"
C. direction: "trigger"
D. direction: "in"

Solution

  1. Step 1: Recall binding directions

    Input bindings receive data into the function, so their direction is "in".
  2. Step 2: Match binding direction for Cosmos DB input

    Cosmos DB input binding must have direction set to "in" to read data.
  3. Final Answer:

    direction: "in" -> Option D
  4. Quick Check:

    Input binding direction = in [OK]
Hint: Input bindings always use direction "in" [OK]
Common Mistakes:
  • Using "out" for input bindings
  • Confusing trigger with input binding
  • Using invalid directions like "both"
3. Given this Azure Function code snippet triggered by Cosmos DB changes, what will be logged if a new document with id "123" is added?
module.exports = async function (context, documents) {
  if (!!documents && documents.length > 0) {
    context.log(`Document id: ${documents[0].id}`);
  }
};
medium
A. No output logged
B. Document id: undefined
C. Document id: 123
D. Error: documents is not defined

Solution

  1. Step 1: Understand the trigger input

    The function receives an array 'documents' with changed documents; the first document has id "123".
  2. Step 2: Analyze the logging statement

    The code logs the id of the first document, which is "123".
  3. Final Answer:

    Document id: 123 -> Option C
  4. Quick Check:

    documents[0].id = 123 logged [OK]
Hint: documents array holds changed items; access first with documents[0] [OK]
Common Mistakes:
  • Assuming documents is undefined
  • Logging without checking documents length
  • Confusing document id property
4. You wrote an Azure Function triggered by Cosmos DB changes, but it never runs when documents change. Which is the most likely cause?
medium
A. The function code has a syntax error
B. The function.json binding has incorrect connection string name
C. The Cosmos DB container is empty
D. The function app is stopped

Solution

  1. Step 1: Check trigger configuration

    If the connection string name in function.json is wrong, the function won't connect to Cosmos DB changes.
  2. Step 2: Consider other causes

    Syntax errors cause failures but usually show errors; empty container still triggers on inserts; stopped app won't run but question implies function exists.
  3. Final Answer:

    The function.json binding has incorrect connection string name -> Option B
  4. Quick Check:

    Wrong connection string stops trigger [OK]
Hint: Check connection string name in function.json first [OK]
Common Mistakes:
  • Ignoring binding configuration errors
  • Assuming empty container prevents triggers
  • Not verifying function app status
5. You want to create an Azure Function that writes a summary document to Cosmos DB whenever multiple documents are added. Which binding setup should you use?
hard
A. Use Cosmos DB trigger for input and Cosmos DB output binding for summary document
B. Use HTTP trigger and Cosmos DB input binding only
C. Use Cosmos DB input binding only, no trigger
D. Use Timer trigger and Cosmos DB output binding only

Solution

  1. Step 1: Identify trigger for reacting to data changes

    Cosmos DB trigger runs the function automatically when documents change.
  2. Step 2: Use output binding to write summary

    Output binding lets the function write a new summary document back to Cosmos DB.
  3. Final Answer:

    Use Cosmos DB trigger for input and Cosmos DB output binding for summary document -> Option A
  4. Quick Check:

    Trigger input + output binding for writing = C [OK]
Hint: Trigger on changes, output binding to write summary [OK]
Common Mistakes:
  • Using HTTP trigger instead of Cosmos DB trigger
  • Missing output binding for writing data
  • Using timer trigger without data event