Azure Function cannot process data streaming at 10 Hz

Alberto Morando 20 Reputation points
2024-12-03T09:52:23.3433333+00:00

Hi

I have the following architecture

User's image

I am using the Azure Function to be able to save data in CosmosDB for Mongo since this is not supported by the Azure Streaming Analytics API and we need to use MongoDB.

All is working but when I test with incoming messages at 10 Hz I can see that Azure Function cannot save all the messages in the CosmosDB for Mongo (i.e. some of the messages are missing). It seems that the performance of the Azure Faction is not good enough.

The Azure Function is Azure Service Plan with P2v3 pricing plan

User's image

The Azure Function is


import azure.functions as func
import logging
import pymongo
import os

app = func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION)

@app.route(route="SaveToMongoDB")
def SaveToMongoDB(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    client = pymongo.MongoClient(os.environ['cosmosdbformongodb_connectionstring'])

    db = client["db"]
    json_data = req.get_json()[0]
    db["events"].insert_one(json_data)
    
    return func.HttpResponse(f"This HTTP triggered function executed successfully.")

My questions are:

  1. Is this the correct setup for saving the streaming data to CosmosDB for Mongo?
  2. if yes to the 1) why is the Azure Function not performing to save all the data? Do I miss some setting?

Please let me know

Thanks
Alberto

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,185 questions
Azure Stream Analytics
Azure Stream Analytics
An Azure real-time analytics service designed for mission-critical workloads.
365 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Sander van de Velde | MVP 33,556 Reputation points MVP
    2024-12-03T18:13:50.6033333+00:00

    Hello @Alberto Morando,

    welcome to this moderated Azure community forum.

    Although I use C#, Azure Functions are reliable in my situation.

    10Hz could be a challenge if you have to do the scaling yourself because you need to monitor the resource usage.

    Can you first test the Azure Function being triggered while no actual logic being executed?

    So you only log the separate calls and show 10Hz is not an issue?

    This way, is this works well, the challenge could be the DB connection.

    I'm not an expert regarding Python, but it seems you are creating and destroying a DB connection with EVERY call, 10 times a second.

    Can you reuse an existing connection that survives the stateless Function calls?


    If the response helped, do "Accept Answer". If it doesn't work, please let us know the progress. All community members with similar issues will benefit by doing so. Your contribution is highly appreciated.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.