Mobile Internet combat--social game leaderboard Design and implementation (1)

Source: Internet
Author: User

Objective:
Game areas, especially mobile social games, leaderboards have become a great magic weapon to enhance experience interaction and increase user stickiness. This is about how to design and implement the game rankings under the different user scale, game service/game platform trend. This article focuses on the implementation of the traditional relational MySQL scheme, which will explain the role of NoSQL in the future. Small (Mumuxinfei) The understanding of this piece is relatively shallow, the viewpoint does not represent the mainstream (industry) practice, hope can be a catalyst.

Demand analysis
Once upon a while, version aircraft war smash hit. Each road hero Brush rankings, drying results. However, the rankings are limited to their friends, and each user's circle of friends is different, so each user has their own rankings. And the rankings are reset by week 0. Some simple mobile games (such as 2048, without a buddy concept) are in a simple global ranking, with the highest ranking in history.

  
In summary, for the game leaderboard, we can be divided according to attributes.
1). by whether it belongs to the circle of friends
* Game Global Top n mode.
* Top N mode with its own circle of friends.
2). by time period to divide
* Reset by time period, e.g. press Zhou Qing 0
* History highest, no reset 0 mechanism

Basic article:
Social games, in the premise of small-scale users, with the help of a relational database (MySQL) to achieve, the use of single-Library single table.
Define a friend table Tb_friend

CREATE TABLE IF not EXISTS tb_friend (id INT PRIMARY KEY auto_increment, user_id varchar (+), friend_id varchar (64), UNIQUE KEY ' idx_tb_friend_user_id_friend_id ' (' user_id ', ' friend_id ')) Engine=innodb DEFAULT Charset=utf8;

User Score Table Tb_score

CREATE TABLE IF not EXISTS tb_score (id INT PRIMARY KEY auto_increment, user_id varchar (+), score INT, UNIQUE KEY ' I dx_tb_score_user_id ' (' user_id ')) Engine=innodb DEFAULT Charset=utf8;

Commentary: The user_id/friend_id form a composite index in the Tb_friend table, used to maintain USER_ID's buddy list, Tb_score to record each user's score
Under the premise of the two tables, how do I get the leaderboard for my friends?
Use two table joins to achieve:

SELECT tf.friend_id as friend_id, Ts.score as score from tb_friend TF JOIN tb_score ts on tf.friend_id = ts.user_id WHERE tf.user_id =? ORDER by Ts.score DESC

A similar result is as follows:
  
  Commentary: In the SQL join, you need to be aware of the difference between the left join/equal Join/right join. The equivalent join is selected here.

performance evaluation and execution analysis:
1). Small table + Large table mode: In Tb_friend single table 9,801 strips (100 small partners, mutual friends)/tb_score single table 53 strips (53 small partners have scored) record, perform a join analysis
Execution planning

EXPLAIN SELECT tf.friend_id as friend_id, Ts.score as score from tb_friend TF JOIN tb_score ts on tf.friend_id = Ts.user_i D WHERE tf.user_id =? ORDER by Ts.score DESC

  

  Commentary: This side of the SQL optimizer is very intelligent, with the help of small table driver large table join optimization (small table Tb_score Drive large table Tb_friend to join), the small table to the file sort (a total of 53 records), the Big Table with index ( Equivalent join corresponds to a row of large table records).
2). Table mode: In Tb_friend single table 19,602/tb_score single table 5,092 records, the join analysis

  

  Commentary: This side tb_friend table drive Tb_score as join, tb_friend with Composite index (USER_ID,FRIEND_ID) to speed up optimization. MySQL's SQL Optimizer is still quite intelligent and powerful.

Advanced article:
As the data scale becomes larger and the number of concurrent accesses increases, MySQL's access is becoming a bottleneck. At the same time, the SQL statement of the join involves Filesort CPU-intensive. How to solve this situation?
  *) Introduction of distributed MySQL cluster, the sub-database sub-table.
As a great artifact of the Internet, the sub-database is of immediate effect. However, there will be some loss, after the sub-table, will lose a lot of features. such as transactional, FOREIGN KEY constraint association. At the level of the business, the tb_score of the user is involved, and the Tb_friend table data is not in the same library, resulting in join invalidation. Eventually, the merge at the application layer makes the ranking operation evolve into 1+n SQL operations (1 SQL is used to get a buddy list, and N SQL is used to get each friend's score). This requires attention.

1+n's SQL Evolution, Application layer scoring ranking, performance would become a disaster.

(1) Get user Friend list select friend_id from tb_friend_{n} WHERE user_id =? (2) Traversal gets each friend's score foreach friend_id in Friend_list (?) SELECT score from tb_score_{m} wehre user_id =? (3) The application layer to do score sorting

Commentary: {n},{m} refers to a specific number of tables , of course, the same table in the same library, with the help of SELECT * in (...) . To optimize, this depends on the specific data distribution. It's just a good way of thinking.
Small part of the view: because Tb_friend is a large table, and Tb_score is a small table, so tb_friend using the sub-Library sub-table (user_id as the basis) to achieve, and Tb_score using single-Library single table (easy to batch query) way to achieve.
Of course, in industry, MySQL's optimization program is very mature, not only the sub-database sub- table, as well as master-slave separation (Master/slave mechanism, master for writing services, multi-slave nodes to provide read services).
You can see the following illustration:
  

Summary & Follow-up:
The main story here is based on the traditional relational database MySQL to achieve a friend-based game leaderboards, personal achievements need to get real-time, and the Friends list of the record can allow a certain delay. The realization of the ranking of the Friends ' record is the central issue of this article. MySQL implementation of the data volume/concurrency on the premise of the increase, but also shows a certain fatigue state. Here's how to introduce a nosql system that plays an important role in the game rank. Look forward to your attention.

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.