Share the love

KEDA may be the only option to use when you have multiple microservices that are processing messages from different message brokers such as Kafka, rabbitmq, sqs etc. If each service has different message rate and you want to autoscale them independently, read on.

Here is an example YAML file that demonstrates how to configure KEDA for this scenario:

apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
  name: my-microservice-1-scaledobject
  namespace: my-namespace
spec:
  scaleTargetRef:
    kind: Deployment
    name: my-microservice-1-deployment
  pollingInterval: 10
  cooldownPeriod: 300
  triggers:
  - type: rabbitmq
    metadata:
      queueName: my-queue-1
      connection: amqp://user:password@rabbitmq-service:5672/
    scaleDown:
      threshold: 0
    scaleUp:
      threshold: 10
---
apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
  name: my-microservice-2-scaledobject
  namespace: my-namespace
spec:
  scaleTargetRef:
    kind: Deployment
    name: my-microservice-2-deployment
  pollingInterval: 10
  cooldownPeriod: 300
  triggers:
  - type: kafka
    metadata:
      topic: my-topic-2
      brokerList: kafka-service:9092
    scaleDown:
      threshold: 0
    scaleUp:
      threshold: 20

In this example, KEDA is set to monitor the message rate of the my-queue-1 rabbitmq queue for the my-microservice-1-deployment deployment in the my-namespace namespace. The pollingInterval is set to 10 seconds and the cooldownPeriod is set to 300 seconds. When the message rate exceeds 10, KEDA will scale up the number of replicas of the deployment, and when the message rate falls below 0, KEDA will scale down the number of replicas.

Similarly for my-microservice-2-scaledobject, KEDA is set to monitor the message rate of the my-topic-2 kafka topic for the my-microservice-2-deployment deployment in the my-namespace namespace. The pollingInterval is set to 10 seconds and the cooldownPeriod is set to 300 seconds. When the message rate exceeds 20, KEDA will scale up the number of replicas of the deployment, and when the message rate falls below 0, KEDA will scale down the number of replicas.

In this way, you can use KEDA to autoscale multiple microservices independently based on the message rate from different message brokers.