How to Enable Message Partitioning in Spring Cloud Stream for Consistent Consumer Routing

This guide explains how to configure message partitioning in Spring Cloud Stream by adjusting consumer and producer properties—such as group, destination, partitioned flag, instanceCount, instanceIndex, partitionKeyExpression, and partitionCount—to ensure that messages with the same characteristics are consistently processed by the same instance across multiple service instances.

Programmer DD
Programmer DD
Programmer DD
How to Enable Message Partitioning in Spring Cloud Stream for Consistent Consumer Routing

After learning from the previous article about message‑driven microservices, we can ensure that a message is consumed by only one instance in a multi‑instance environment. For scenarios where messages sharing certain attributes should always be processed by the same consumer instance, message partitioning is required.

Configuring Message Partitioning in Spring Cloud Stream

Consumer side

Modify the SinkReceiver application’s configuration to add the following settings:

spring.cloud.stream.bindings.input.group=Service-A
spring.cloud.stream.bindings.input.destination=greetings
spring.cloud.stream.bindings.input.consumer.partitioned=true
spring.cloud.stream.instanceCount=2
spring.cloud.stream.instanceIndex=0

Explanation of the parameters: spring.cloud.stream.bindings.input.consumer.partitioned=true: enables partitioned consumption. spring.cloud.stream.instanceCount=2: total number of consumer instances. spring.cloud.stream.instanceIndex=0: index of the current instance (starts at 0, must be less than instanceCount).

Producer side

In the SinkSender application, add the following bindings:

spring.cloud.stream.bindings.output.destination=greetings
spring.cloud.stream.bindings.output.producer.partitionKeyExpression=payload
spring.cloud.stream.bindings.output.producer.partitionCount=2

Parameter details:

spring.cloud.stream.bindings.output.producer.partitionKeyExpression=payload

: defines the SpEL expression used to compute the partition key from the message payload.

spring.cloud.stream.bindings.output.producer.partitionCount=2

: sets the number of partitions for the destination.

Running the services

Start both the producer and the consumer applications. When multiple consumer instances are launched, assign each a different instanceIndex. With the same message sent to the consumer group, only the instance whose partition matches the computed key will receive and process the message, ensuring consistent handling of related messages.

Message partitioning illustration
Message partitioning illustration
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

MicroservicesConfigurationconsumer-groupSpring Cloud StreamMessage Partitioning
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.