I'm introducing myself into Event-driven architecture using MassTransit with RabbitMQ on localhost and AWS on live environments, using .NET 5 Web APIs.
I have an API (#API-1) which produces events, such as EntityCreated, EntityUpdated, EntityDeleted, etc.
Then I have another API (#API-2) which stores an in-memory copy of entities. This #API-2 must subscribe to those events in order to update its local cache. The #API-2 will have multiple running instances, let's say 3.
Producer Configuration (#API-1):
services.AddMassTransit(x =>
{
x.UsingRabbitMq();
});
services.AddMassTransitHostedService();
Consumer Configuration (#API-2):
services.AddMassTransit(x =>
{
x.AddConsumer<EntityCreatedConsumer>();
x.AddConsumer<EntityUpdatedConsumer>();
x.AddConsumer<EntityDeletedConsumer>();
x.UsingRabbitMq((context, cfg) =>
{
cfg.ConfigureEndpoints(context);
});
});
services.AddMassTransitHostedService();
You need to use different endpoint names. Each endpoint gets its own queue and binding, so if you use the same endpoint name, you get one queue and all instances started to compete for messages.
It is a common mistake, that's why it's even mentioned in the documentation.
Thanks for pointing that out. I assume I should suffix the queue name with some unique identifier, such as the MachineName. Is this a good practice? Moreover, I believe I should mark those Queues as AutoDelete so that they are removed if an instance is turned off.