#24 Golang - Concurrency: Unbuffered vs Buffered Channels

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] today we're diving into an important concept of concurrency buffered and unbuffered channels channels in go are a powerful feature for communication between G routines they allow us to pass data from one go routine to another ensuring synchronized execution and avoiding common pitfalls like race conditions but not all channels are created equally there are two types unbuffered and buffered unbuffered channels are the most straightforward type of Channel when you send a value into an unbuffered channel the send operation blocks until another go routine is ready to receive the value similarly when you receive a value from an unbuffered channel the receive operation blocks until there's data to be received suppose these two Gophers represent two goroutines they both are busy with their jobs goroutine 1 is ready to send data on the channel but the other goroutine is busy so goroutine 1 Waits patiently as soon as the other gartine is ready to read the data the data exchange happens on the channel then they both go back to their respective jobs this is the concept of an unbuffered Channel buffered Channel on the other hand allow values to be stored in the channel up to a certain capacity this capacity is defined when you create the Channel with a buffered channel the send operation blocks only when the channel is full and the receive operation blocks when the channel is empty here again we have two G routines busy doing their jobs there is a buffered Channel between them of size four goroutine 1 wants to send something on the channel it places the value in the cube and goes back to its job without blocking it again has something and adds to the channel when goroutine 2 reads the channel it receives the value this is how buffered channels work to make an unbuffered Channel we can use a construct like this this is the same as passing zero as the length of the channel the only difference that the Bufford Channel brings is the nonzero length of the channel let's use a coffee shop analogy Suppose there is a coffee shop where a customer waits to place an order until the Barista is ready to accept and prepare the order the Barista can only start preparing an order when it is received which means the system can handle only one order at a time let's see how this looks as a code here is the main function We Begin by creating an unbuffered Channel called orders of typ string this go routine represents customers placing orders and this code here is like a barista processing orders and brewing coffee for customers in this G routine we run a loop simulating five customers an order is created it is a string containing this text with order number at the end then the order is placed on the channel this goutine waits till the Barista is ready to accept an order this makes the other customers wait too as soon as the Barista accepts the order we print the statement and go to the next customer once all customer orders have been placed we close the channel now let's see how the code for Barista works here the Barista Waits for an order by reading the channel this is a blocking call as soon as he receives the order he starts preparing the coffee this sleep here simulates the time taken to prepare coffee for the customer next the coffee gets served to the customer now the Barista goes back to look for another order this Loop breaks when the channel is closed in the customer go routine let's run the program here the first order gets placed and it immediately got accepted as the Barista was free here the coffee is ready and gets served then comes the order number two the processing statement comes before the order placed print as they happen almost at the same time in different goroutines as soon as the order gets placed the Barista starts processing the order and then the order gets served and so on now let's look at the buffer Channel counterpart in this scenario customers can place their orders at a counter and wait elsewhere allowing other customers to place their orders even if the Barista is still busy the Barista processes orders from the counter as they come in but has the flexibility to work through a backlog of up to three orders this allows the Barista to manage order preparation more efficiently especially during surges as the system is not entirely halted by the preparation time of each order we made only one change in the code this channel is replaced with a buffered Channel with size three now placing an order blocks only when the buffer is full the rest of the code Remains the the same here the first three orders get placed without any weight then the buffer gets full as soon as the preparation of the first order starts the fourth order is placed here we saw that the customers would get blocked only when the buffer was full similarly the Barista will wait if the buffer is empty now we understand the difference between unbuffered and buffered channels let's see what are the advantages of using buffered channels it decou couples the producer and the consumer by putting a queue between them it is great for handling bursts of traffic again by providing a buffer by unblocking the G routines it improves throughput these properties of buffered channels make them useful for worker pools and for asynchronous signaling and there you have it a comprehensive guide to understanding buffered and unbuffered channels in golang remember the choice between them should be guided by your application specific requirements for synchronization and performance if you enjoyed this tutorial don't forget to like share and subscribe for more programming insights until next time happy coding
Info
Channel: codeHeim
Views: 2,520
Rating: undefined out of 5
Keywords:
Id: uwEfn_yMplE
Channel Id: undefined
Length: 7min 47sec (467 seconds)
Published: Tue Feb 20 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.