Monday, 3 June 2024

Multiple KEDA Triggers for a Scaled Job with Event Hubs in AKS

 Kubernetes scaled job helps us running one job per event/message we recive from the queue/even hub. We can have an event handler job which can handle more than one type of event messages or  queue messages. Let's look at what we need to consider when we are defining more than one trigger, with kubernetes event drivern autoscaler (KEDA) for a scaled job.

The trigger definition can be as shown below.

triggers:
    - type: azure-eventhub
      metadata:
        consumerGroup: orderhandler
        unprocessedEventThreshold: "1"
        activationUnprocessedEventThreshold: "0"
        blobContainer: orderhandler-neworder
        eventHubNamespace: myeventhubnamespace
        eventHubName: neworder
        storageAccountName: stdev001ehnblue
        checkpointStrategy: blobMetadata
      authenticationRef:
        name: keda-trigger-auth
    - type: azure-eventhub
      metadata:
        consumerGroup: orderhandler
        unprocessedEventThreshold: "1"
        activationUnprocessedEventThreshold: "0"
        blobContainer: orderhandler-updateorder
        eventHubNamespace: myeventhubnamespace
        eventHubName: updateorder
        storageAccountName: stdev001ehnblue
        checkpointStrategy: blobMetadata
      authenticationRef:
        name: keda-trigger-auth

When we more than one trigger, how we need to define a strategy to identify how many job instances should be created when events are recevied in one or in both event hubs.

For that in scaling strategy we can define multipleScalersCalculation parameter.

scalingStrategy:
    strategy: "default"
    multipleScalersCalculation : "sum"

KEDA documentation here provides clear explanation on how the multipleScalersCalculation behaves.

If you implment your scaled job logic to only process one event per job, the ideal value for the multipleScalersCalculation would be sum. In addition to that setting the scaled job minReplicaCount as 0 is recommended due to, when  no events there should not be any running jobs. See example below (note that pod template is removed for brevity).


spec:
  jobTargetRef:
    parallelism: 1
    completions: 1
    activeDeadlineSeconds: 600
    backoffLimit: 6
    template:
      # job pod template goes here
  pollingInterval: 5
  successfulJobsHistoryLimit: 20
  failedJobsHistoryLimit: 20
  minReplicaCount: 0
  maxReplicaCount: 10
  rollout:
    strategy: gradual
    propagationPolicy: foreground
  scalingStrategy:
    strategy: "default"
    multipleScalersCalculation : "sum"
  triggers:



  In scaled job implmentation, it would be useful to setup logic to wait for a new event, for a given timeout, say 60 seconds for example and make the job terminated after timeout. this is necessary due to KEDA may scale addtional jobs (mostly 1 addtional job when multiple events are received), beyond number of the events received in the event hubs or the queue. Any additional job should terminate if there is nothing to process within a given timeout period.

No comments:

Popular Posts