4
votes

The best practices article from Azure docs recommends reusing QueueClient to send multiple messages to obtain faster performance. https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-performance-improvements#reusing-factories-and-clients

I have a scenario of request-response messaging where Azure function is sending responses after being triggered by a service bus queue trigger (which is a request message). The performance of QueueClient.SendAsync is slow (1.5 to 3 seconds) if I create a new QueueClient everytime the function executes.

If I reuse the QueueClient in a static variable then the SendAsync time reduces to 50ms for later calls. However, the static reference does not seem to be a reliable way of reusing QueueClient connection, since I do not know when to close it.

Is there a reliable way to reuse and then close the QueueClient across multiple function executions? Can Azure-Function runtime provide any event where I can reliably close the queue client?

2

2 Answers

3
votes

I think the challenge here is that the Azure Function doesn't know when you are "done" sending messages. If your Function has gone idle for sometime we may remove instances, but I don't believe there is any downside to doing a static QueueClient in a Function without ever explicitly closing it -- and it would be a best practice.

2
votes

I suggest you switching to Service Bus output binding instead of sending messages manually with QueueClient. This way runtime will manage the instances of QueueClient and their configuration for you. See Azure Functions Service Bus bindings, "Service Bus output binding" section.

Otherwise, static QueueClient as suggested by @jeffhollan is a viable option.