High-speed game data server design architecture based on memory and Redis,mysql

Source: Internet
Author: User
Tags generator message queue serialization

Reprint please indicate the source, welcome everyone to criticize correct

1. Detailed design of data server

Data server is designed with three levels of data synchronization, to achieve high-speed player data acquisition and modification.

Data levels are divided into: memory data,Redis data,mysql data

Design Purpose: First of all to ensure the reliability of data, to prevent data loss, ensure data integrity. It then achieves high-speed access to the data, reducing the impact on data server performance caused by increased player count. Finally, the storage of operational data and data persistence are realized.

On this basis the data server is no longer a single server, it involves interaction with other servers.

The core of the data server is the redis data plane. Fast pull of player data with Redis.

The memory data is directly interacting with the logical server, so the memory data is divided into logical servers for management,Redis data is divided into Data server management,MySQL data is the Redis Backup and synchronization of the database.

The main process, when the data server starts , loads all active player related data from the MySQL database into the redis database, and the interaction between the logical server and the data server is Redis data for operation. Redis database writeback to MySQL can be implemented through a writeback process.

Figure 1 Data server data Twist flowchart

1.1Data server master process

The core of the data server main process is the operation of Redis , which realizes high-speed storage of data through the Redis database. Therefore, the core of the data server is to ensure that players in a large number of cases to the player data additions and deletions can be maintained within a certain time efficiency.

For Redis database itself, we can consider the implementation of Redis 's master-slave library construction, to ensure the backup and efficiency of Redis database.

The composition of the data server master process:

1. mysql data is synchronized to the redis database.

2. Network libraries for logical servers and data servers

3, Player account modification cache

4, the Redis database write back to MySQL

5. Request Processing of logical database

6. The organizational structure of the memory database

1.2data back -up service

After the data server receives the request from the logical server, the queue buffers the operation of the database operation request, forming the operation queue. The operations queue provides both a timely write of the Redis database and An asynchronous write to the MySQL database.

1.3data Server provides interfaces

For logical servers, the data server should open the following interfaces for it:

1, data pull interface, pull all the data corresponding to the player, save the data in memory data

2, modify the Player data interface, provide some or all of the player's information to modify the command

2Data Server module features

First Redis is a key-value in-memory database with key and value The way to store data for high-speed access and operation. Second , MySQL is a relational database that stores structured data. This difference determines the need to do the processing when the data is in Redis.

2.1Data WarehousingRedis

Using Redis 's key-value to store the table names in the database and the indexes and HashKeyof the tables, the two-dimensional data tables are constructed.

For each table,key structure [ table name ]:[ID value ]

Requires that the first field IDof all database tables,or all tables are primary, and cannot be duplicated, so that all keys are not duplicated

Figure 2 Data Warehousing post- redis Performance Structure

Warehousing Process Method One:

Clear Redis First, and then put All of MySQL's amount data into Redis. The advantage of one-on-another is that players can be dealt with other operations, the disadvantage is that the start of the server slow.

Method Two:

First, the Redis database is emptied, and the MySQL read processing data is cached as a redis -recognized transaction, one-time Redis Storage.

2.2Sort

For efficient sequencing, you can use Redis 's zset to build a hunt queue, and for a player level or experience change, the redis The database is modified to achieve efficient sorting when it is necessary or obtained to get The order of the Zset collection class directly.

2.3 Redisdata back -upMySQL

Operations on the redis database need to be stored back into the MySQL database.

Scenario One: The use of multi-threaded asynchronous, multi-threaded asynchronous implementation of MySQL modified back-to-memory synchronization. The data server opens one more thread for the back-up.

Pros: The design structure is relatively simple and does not involve interprocess communication, but requires multi-threading development support

Scenario two: Using a multi-process approach, the back-up is performed by a dedicated process, and no operation message is forwarded by the redis database default forwarding process.

Pros: Simple single-threaded to meet demand, but requires interprocess communication.

2.4Network interaction module and serialization

The network transmission between servers is performed by a unified network module. The serialization feature uses the protobuf serialization feature.

3Specific Implementation

Most of the data server designs are tables and Redis Data operations, which are similar in nature. Such a complex operation is not conducive to manual modification and writing code.

Therefore, the reasonable use of the generator here will reduce a large part of the workload, through the generator to quickly generate C + + code, and flexible response to changes in the database structure.

3.1Generators and generators

The generator uses the C # language to quickly write access to the database structure and generate class code.

The generated object can be designed into a variety of language operations files, currently generated C + + files.

3.2 Mysqldata Write-back process

The data writeback process also consists of a write-back message queue and a write-back that writes back the message queue cache for all write-back operations, and a writeback writer. Multi-threaded write-back message queue operation plus lock.

3.3 Player Login information Query

When the player logs in, it pulls the player data into memory. This process now queries the player data in the Redis database, if present, back to the logical server player data, if the player data does not exist in the Redis database, will again be in MySQL If There is a query that is returned to the logical server if it does not exist, the return query fails. The specific process to see

    

Figure 3 Player data query process

When modifying player data, three layers of data need to be synchronized. The data is first modified in memory, then the Modify command is initiated to the data server, and the data server presses the Modify command into the redis database command queue and the mysql command queue, respectively. Two more threads execute asynchronously on the command queue, completing the data write-back.

Figure 4 Data write-back command execution flow

3.4 player Activity level Division

To improve the speed and efficiency of data access, players are rated for activity. The game player data is loaded into memory for quick access. Active players are loaded into the redis database for quick pull. Inactive players Store MySQLand Access queries when needed.

Advantages: According to the frequency of data access to the data classification, so that high-frequency data access, saving memory space.

Cons: Inactive player Login Time is relatively long, but does not affect the game efficiency. Once logged in, the player again enters the active player storage scheme.

High-speed game data server design architecture based on memory and Redis,mysql

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.