Search code examples
azurekubernetesevent-handlingazure-eventhubkubernetes-pod

Avoid consuming same events parallel from EventHub


I'm using:

  • Azure platform to run some microservice architecture software solution.
  • microservices are using the Azure-EventHub for communicating in special cases.
  • Kubernetes with 2 clusters (primary, secondary)
  • per application namespace, there is 1 event-listener pod running per cluster for consuming from eventhub

The last point is relevant to my current problem: The load balancers will share traffic between the primary and secondary clusters. This means that 2 event-listener-pods are running per application at the same time. So they are just reacting to events but some times they are consuming the same event from the event hub and this causes some duplicated notification mails.

So finally my question is: How can I avoid reading the same event twice the same time? I thought event hub index is always increasing but starting at the same moment is not "secured".


Solution

  • You will need to use separate consumer groups per pod to avoid EPOCH error.

    That said, both pods will read the same events, so you have two options.

    1. Have an active-passive set up. One consumer group, one pod that reads the events and delegates the work out on each event. If that pod fails, then a health/heart beat mechanism brings the second pod online.

    2. Have an active-active set up. Two consumer groups, two active pods. You will need to implement idempotent processing.

    Idempotent processing, where processing the same message multiple times produces the same result, is good practice regardless of approach. This would allow you to replay batches of events in which one errored and not have adverse affects on the integrity of your data.

    I would opt for the first option, a single event hub reader will process thousands of events per second and pass off the work to your micro services.

    If you have lower volumes of messages and need guaranteed message processing, then using Service Bus may be a better choice where messages can be locked, completed and abandoned.