Slide 1

Slide 1 text

Working with Channels in Alper Hankendi

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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#.

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

CreateBounded: 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

Slide 9

Slide 9 text

CreateUnbounded : 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

Slide 10

Slide 10 text

10 Built-In Channel Implementations

Slide 11

Slide 11 text

11 Writing to a Channel

Slide 12

Slide 12 text

12 Reading From a Channel

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

14 With ‘MyChannel’ Implementation

Slide 15

Slide 15 text

15 With dotnet Channels

Slide 16

Slide 16 text

16 Benchmark Custom Imp vs. Channels

Slide 17

Slide 17 text

17 Channel Samples source: https://github.com/stevejgordon/ChannelSample

Slide 18

Slide 18 text

18 Channel Samples source: https://github.com/stevejgordon/ChannelSample

Slide 19

Slide 19 text

19 Channel Samples source: https://github.com/stevejgordon/ChannelSample

Slide 20

Slide 20 text

20 Channel Samples source: https://github.com/stevejgordon/ChannelSample

Slide 21

Slide 21 text

21 RECAP

Slide 22

Slide 22 text

22 Use Case; async data pipelines

Slide 23

Slide 23 text

23 Async Data Pipelines

Slide 24

Slide 24 text

24 Dealing With Back pressure

Slide 25

Slide 25 text

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 and Merge 25 Dealing With Back pressure

Slide 26

Slide 26 text

26 Dealing With Back pressure- Multiplexer Split 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.

Slide 27

Slide 27 text

Merge is the opposite operation. It takes multiple input channels and consolidates them in a single output. 27 Dealing With Back pressure - Demultipler

Slide 28

Slide 28 text

28 Async Data Pipelines

Slide 29

Slide 29 text

29 Questions ? @alper_hankendi github.com/alperhankendi