TJ_
TJ_

Reputation: 647

Azure Function Queue Trigger Gets Triggered Twice, Two Instances Running?

I have an Azure Function, with a queue trigger. The function must proces one queue message each time, one by one, sequentially. This is important, since the function uses an external OAuth API, with various limitions on requesting new access and refresh tokens.

In order to process the queue sequentially, I've the following settings:

host.json

"queues": {
  "batchSize": 1,
  "newBatchThreshold": 0
}

Application settings

FUNCTIONS_WORKER_PROCESS_COUNT = 1
WEBSITE_MAX_DYNAMIC_APPLICATION_SCALE_OUT = 1

Despite these settings, it happens sometimes that the function gets triggered multiple times. Not in my tests, where I dump a lot of messages in the queue, but when the functions is live.

I've dived into the Application Insights Log, and I found that the function gets restarted a lot during the day, and when it restarts, it sometimes start the functions twice.

For example:

Log

Two instances are started. I'm guessing this causes my double triggers from the queue.

How can I make sure the function is triggered once?

Upvotes: 4

Views: 2244

Answers (1)

Alex
Alex

Reputation: 18526

I believe the only way to make it even more secure that only one message will be executed at the same time is to use sessions. Give every message the same sessionId to guarantee your desired behavior. Be aware that some things like deadlettering will behave differently if you switch to sessions.

You can also use the [Singleton] attribute if that fits your function app. It should also work, but can incur additional costs when you use the consumption plan. So you need to validate how it affects costs given your workload.

See also my other answer here: azure servicebus maxConcurrentCalls totally ignored

Upvotes: 2

Related Questions