8

I understand from this question "Golang - What is channel buffer size?" that if the channel is buffered it won't block.

c := make(chan int, 1)
c <- data1 // doesn't block
c <- data2 // blocks until another goroutine receives from the channel
c <- data3
c <- data4

But I don't understand whats the use of it. Suppose if I have 2 goroutines, 1st one will received data1 and 2nd one receives data2 then it will block till any subroutines gets free to process data3.

I don't understand what difference did it make ? It would have executed the same way without buffer. Can you explain a possible scenario where buffering is useful ?

Community
  • 1
  • 1
Rahul Prasad
  • 8,074
  • 8
  • 43
  • 49

2 Answers2

9

A buffered channel allows the goroutine that is adding data to the buffered channel to keep running and doing things, even if the goroutines reading from the channel are starting to fall behind a little bit.

For example, you might have one goroutine that is receiving HTTP requests and you want it to be as fast as possible. However you also want it to queue up some background job, like sending an email, which could take a while. So the HTTP goroutine just parses the user's request and quickly adds the background job to the buffered channel. The other goroutines will process it when they have time. If you get a sudden surge in HTTP requests, the users will not notice any slowness in the HTTP if your buffer is big enough.

David Grayson
  • 84,103
  • 24
  • 152
  • 189
  • 4
    I'd add to this by remarking that a good rule of thumb is that **buffering is a tool to improve performance**. Never make the mistake of trying to avoid deadlock etc only by using buffering, because that's futile. If your app can't deadlock without buffering, it won't deadlock when buffering is added ... but it might run faster. Design a simple deadlock-free unbuffered network first and then tune the performance. – Rick-777 Apr 01 '14 at 22:21
  • Buffered channels are not for performance. They are for flow control. – Inanc Gumus Jun 18 '22 at 07:41
  • You can get flow control without using a buffer, I think the buffering is for performance. If your system is under a lot of load, having buffered channels would allow a goroutine to run for a long time, pulling lots of input from its input channels and dumping lots of output to its output channels before the system has to switch to running another goroutine. Doing things in big batches instead of small batches should improve performance because of the caching the CPU does. – David Grayson Jun 18 '22 at 07:47
  • Even without heavy load, there is the possibility of quickly queueing jobs to run later by pushing them to a buffered channel, which means the Goroutine queing the jobs will finish its task (e.g. handling an HTTP request) faster. – David Grayson Jun 18 '22 at 07:53
1

This site has a good explanation:

https://www.openmymind.net/Introduction-To-Go-Buffered-Channels/

David Grayson
  • 84,103
  • 24
  • 152
  • 189
Evan
  • 6,369
  • 1
  • 29
  • 30