This post gives an answer to the question: why Go is optimized for concurrent I/O-intensive systems such as network services.
A programming language is not only syntax, but also a runtime implementation. Runtime is the part of the executing program where data structures and algorithms that support syntactic constructs of the language are implemented. Namely, the implementation of dictionaries (maps), sets, asynchrony, parallelism, and concurrency (which are not the same thing).
Golang is unique, and for the 2010s revolutionary, in its implementation of the Syntax + Runtime combination for an ARCHETYPICAL program design. An archetypical Go program is a server running in a single OS process on multiple OS threads, which handles a flood of HTTP requests while maximizing the utilization of OS threads.
As is known, the execution of a program’s algorithm can be blocked algorithmically (something is waiting for a response), or physically when an OS thread waits for I/O. In Go, both types of blocking are reduced to a minimum through the implementation of a virtual processor layer and lightweight goroutines. In this way, virtual processors are switched between OS threads, and goroutines that are waiting and ready to execute are shuffled, so that the real CPU is practically not idle. And the entire mechanism is described with simple and clear syntax.
Thus, Go is designed for writing programs that, within a single OS process, process the maximum number of requests above the Transport Layer in the most optimal way. The archetypical program design is: a listening goroutine that launches handler goroutines for each connection. Everything else is delegated to the runtime. And the runtime, in turn, manages the execution sequence of goroutines across different virtual processors and different OS threads.
// Core of the Archetypical Go Application
listener, _ := net.Listen("tcp", ":8080")
for {
conn, _ := listener.Accept()
go handle(conn)`
}
The Go scheduler model is called G/P/M — Goroutine / Processor Virtual / Machine Thread, or alternatively N:M, where N goroutines are executed on M OS threads. Sometimes it is also called M:N, but I prefer to keep the letter M for OS threads.
The essence in one paragraph from ChatGPT:
Go is designed to simplify the development of concurrent I/O-intensive systems. This is achieved through the built-in goroutine model and a runtime scheduler that distributes their execution across a limited number of OS threads (M:N scheduling). As a result, the developer writes simple synchronous code, while concurrency and task switching are handled by the runtime.
Top comments (0)