Azure Functions in ASP - Distribute the traffic to multiple instances

Singh, Rahul 1 Reputation point
2024-07-25T18:10:13.22+00:00

I have a Azure function which is triggered by azure service bus. The function is deployed to Azure app service. Earlier I had 1 instance, but sometimes the memory was reaching 100% so I have added another instance.

I am still seeing the same issue, if I check the metric it shows the memory is used by 1 instance is above 100% and at the same time the other instance is just sitting at 20%.

I have session added to Service bus messages so multiple instances of functions can run at the same time. Could you let me know if there is any way to split the traffic evenly? Or how better I can strategize this?

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,670 questions
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
7,405 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Sina Salam 7,441 Reputation points
    2024-07-25T19:31:53.4866667+00:00

    Hello Singh, Rahul,

    Welcome to the Microsoft Q&A and thank you for posting your questions here.

    Problem

    I understand that you are experiencing uneven memory usage across multiple instances of your Azure Function triggered by Azure Service Bus, and you would like to split the traffic evenly or strategically distribute the traffic to multiple instances of Azure Functions in ASP.

    Solution

    Managing memory usage in Azure Functions can be crucial for performance and cost optimization. However, to address this issue and explore strategies to optimize your setup, I put together the below for your scenario.

    1. Ensure your function code is efficient and doesn't unnecessarily consume memory. Consider reducing the number of functions within your function app. Having too many functions can impact startup time and overall memory usage and if possible, use smaller memory configurations for your function instances.
    2. If you have multiple Service Bus event queues triggering your Azure Function, consider forwarding messages from these queues to a single entity (e.g., a queue). Configure your Function to be triggered by messages from this consolidated queue. Azure Service Bus supports Auto-Forwarding, which allows you to forward messages from one entity to another. Keep in mind that there's a limit of 3 hops, and you won't necessarily know the original source if a message was forwarded.
    3. For subscriptions, you can use rules to add custom metadata (headers) to messages. Each subscription rule has a filter and an action. Create a rule that "stamps" messages with additional information (e.g., the subscription name). When your Function receives a message, it can identify its source based on this custom header. This approach allows you to reuse the same Function for different events while maintaining differentiation based on the stamped metadata.
    4. If you're dealing with high traffic and uneven load, consider sharding your entities (queues or topics) across different Service Bus Premium namespaces. You can also shard across different namespaces in different Azure regions to distribute the load more effectively.
    5. To evenly distribute traffic between instances, consider using traffic splitting techniques:
      1. If you're using Azure Container Apps, you can split incoming traffic between active revisions based on weight percentages.
      2. Configure traffic splitting using the az containerapp ingress traffic set command.
      3. For web app or Azure Static Web Apps. Use the Traffic Splitting feature to allocate traffic among different environments (e.g., blue-green deployments or A/B testing).
      4. In the Azure portal, configure traffic splitting by specifying the percentage of traffic for each environment.
      5. Azure Application Gateway (for internal and external users). Leverage URL-based routing capabilities to direct users to appropriate servers based on URL paths or host headers
    6. Since you're using sessions with Service Bus messages, ensure that your function instances handle sessions correctly. To do this, you can verify that session state is maintained consistently across instances.

    References

    Memory considerations when hosting Azure Functions on the App Service.

    OutOfMemoryException on V3 to V4 Migration (with .NET Core 3.1 to .NET 6.0)

    Best practices for improving performance using Azure Service Bus.

    Accept Answer

    I hope this is helpful! Do not hesitate to let me know if you have any other questions.

    ** Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful ** so that others in the community facing similar issues can easily find the solution.

    Best Regards,

    Sina Salam

    0 comments No comments