Search code examples
c#cpu-usagemasstransit

How does MassTransit consumes CPU


I am using https://masstransit.io/ library and sending 2000 messages uses 100% CPU for 15 minutes in average.

First, I thought it was due to the job requested by the message, but then I have tried without any job and just sending 2000 messages gave the same result.

Is that normal ? How does MassTransit consumes CPU ?

Messages were sent with Azure Function that called following method:

public async Task DoJob(string contextName, RequestDto request) 
{
    using var provider = _publishEndpointProvider.Build();
    var publishEndpoint = provider.Get();

    foreach (var reference in references)
    {
        await publishEndpoint.Publish(new ProcessMessage
        {
            MessageCorrelationId = Guid.NewGuid().ToString(),
            MessageCreationTimestamp = DateTimeOffset.UtcNow,
            ContextName = contextName,
            StartDate = request.StartDate,
            EndDate = request.EndDate,
            Reference = reference
        }).ConfigureAwait(false);
    }
}

Many thanks, AK


Solution

    1. Don't await every single call to publish in a loop, you're basically creating a massive wait for the broker to acknowledge the message. That's a lot of dead time doing nothing but waiting on the broker.

    2. No clue what's behind your Build function, but I know it can't be good. In fact, I really know it can't be good. It can be terrible in fact. I'd suggest using the sample as a guideline of how to publish messages using MassTransit in an Azure Function.

    Good luck!