Why go language is suitable for developing online games server side

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.  Some time ago in Golang-china read this post: Have Mook Jong use (or intend) Golang do game server side? Personally think Golang is very suitable for the development of the server side of the game, write down this article to summarize. From the point of view of online games: To successfully operate a online game, to a large extent depends on the player's spontaneous formation of the community. Only the player spontaneously form a stable ecosystem, the game can continue to avoid the emergence of ghost towns. This requires a large number of users to import, at the same time the amount of online users reached a critical point, it is possible to complete. Therefore, it is necessary for many people to be online at the same time. Then look at the common play of online games, in addition to the leaderboard such statistics and data summary of the function, there is no need for a lot of CPU time application. In previous projects, the CPU consumption of the various damage calculations generated by instant combat was not very significant. Players need to complete an operation, through the client-server side-client such a back and forth, in order to achieve high response speed, to meet the player experience, server-side processing can not take up too much time. Therefore, the corresponding CPU consumption per request is relatively small. The IO of online games is mainly divided into two aspects, one is network IO and the other is disk IO. The network IO aspect, can divide into the fine arts resources IO and the game logic instruction Io, here mainly analyzes the game logic the IO. The IO of the game logic is similar to CPU usage, with a small number of bytes per request, but the concurrency is quite high because many people are simultaneously online. In addition, the broadcast of map information will bring more frequent network communication. The disk IO aspect, mainly is the game data preservation. With different databases, there will be a big difference. In previous projects, the process of moving from MySQL to MongoDB, a memory database, is no longer a bottleneck. In general, it is also a buffer of memory to avoid a large number of small chunks of data read and write scenarios.
For these characteristics of online games, Golang's language features are ideal for developing game server-side.

first, the Go language provides the goroutine mechanism as a native concurrency mechanism. The memory required for each goroutine is very small, and in practice, a large number of goroutine can be initiated to respond to concurrent connections. Goroutine is very similar to the Greenlet in Gevent, when encountering IO blocking, the scheduler automatically switches to another goroutine execution, ensuring that the CPU does not wait for IO to occur. Compared with gevent, Goroutine does not need to use multiple processes to extract the performance of multicore machines without the underlying Gil limitations of Python. By setting the maximum number of threads, you can control the threads that go starts, and each thread executes a goroutine, allowing the CPU to run at full load.   at the same time, go language provides the unique communication mechanism--channel for Goroutine. When the channel reads and writes, it also suspends the current operation of the channel Goroutine, which is a synchronous blocking communication. This achieves both the purpose of communication and synchronization, and with the CSP model, the concurrency model is triggered by a set of processes and events between processes to trigger the resolution of the task. Although the mainstream programming languages, as long as they are Turing-complete, they can all achieve the same function. But the go language provides this inter-process communication mechanism, it is very elegant to reveal the nature of the Association of Communication, to avoid the previous lock explicit use to the programmer's psychological burden, is indeed a major advantage. For the development of online games, the programmer can write the game logic according to the single-threaded block, no need to consider the problem of thread scheduling, and the problem of data dependency between threads. Because of the channel communication between threads, the data dependencies between threads have been expressed, and the go Scheduler is properly handled.   In addition, the Go language provides the GC mechanism, as well as the protection of pointers to use, can greatly reduce the programmer's development pressure, improve development efficiency.   looking to the future, I expect the go language community to provide more isolation mechanisms between goroutine. Individuals highly appreciate the philosophy of the Erlang community's brittle disintegration, pushing the application to break down as early as possible, and then fork out new processes to process new requests. For the co-process mechanism, it is necessary for the programmer to ensure that the function executed does not have a dead loop, causing the thread to die. If you can customize the maximum CPU execution time of the functions performed by Goroutine, and the maximum memory space that can be used, it is beneficial to improve the robustness of the system.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.