Search code examples
c#masstransit

Masstransit using 30% CPU when idle


I've been searching for an answer for several days now to find out why my C# MassTransit configuration is using so much CPU when it is idle.

I found very few responses to other users asking this same question, and mostly they state "you must be doing something wrong". Problem is, I've followed the examples on the MassTransit website and even done a bare bones project and still see it using 30+% cpu when it is just idle.

Is there something that can be done to make it use 0-10% when idle? In our environment running a process that will consume 30+% cpu at idle is significant.

Here is an example of how I have MT configured in my dotnet 7 app:

    .ConfigureServices((host, services) =>
    {

    IAgentInformation agentInformation = new AgentInformation(host.Configuration);

    services.AddMassTransit(x =>
    {
        x.SetKebabCaseEndpointNameFormatter();

        var entryAssembly = Assembly.GetEntryAssembly();

        x.AddConsumer<AgentConsumer>();
        x.AddSagaStateMachines(entryAssembly);
        x.AddSagas(entryAssembly);
        x.AddActivities(entryAssembly);

        x.UsingRabbitMq((context, cfg) =>
        {
            cfg.Host(agentInformation.ATLASServer, "/", h =>
            {
                h.Username(agentInformation.RabbitId);
                h.Password(agentInformation.Rabbit);
            });

            cfg.ConfigureEndpoints(context);
        });
        services.AddHostedService<Worker>();
    });
    });

And when consuming:

    public class AgentConsumer : IConsumer<ServerMessageContract>
{
    readonly ILogger<AgentConsumer> _logger;

    public AgentConsumer(ILogger<AgentConsumer> logger)
    {
        _logger = logger;
    }

    public Task Consume(ConsumeContext<ServerMessageContract> context)
    {
        _logger.LogInformation("Received Message: {Text}", context.Message.Payload == null ? "" : context.Message.Payload.ToString());
        TaskRunner.CommandsFromServer(context.Message);

        return Task.CompletedTask;
    }
}

And when publishing:

var endpoint = await _bus.GetSendEndpoint(new Uri($"rabbitmq://{ServerAddress}/{SendQueue}"));
await endpoint.Send(agentMessage, stoppingToken);

Pretty straight forward....

EDIT: The problem was with another library we created in-house that was spiking the CPU. By process of elimination we found the issue.


Solution

  • Well, I don't want to echo what you've heard elsewhere, but it's something on your end.

    CONTAINER ID   NAME                CPU %     MEM USAGE / LIMIT     MEM %     NET I/O           BLOCK I/O         PIDS
    09155c5ce2d1   outbox-postgres-1   0.12%     75.51MiB / 47.06GiB   0.16%     95.8kB / 107kB    47.3MB / 47.5MB   8
    6ad4b6151e35   outbox-rabbitmq-1   0.51%     149.1MiB / 47.06GiB   0.31%     10.9kB / 8.23kB   57.3MB / 778kB    46
    317dc978e517   outbox-api-1        0.55%     89.48MiB / 47.06GiB   0.19%     96.3kB / 89.8kB   34.9MB / 0B       29
    8dda648a87ad   outbox-jaeger-1     0.05%     10.66MiB / 47.06GiB   0.02%     1.47kB / 0B       29.7MB / 0B       16
    f01392265691   outbox-service-1    0.00%     67.85MiB / 47.06GiB   0.14%     21.8kB / 14.1kB   62.1MB / 0B       16
    

    Both outbox-api and outbox-service are running MassTransit against RabbitMQ with a whole bunch of endpoints. As you can see, nearly idle.

    This is from the Sample-Outbox project.