Recommended way to implement a Channel consumer with async handlers & backpressure?
Hey everyone 👋
I’m working on a microservice (DDD style) where aggregates communicate through an in-memory Channel (System.Threading.Channels).
Producers push domain messages/events into it, and a background consumer runs handlers for each message.
I want to keep backpressure (so producers block if the consumer is behind) but still handle multiple messages asynchronously — without serializing everything or flooding the system.
Here’s the trade-off I’m running into:
20 Replies
seems like you want to implement a concurrency limit so only N messages can be handled at once
yes, but to set static amount of workers seems limitting given if its deployed to cloud it can scale and i wont be able to optimize the ideal amount of workers
then make it dynamic
okay and would u advise to do something like Environment.ProcessorCount
and multiply that by some constant ? to get roughly the amount of threads we want
you're not controlling threads here, you're controlling tasks
they use whatever thread pool threads are available when there is CPU bound work to be done
okay so that means i could just use the _ = Task.Run(() => HandleAsync(msg)); and when threads are available they do the work ?
my worry here is that if we enqueue too many tasks wont there be an issue with context switching ?
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
so dont use in mem channel for communication inside of a microservice but use external message bus also ?
meaning that microservice would consume its own events?
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
okay i mean that can actually simplify my code logic, i ditch the channel completely and subscribe it to its own events ; because for communication between microservices i still use service bus
so its easier to use it for internal stuff also
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
i get what u mnea stuff like paymentprocessed shouldnt be stored in memory channel because if the app goes down i am screwed
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
might do that or just directly from handler commit to outbox and send to service bus
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
via bg service
what do u mean
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
Opinion: first article looks better as an introduction than the devblog one, but but should still be read in that order
https://github.com/tkp1n/ndportmann.com/blob/master/posts/2019-01-03--system-threading-channels/index.md
https://web.archive.org/web/20221129052633/http://ndportmann.com/system-threading-channels/
https://devblogs.microsoft.com/dotnet/an-introduction-to-system-threading-channels
https://www.stevejgordon.co.uk/an-introduction-to-system-threading-channels
Video:
https://docs.microsoft.com/en-us/shows/on-net/working-with-channels-in-net
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
thanks for the help, i will be using external busses even for in proc communication then
given i already use them