Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the design and implementation of IM message system?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the design and implementation of the IM message system, the content is very detailed, interested friends can refer to, hope to be helpful to you. I. interpretation of nouns

Unicast: the server sends messages to a single client user

Multicast: the server sends messages to multiple client users

Multicast / broadcast: the server sends messages to a group of clients. There is a group of ID to identify this group of users

Uplink message: the server sends a message to a group of clients. There is a group of ID to identify this group of users

Downstream message: the server sends a message to the client

II. System architecture

Proxy: deployed in the edge computer room, and the client accesses nearby through smart dns

LogicService: handle authentication, heartbeat, online and offline, in and out of the group

PushService: unicast, broadcast, receive a message and forward it to comet, and then send it out by comet

ImService: chat server, dealing with single chat, group chat, offline messages

CosumerService: asynchronous write diffusion of group messages

AuthService: authentication service

Data structure

CacheService maintains global online users and is a secondary map user_id-> conn_id-> server_id.

User_id is specified by the business and uniquely identifies a user

The conn_id is assigned by the storage process and uniquely identifies a connection for the user

Server_id identifies which access process this connection belongs to.

Access process proxy to maintain its own online users, user_id+conn_id-> Connection

Connection is the encapsulation of a client connection to which you can push messages.

Access process proxy maintains connection room information, room_id-> ConnectionList

Third, message model

3.1 Reading Diffusion Model

Advantages: write only once, reducing the number of writes, especially in group mode

Disadvantages: the logic of synchronous messages will be more complex, the receiver has to read each session once, enlarged reading, will produce a lot of invalid requests.

3.2 write diffusion model

Advantages: the logic of pulling messages is simple

Disadvantages: enlarge the writing, write twice for a single chat, and write N times in the group.

IV. Implementation 4.1 single chat 4.1.1 Design goal 4.1.2 online message

4.1.3 offline message 4.1.4 Detection message loss 4.2 Group chat 4.2.1 Design goal 4.2.2 small Group (write Diffusion)

4.2.3 large groups (read diffusion)

5. Bottleneck of high performance analysis CPU > bandwidth > memory 5.1 capacity planning: (Ali CVM 16C32G-2.5GHz, reserve more than 50%)

10000 conn per proxy

100 proxy

50 logicService/cacheService/pushService

Or improvements:

10 logicService

5 pushService

Kafka cluster

Zookeeper cluster

10 cacheService

5.2 there are no bottlenecks in internal communications, but the paths for horizontal expansion are as follows:

Client-initiated RPC mobile-> proxy-> micro

Online / offline / switch rooms / heartbeat mobile-> proxy-> logicService-> cacheService

Unicast micro-> logicService (- > cacheService)-> pushService-> proxy-> mobile

Online information query

Check rooms / session online by users

5.3 Internal communications, paths that may have bottlenecks:

Batch unicast micro-> logicService ((N-parallel)-> router)-> pushService-> proxy-> mobile

Limit: the total number of users in a batch, not too many

Broadcast micro-> logicService-> pushService-> proxy-> mobile

Limit: due to the room list on pushService periodic absorb proxy, the number of so pushService cannot be too large.

Improvement: logicService is decoupled from pushService and connected with kafka. Since pushService consumes the least CPU in proxy/logicService/cacheService, only a very small number of pushService instances are needed.

Online information query

Check the total number of online / count due to the room users on the logicService periodic absorb cacheService, only a limited number of logicService can open counter to check regularly.

Check users / room with / count by room

Interface for traversal / list debugging, not for service

5.4 proxy performance bottleneck 5.5 rpc performance bottleneck 6. High availability analysis provides users with 7-24-hour uninterrupted service. Iterative development requires that the upgrading and expansion of internal modules and business services are unaware of users.

Proxy stateless service. When restarting or upgrading, the client detects a disconnection and automatically reconnects to another proxy

LogicService stateless service. When you restart or upgrade, proxy will automatically find the next logic.

PushService stateless service. When you restart or upgrade, other pushService will provide services.

CacheService stateful service. When restarting or upgrading, the slave cacheService is topped. When the upgrade is completed, switch back to the master cacheService.

ImService stateless service. When you restart or upgrade, other pushService will provide services.

Mysql: use the mysql master master mechanism to guarantee

Redis: use the Sentinel mechanism to ensure availability

VII. Handling of abnormal situations

How to prevent message loss (the receiver reports the largest received message id, and the exception server resends it)

Self-increasing id discontinuity caused by redis master-slave switching

How to improve the performance of proxy broadcast

How to prevent the single connection of rpc from becoming a bottleneck

VIII. Low cost and safety

Almost no external dependence, extremely low operation and maintenance cost

High-performance code implementation to save server costs

Integrated authentication and authentication, also supports HTTPS

On the design and implementation of the IM message system is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report