skip to Main Content

I am creating a poll app. Users can define one or more poll questions and configure answer options. Guests can join a session and when a poll (question) is activated, start voting. Basically what a standard poll looks like.

For processing the incoming votes, I use the Azure Service Bus. I have an endpoint that accepts votes and sends a message to a Service Bus Queue. Then, an Azure Function with a Service Bus Queue trigger will consume that message and persist the vote somewhere in a repository.

My problem is that I want another ‘background process’, I imagine another Azure Function, that will be triggered when votes come in, to go and calculate the cumulative of votes to be able to draw a pie chart.

Now I want this Function to be triggered as efficiently as possible. Key is that it must be accurate. What I’m looking for, is a method that will trigger the calculation once when a vote comes in, but when a bunch of votes comes in, I want to trigger the calculation only once after the last vote was persisted. I was thinking of introducing a new queue to send ‘calculation commands’ to. I use a real-time framework to update the pie chart. I would like to send pie-chart updates frequently, but not necessarily thousands of times a second when huge amounts of votes came in in a short amount of time.

I looked for a solution where I can use the de-duplication of an SB queue, but I think this de-dup also checks for previously sent messages. And using this solution does not guarantee that the calculation takes place after the last vote has been processed, because the message may be recognized as a duplicate and therefore ignored.

Another solution may be to introduce a SessionId for the votes queue allowing me to overcome the problem that vote messages are handled simultaneously, but this feels like an anti-pattern using the Service Bus. In the end, you want the thing to scale like a maniac when large amounts of votes come in, so for that reason, the session is a no go to me.

And now I’m running out of ideas, is there a mechanism that I overlooked that I can take advantage of to (for example) only put a message on a queue when there is no similar message waiting to be processed (e.g. without a lock) or something?

2

Answers


  1. You can trigger the Function using one of the available Event Grid events for Service Bus, if the concern is that you don’t want a listener to run at all times.

    Login or Signup to reply.
  2. The Azure Functions approach suggested by Clemens is a viable approach. You probably don’t need Event Grid because your function could be triggered by the Service Bus queue.

    I want to trigger the calculation only once after the last vote was persisted.

    If there is a way to indicate voting period is over, you could have a 2nd function that runs the calculations from the data stored by processing voting messages. One thing to watch out for is how the 1st function that accepts the voting messages stores the data. If the data is stored in append-only mode, you’re good. If you’re trying to keep a counter only, you’ll have contention and don’t recommend that approach. Append only is a more efficient approach.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search