davidfowl / AspNetCoreDiagnosticScenarios

This repository has examples of broken patterns in ASP.NET Core applications
7.65k stars 736 forks source link

CPU consumption while using Task.Run vs Background thread #83

Closed bmwhite20 closed 2 years ago

bmwhite20 commented 2 years ago

Hi David,

Hope you are doing well!

Following your (awesome) async guidance, I'm having a strange behavior while testing a long-running message consumer with Task.Run vs Background Thread.

Code sample:

        public async Task MainAsync(string[] args)
        {
            var backgroundThread = new Thread(Consume)
            {
                IsBackground = true
            };

            backgroundThread.Start(); //CPU - NOK

            await Task.Run(Consume); //CPU - OK
        }

        private void Consume()
        {
            //while loop (sync) consuming messages from Kafka
        }

Test conditions:

Background thread image

Task.Run image

As we can see, pods running with background thread are struggling the CPU.

Do you have any thoughts why it happens?

Code: https://github.com/bmwhite20/cpu-workload

Best, Bruno

newbe36524 commented 2 years ago

As I understand it, it is a matter of Kafka's own implementation. If you want to understand the CPU difference between the two ways of writing code. Then Kafka should not be introduced.

As for Kafka itself, it is actually recommended to use Consume with Timeout, and I think this case may be related to your question. https://github.com/confluentinc/confluent-kafka-dotnet/issues/1261

bmwhite20 commented 2 years ago

It's definitely related with Kafka