Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Situation Awareness
Managed Threat Detection
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive

Merging Game Servers with DCS

Updated on 2024-10-28 GMT+08:00

Overview

Application Scenario

Merging game servers is a strategy for some large online games. After running a game for a while, game providers set up a new server to attract new players. As users shift to the new server, game developers usually merge the new server and the old one, so new and old players can play together for a better game experience. During this process, game developers must consider how to synchronize data among different servers.

Solution

DCS for Redis can be used in the following game server merge scenarios:

  • Cross-server data synchronization

    After servers merger, data on multiple servers needs to be synchronized to ensure consistency. With the pub/sub message queuing mechanism of Redis, data changes can be published to Redis channels. Other game servers can subscribe to the channels to receive messages of changes.

  • Cross-server resource sharing

    After servers merge, resources on multiple servers, such as player props and gold coins, can be shared. The distributed lock mechanism of Redis can ensure mutual exclusion among multiple servers in resource access.

  • Cross-server ranking

    After servers merge, rankings on multiple servers can be combined to show the ranking over all servers. Sorted sets in Redis can store ranking data and perform calculation and query.

For details about cross-server resource sharing, see Serializing Access to Frequently Accessed Resources. For details about cross-server ranking, see Ranking with DCS.

The following describes how to implement cross-server data synchronization through pub/sub message queuing in Redis.

NOTICE:

When using Redis for game server merge, you need to consider data consistency, performance, and security. Issues such as data errors, performance bottlenecks, and security vulnerabilities should be avoided.

Prerequisites

  • A DCS instance has been created, and is in the Running state.
  • The network between the client server and the DCS instance is connected:
    • When the client and the DCS Redis instance are in the same VPC:

      By default, networks in a VPC can communicate with each other.

    • When the client and the DCS Redis instance are in different VPCs in the same region:

      If the client and DCS Redis instance are not in the same VPC, connect them by establishing a VPC peering connection. For details, see Does DCS Support Cross-VPC Access?

    • To access a Redis instance of another region on a client

      If the client server and the Redis instance are not in the same region, connect the network using Direct Connect. For details, see What Is Direct Connect.

    • For public access

      For details about how to access a DCS Redis 4.0/5.0/6.0 instance on a client over a public network, see Using Nginx for Public Access to DCS or Using ELB for Public Access to DCS.

Procedure

  1. Use the Redis() method from the redis-py library to create a Redis client connection on each game server.
  2. Use the pubsub() method to create a Redis subscriber and publisher on each game server. They will be used for subscribing to messages from other game servers and publishing data changes on the local server. When a server needs to update data, it publishes updates to the Redis message queue. Other servers will receive the updates and update their local data.
  3. Define a publish_update() method to publish updates, and use the subscriber.listen() method in the listen_updates() method to listen to updates.
  4. Once an update is captured, the handle_update() method is invoked to process the update and update local data. In game servers, the publish_update() method can be invoked to publish updates, and the listen_updates() method can be invoked to listen to updates.

Sample Code

The sample code (Python 2) for using the redis-py-based pub/sub mechanism to implement cross-server game data synchronization is as follows:

import redis
 # Create a Redis client connection. Replace the Redis instance connection address and port with the actual values.
redis_client = redis.Redis(host='localhost', port=6379, db=0)
 # Create a subscriber.
subscriber = redis_client.pubsub()
subscriber.subscribe('game_updates')
 # Create a publisher.
publisher = redis_client
 # Publish updates.
def publish_update(update):
    publisher.publish('game_updates', update)
 # Process updates.
def handle_update(update):
    # Update local data.
    print('Received update:', update)
 # Listen to updates.
def listen_updates():
    for message in subscriber.listen():
        if message['type'] == 'message':
            update = message['data']
            handle_update(update)
 # Invoke publish_update().
publish_update('player_data_updated')
 # Invoke listen_updates().
listen_updates()

Result:

D:\workspace\pythonProject\venv\Scripts\python.exe D:\workspace\pythonProject\test2.py 
Received update: b'player_data_updated'

We use cookies to improve our site and your experience. By continuing to browse our site you accept our cookie policy. Find out more

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback