Junior — Middle
What is the maximum number of goroutines that can be launched simultaneously without significant problems for the system?
sobes.tech AI
Answer from AI
In Go, the maximum number of goroutines that can run simultaneously depends on system resources (RAM, CPU time) and the nature of the tasks they perform. Theoretically, hundreds of thousands or even millions of goroutines can be launched, as they are very lightweight compared to OS threads.
However, in practice:
- If goroutines actively use the CPU, too many of them can lead to decreased performance due to context switching.
- If goroutines block on I/O or wait for events, their number can be much larger without significant issues.
Typically, in real applications, launching from a few hundred to several thousand goroutines is not problematic. When scaling, it is advisable to profile the application and monitor memory consumption and CPU load.
Example of creating a large number of goroutines:
for i := 0; i < 100000; i++ {
go func(n int) {
// some work
fmt.Println(n)
}(i)
}