Load Balancing by Message Broker
Context
Microservices are being adopted. Microservices can be deployed with a configurable amount of instances. There are mechanisms to discover service instances. There is publish-subscribe based communication with the microservice.
Problem
- The dynamic deployment environment sometimes leads to communication errors since some microservice instance was relocated or teared down
- There is an uneven load distribution between the instances of a microservices. In an extreme case all requests just go to one microservice.
- Sent events are processed n times at consumer-side, where n is the number of instances of the consuming microservice
Solution
Use a load balancing mechanism of the message broker to distribute the load between microservice instances. Instead of delivering the message to all subscribers (instances of a microservice), the broker should deliver the message to only one subscriber (instance of a microservice). We call such a shared subscription a consumer group.
If all instances of a microservice form a consumer group, the load will be automatically balanced between those instances regarding the metrics configured in the message broker.
When using message brokers, message brokers usually come with their own service instance discovery mechanism since all microservice instances have to register for receiving any message and are thus already known.
Maturity
Proposed, requires evaluation.
Sources of Evidence
L53:
- Message broker of MQTT protocol is similar as load balancing proxy for web services / API gateway
- load balancing can be facilitated by binding multiple event consumers to the same event channel
- events dispatched among consumers instead duplicates
- Name resolution policies and API gateways can combine to decouple event producer from consumer
L59:
- need for messaging and queuing services to discover instances and route calls to correct instances
LM48:
- Context: microservice migration describes an examples project (FX Core) and compares back to monolith
- Load balancing implemented by DockerSwarm and by RabbitMQ
- RabbitMQ by distributing messages between subscribers to a queue => spreads load
- all replicas of service subscribe to same queue
- round-robin between them, distribute load between instances in sequential order
- acknowledgements => at-least-once delivery
- can also deliver in different way if load balancing not needed
- Fig 6: illustration of load balancing by queue