Josh Wilson
Josh Wilson

Reputation: 3745

Overhead of go routines in golang

So I understand go routines have low overhead, but I'm wondering just how good they are. If I have a server that handles incoming messages how does creating a new go routine to process each incoming message compare to using the standard producer/consumer model with channels?

Like is it reasonable to have a high performance go server that spawns new go routines for ever incoming request.

Upvotes: 2

Views: 5367

Answers (2)

Raj Kumar
Raj Kumar

Reputation: 1587

We were actually doing stress testing with a similar approach. We spawned new threads for every http request. The concurrency was so good than within 10 seconds we had reached something like 100,000 requests. The only bottle neck you might face is memory because if all the processing isn't fast enough then you might run out of memory for that process.

I'm quite sure there is a work around for it, but that is one reason why you would want to throttle and nor spawn unlimited go routines. The problem we faced is we were trying to get data from another API which was just not able to keep up with this level of concurrency. Hence the throttling suggestion earlier.

Upvotes: 5

kichik
kichik

Reputation: 34704

The built-in http package uses a goroutine for each connection and there are numerous benchmarks showing it being able to handle thousands of concurrent users. So unless you have many connections with many messages each, I'd say it's reasonable to create a new goroutine for each message. And in any case, go has a nice benchmarking feature you can use to verify your assumptions.

https://golang.org/src/net/http/server.go#L2137

Upvotes: 4

Related Questions