Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Working with Channels in .net core

Working with Channels in .net core

Dotnet Konferansı 2021

Alper Hankendi

February 20, 2021
Tweet

More Decks by Alper Hankendi

Other Decks in Programming

Transcript

  1. 20 2019 KrakenD API Gateway About Me Working as Product

    Engineering Director at Hepsiburada 20 Years Experience… approximately (professional) Ninja Developer Like to build useless stuff Drummer, Brewer and…. Father @alper_hankendi github.com/alperhankendi
  2. 33 2020 Authorization granularity • What is a Channels •

    Why use Channels • Bounded/Unbounded Channels • Writing Channels • Reading Channels • Channel Types • Benchmark • Dealing with back pressure • Async data pipelines AGENDA
  3. 4 What is A Channel ? A channel is a

    data structure that allows one thread to communicate with another thread. In .NET, this was usually done by using a shared variable that supports concurrency (by implementing some synchronization/locking mechanism). Channels, on the other hand, can be used to send messages directly between threads without any external synchronization or locking required. Logically a channel is effectively an efficient, thread-safe queue. The API is designed to be Used with async/await in C#.
  4. 5 What is A Channel ? Consumer task which performs

    the processing steps for each message. It reads from the channel, processing each message in turn. A benefit of this approach is that my producer/consumer functionality has now been separated and data can be passed via the channel. Create more producer or consumer tasks to achieve higher throughput
  5. 6 Why Use System.Threading.Channels - “Producer/Consumer” problems are everywhere, in

    all facets of our lives. - When it comes to multi-threading, one of the first pattern that emerges in the producer-consumer pattern - It is a powerful tool for simplifying many producers & consumers patterns in .NET. - Anytime you need to exchange items between “Tasks” you will find channels is a pretty convenient and straightforward way to get started. - Channels enable concurrency patterns
  6. 7 Channels Enable Concurrency Pattern The relationship between concurrency and

    parallelism is commonly misunderstood. In fact, two task being concurrent doesn’t mean that they’ll run in parallel. The following quote by Martin Kleppmann has stood out in my mind when it comes to concurrency: google “youtube: Concurrency is not parallelism by Rob Pike For defining concurrency, the exact time doesn’t matter: we simply call two operations concurrent if they are both unaware of each other, regardless of the physical time at which they occurred. “Designing Data-Intensive Applications” by Martin Kleppmann
  7. CreateBounded<T>: creates a channel with a finity capacity. In that

    case, it’s possible to develop a producer/consumer pattern which accommodates this limit. Attempts to write the channel when it contains the maximum allowed number of items result in behavior according to the “mode” specified when the channel is created 8 Channel Factory & Bounded Channels
  8. CreateUnbounded<T> : creates a channel with an unlimited capacity. This

    can be quite dangerous if your producer outpaces you the consumer. The channel will keep accepting new items. When the consumer is not keeping up, the number of queyed items will keep increasing. It’s possible to run out of available memory in this scenario. 9 Unbounded Channels
  9. Testing the throughput and memory allocation on an unbounded channel

    when writing an element and then reading out that element a million times 13 My Channel vs Channel
  10. The stage where we retrieve the license file from github

    might cause experience backpressure because retrieving the license is slower than ‘Read File stage’. It makes sense to increase the capacity of ‘Get License stage’ and that’s where Split<T> and Merge<T> 25 Dealing With Back pressure
  11. 26 Dealing With Back pressure- Multiplexer Split<T> is a method

    that takes an input channel and distributes its messages amongst several outputs. That way we can let several threads concurrently handle the message processing.
  12. Merge<T> is the opposite operation. It takes multiple input channels

    and consolidates them in a single output. 27 Dealing With Back pressure - Demultipler