This project demonstrates how to use RabbitMQ's consistent hash exchange to distribute messages across multiple queues based on a hash of a message property, ensuring that messages with the same hash key always go to the same queue.
The consistent hash exchange is a RabbitMQ exchange type that routes messages to queues based on a consistent hash algorithm. This is particularly useful when you need to:
- Ensure message ordering for related messages (e.g., all messages for a specific account)
- Distribute load evenly across multiple consumer queues
- Maintain session affinity or stateful processing
In this example, messages are routed based on an accountId header, ensuring that all messages for the same account are always processed by the same queue and consumer.
- .NET 8.0 SDK - Download here
- RabbitMQ Server - Installation Guide
- Default configuration (localhost, guest/guest credentials)
- Consistent hash exchange plugin must be enabled (usually enabled by default)
Make sure RabbitMQ is running on your local machine with default settings:
- Host:
localhost - Username:
guest - Password:
guest - Port:
5672(AMQP)
dotnet builddotnet run --project RabbitConsistentHashThe application creates:
- 1 Exchange:
account-hash-exchange(type:x-consistent-hash) - 10 Queues:
queue.account.0throughqueue.account.9 - Hash Header: Messages are routed based on the
accountIdheader
-
Publisher (
Publisher.cs):- Generates random account IDs
- Publishes messages to the consistent hash exchange
- Includes the
accountIdin the message headers
-
Consistent Hash Exchange:
- Calculates a hash of the
accountIdheader value - Routes the message to one of the 10 bound queues
- Ensures messages with the same
accountIdalways go to the same queue
- Calculates a hash of the
-
Subscribers (
Subscriber.cs):- Each queue has a dedicated consumer
- Processes messages and logs which queue received each message
- Shows the account ID for traceability
RabbitConsistentHash/
├── Program.cs # Main application entry point
├── Publisher.cs # Message publishing logic
├── Subscriber.cs # Message consuming logic
└── RabbitConsistentHash.csproj
- Program.cs: Sets up RabbitMQ connection, declares the exchange and queues, starts consumers, and runs the publishing loop
- Publisher.cs: Contains the
Publishmethod that sends messages withaccountIdheaders - Subscriber.cs: Contains the
Subscribemethod that consumes messages from a specific queue
When running the application, you'll see output similar to:
Published: 'This is a test message for account 12345678-1234-1234-1234-123456789abc' for accountId: 12345678-1234-1234-1234-123456789abc
[Queue 3] Received: This is a test message for account 12345678-1234-1234-1234-123456789abc for accountId: 12345678-1234-1234-1234-123456789abc
Published: 'This is a test message for account 87654321-4321-4321-4321-cba987654321' for accountId: 87654321-4321-4321-4321-cba987654321
[Queue 7] Received: This is a test message for account 87654321-4321-4321-4321-cba987654321 for accountId: 87654321-4321-4321-4321-cba987654321
Notice how messages with the same accountId consistently go to the same queue number.
You can modify the following parameters in Program.cs:
- Exchange Name: Change
exchangeNamevariable - Queue Count: Modify
queueCountto create more or fewer queues - Hash Header: Update the exchange arguments to use a different header for hashing
- Message Interval: Adjust the
Task.Delayvalue to change publishing frequency
- Message Ordering: Messages for the same hash key (accountId) maintain order
- Load Distribution: Messages are distributed evenly across available queues
- Scalability: Easy to add or remove queues without major redistribution
- Fault Tolerance: If a queue becomes unavailable, only messages for specific hash keys are affected
- Ensure RabbitMQ server is running
- Check that the default guest/guest credentials are enabled
- Verify the management plugin is installed for web UI access
- Ensure the consistent hash exchange plugin is enabled:
rabbitmq-plugins enable rabbitmq_consistent_hash_exchange
- RabbitMQ.Client: Version 7.1.2 - Official RabbitMQ .NET client library
This project is provided as an educational example for demonstrating RabbitMQ consistent hash exchange functionality.