Author: Yunfeng comment: This article combines the actual game cases to discuss how MongoDB and redis work together and why it is used. The Momo hegemony project was not named at the beginning. In 2013, it was just a test project our company used to test mobile games. The goal was very clear at the very beginning, and COC was the first one that impressed me.
Author: Yunfeng comment: This article combines the actual game cases to discuss how MongoDB and redis work together and why it is used. The Momo hegemony project was not named at the beginning. In 2013, it was just a test project our company used to test mobile games. The goal was very clear at the very beginning, and COC was the first one that impressed me.
Author: Yunfeng
Comment: based on the actual game cases, this article describes how to use MongoDB and redis together and why.
The Momo hegemony project was not named at the beginning. In 2013, it was just a test project our company used to test mobile games. The goal was very clear at the very beginning. COC was the first online game to impress me on the mobile platform. It showed me a different design direction for online games than traditional MMO. I think we only need to strip out the core of the game. Soon we can make a simple but different from the previous MMO game, and then we can start to develop on this basis.
As for finding Momo cooperation, it was a coincidence. Our test project was completed, but we didn't think about how to push the game to the players (this type of game doesn't have a certain number of players), and Momo's game platform was just launched, only one product (a game similar to bubble Dragon) has poor performance. Because our company and the founder of Momo have worked in NetEase and are very familiar with it. It took only one month for the game to be released on Momo's game platform.
At the beginning, we only called Nan, who just finished the mad edge starter project, and changed me to do this project. After I finished my initial graphic engine work, the game was handed over to him. We only want to do the client, because only this part requires technical experience, and the server will not be too different from the traditional MMO. Our company has been developing around the skynet Server framework for a long time, and we can quickly set up the servers of this mobile game project at any time.
By the summer of 2013, I felt like I should start working on the server. Xiao Jing has accumulated a lot of server development experience in the client game project of Douro mainland. Besides me, he is also the most familiar with skynet; if this test project only has one program to develop servers, there will be no better candidates.
From that time on, we began to consider the server structure, including the database selection and architecture.
Skynet has its own IO model. If it is efficient enough, it is best to use the socket library provided by skynet to write the database driver by yourself. Because redis protocol is the most concise, I made only redis driver for skynet. The game edge developer we acted on was using MongoDB. For ease of operation, our platform also made a lot of things using it, and I made a preliminary mongodb driver for skynet. When we start development on the server, we have two options.
My more than a decade of experience in the gaming industry tells me that Databases play a major role in data backup and disaster tolerance in real-time interactive online games. It is rarely used for data exchange. In other fields, many developers choose to use databases as data exchanges between business modules. Playing games with this idea will often cause serious performance problems.
Simply put, in theory, most of the data in the game world has been stored in memory because the game servers often work hours a day and players are highly interactive. After the server is started, most of the data will no longer need to exit the memory once the data is loaded. The server is constantly creating new data and making the data flow in the memory. It does not need to read data from the outside. If the memory is infinitely large and the server will never become a machine, the database is unnecessary.
Of course, neither of these two prerequisites can be true.
With the infinite memory, traditional MMORPG games consume O (n) memory, which is related to the total number of users. Although the number of simultaneous game users (the number of active users) is limited, it is difficult to continue to grow; but the total number of users indeed increases with time. After we change n from the total number of users to the number of active users, we can basically maintain the memory requirements.
The simplest way is to load the data from a disk when a user is inactive. In the early days of client games, the standard of user activity was whether the user was online or not. We load the user's data when the user goes online, and load the data offline. From the perspective of development, the simplest way to save data is not to use a database, but to serialize user data into text and write it to the file system by using the user id as the file name. This is the general practice of early Netease games.
For the server stability requirements, we cannot set 100% improper machines, so data should be stored on a regular basis. It can be saved on a periodic basis or when a key operation occurs. In this way, the disaster can be recovered.
Btw: the total amount of data to be managed by a system is smaller than the total amount of memory in the system. This is not only in the gaming field, but also in many other systems. Therefore, redis, a pure memory database, has a wide range of applications. The BGSAVE and BGSAVE modes of apsaradb for redis also correspond to the data landing policy mentioned above.
As for how to operate the data, since the data is in the memory of your system, you can always write the corresponding algorithms to process them? By understanding this, we can understand why database selection is not an important issue in most online game systems.
Of course, the operation of an online game still requires a large amount of in-game data analysis. Based on the principle that different business logic should be separated as much as possible, we still need to output the data in the game to professional systems and professionals. The amount of data in this part is much larger than the amount required by the game system to serve players. I think its spatial complexity is O (n * m. There are two dimensions: one is the total number of players and the other is the operation time. The game server needs to spit out the data during operation and store it in a database that can process such a large amount of data. We call this part of data operation log, which is not suitable for me because it is easy to confuse with the error log output by the Program for debugging and analysis, however, when I was working on Netease in history, I was not going to start a new term.
In terms of server selection and architecture, Momo hegemony follows these steps:
We use redis to store player data. Considering the large number of players, a redis warehouse may not be enough. We use 32 redis warehouses for separate storage by player id. In terms of deployment, multiple redis warehouses can be deployed on the same physical machine when the number of users is small, and then deployed separately as the number of users expands. If 32 warehouses are not enough, further segmentation will not be difficult.
In the first three months, we didn't have to worry too much about the hot and cold data. During this period, we could not talk about losing players. All the player data was hot data. Due to the tight development time, we leave Cold Data Processing for later processing.
As for data implementation, redis has the bgsave capability, and we only need to fine tune it.
While Operating Logs and some data that grows naturally over time, such as combat videos, we chose mongodb, which is not limited by memory and easy to perform data analysis. Mongos sharding is used because the data volume is too large.
The initial design is like this. Today, we have not made any adjustments to the structure. However, many pitfalls are worth recording.
I am going to have dinner today, and I will continue to write it tomorrow.
Notice: the second game of Momo game platform: Momo dance troupe was launched half a month before Momo hegemony. The service was suspended in less than two days after the launch, and the service downtime was extended for a week. It is rumored that the problem lies in the database. Next, I plan to gossip about this.
Source: http://blog.codingnow.com/2014/03/mmzb_db.html
Original article address: apsaradb for MongoDB and apsaradb for redis are used in Momo Warcraft games. Thank you for sharing your gratitude.