当前位置:网站首页>Introduction to zero based im development (4): what is message timing consistency in IM systems?
Introduction to zero based im development (4): what is message timing consistency in IM systems?
2020-11-09 11:32:00 【JackJiang2020】
This article quotes Shen Jian 《 How to ensure IM Real time message “ Timing ” And “ Uniformity ”?》 The picture and content of the article ( Because I'm too lazy , The picture is not redrawn ), The original text is linked at the end of the article .
1、 introduction
This article continues with the previous article 《 Zero basis IM Introduction to development ( 3、 ... and ): What is? IM The reliability of the system ?》, Explain IM The consistency of message timing in the system .
So called consistency , stay IM In general, it refers to the consistency of message timing , That's it :
- 1) Context continuity of chat messages ;
- 2) Absolute chronology of chat messages .
More specifically ,IM The consistency of the message is reflected in :
- 1) When talking alone : Make sure that the order in which the sender sends out the chat message is consistent with the order in which the receiver sees it ;
- 2) Group chat : Make sure that all group members see the chat message , It is consistent with the absolute time order when the sender sends the message .
IM The consistency of message timing in the system is a seemingly simple problem , One of the most difficult hot topics in technology , This article tries to explain to you in plain and simple words IM The product significance of message timing consistency problem 、 Cause of occurrence 、 Solutions, etc .
2、 Series articles
《 Zero basis IM Introduction to development ( One ): What is? IM System ?》
《 Zero basis IM Introduction to development ( Two ): What is? IM Real time performance of the system ?》
《 Zero basis IM Introduction to development ( Four ): What is? IM Message timing consistency of the system ?》(* this paper )
《 Zero basis IM Introduction to development ( 5、 ... and ): What is? IM Security of the system ? ( Release later )》
《 Zero basis IM Introduction to development ( 6、 ... and ): What is? IM The heartbeat mechanism of the system ? ( Release later )》
《 Zero basis IM Introduction to development ( 7、 ... and ): How to understand and implement IM System message not read ? ( Release later )》
《 Zero basis IM Introduction to development ( 8、 ... and ): How to understand and implement IM Multi terminal message roaming of the system ? ( Release later )》
3、 Consistency of message timing , about IM The meaning of
Today, , Because of the popularity of mobile Internet , The actual social relationships of modern people , Almost entirely by IM This instant messaging social tool is organized ,IM The importance of this tool is self-evident .
IM In modern life , More and more important , But it's more and more common . Now I want to contact someone , The first thing I thought about was not to make a phone call , It's a “ WeChat ” or “QQ”. Yes ,IM This thing carries more and more meaning .
The consistency of message timing , about IM The meaning of , There is no doubt that it brings more than the so-called user experience problems . Let's take a look at an example .
hypothesis : Your confession to the goddess is entering a critical stage , Just because it sucks IM, This leads to the incoherence of the chat message , At the moment 1000 Kilometer away, your goddess is staring at your mobile phone with a mask “ Drunken talk ”, How serious the consequences are —— Over the past six months “ Flatterer ” life 、 swallow humiliation and bear a heavy load , It's all for nothing by programmers “ Plaid Shirt ”、“ mediterranean sea ” We have been harmed .
To put it more seriously , Because it sucks IM, Let you lose this rare good gene with the help of goddess , It's a great opportunity to transform the appearance of the offspring of the family , It's very painful ...
The above example , It's just a chat , If it's a group chat , Then the problem may be magnified infinitely : Think of a technology exchange group , It's a heated argument “php It's the best language in the world ” When it comes to this kind of topic , Suddenly I want to smash my cell phone , It's not because it's too noisy , And because the order of the messages is completely out of order , It has seriously affected the keyboard heroes to express their personal opinions .
4、 It's hard to guarantee the consistency of message timing ?
4.1 Basic cognition
In general IM In the eyes of users , News is nothing more than passing from one mobile phone to another , What's the difficulty with timing ?
Yes , Ordinary users think so , Technically speaking , He just simply will IM The process of sending and receiving messages is understood as the working mode of single thread .
actually , stay IM In this high performance scenario , In order to pursue high throughput 、 High concurrency , Using multithreading 、 asynchronous IO And so on. .
under these circumstances ,“ High concurrency ” And “ The order ” about IM On the server side , It's a contradiction , It's a little bit fishy and bear's paws ( It's hard to have both ).
therefore , To achieve IM Temporal consistency of messages in scenarios , A trade-off needs to be made , And there are many technical dimensions to consider . This leads to the specific technology implementation has no fixed routine , And because of the uneven technical capabilities of developers , It makes a lot of IM There will be big problems in the actual effect of the system , For users, it will also be reflected directly in the product experience .
The following is a detailed description of the technical difficulties .
4.2 There is no global clock
As shown in the figure above , A really usable production system , It shows that it is impossible for all services to run on one server , The distributed environment is for sure .
that : In a distributed environment , client + All kinds of background services on the server side , They're all distributed on different machines , Local clocks are used between machines , There is no such thing as “ Global clock ”( There's no real global clock ), Then the so-called message timing has no real time sequence benchmark . So the timing of messages is obviously not “ Local time ” It can be completely decided .
4.3 Multi sender problem
When the server is distributed , Out-of-service “ Local time ” To ensure timing , So can we use the receiver's local time to represent the time sequence ?
Unfortunately , Due to the presence of multiple clients ( For example, when talking in groups ), Even the local time of a server , It doesn't mean “ Absolute timing ”.
As shown in the figure above : In absolute timing ,APP1 Send out first msg1,APP2 Post issue msg2, All sent to the server web1, Network transmission is not guaranteed msg1 It must precede msg2 Arrived at , So even with a server web1 According to the time of , It can't be described precisely msg1 And msg2 The absolute timing of .
4.4 Multi receiver problem
The timing of the sender cannot be guaranteed , Suppose there is only one sender , Can the local time of the sender be used to represent the time sequence ? Unfortunately , Due to the presence of multiple receivers , Cannot use sender's local time , Express “ Absolute timing ”.
Pictured above , In absolute timing ,web1 Send out first msg1, Post issue msg2, Due to the network transmission and the existence of multiple receivers , No guarantee msg1 First received, first processed , There is no guarantee that msg1 And msg2 The processing sequence of .
4.5 Network transmission and multithreading
Since it is difficult for multi sender and multi receiver to guarantee absolute timing , So suppose there is only a single sender and a single receiver , Can the absolute timing consistency of messages be guaranteed ?
The conclusion is pessimistic , Because of network transmission and multithreading , It still doesn't work .
As shown in the figure above ,web1 Send out first msg1、 Post issue msg2, Even if msg1 First arrive ( Network transmission is not guaranteed msg1 First arrive ), Because of multithreading , There's no guarantee msg1 Be dealt with first .
5、 How to ensure the absolute consistency of message timing ?
Through the summary of the previous chapter , We're right IM The reason for the problem of temporal consistency of messages in , Have a more profound understanding of .
From a purely technical point of view , hypothesis :
- 1) There's only one sender ;
- 2) A receiver ;
- 3) There is only one upstream and downstream connection socket Connect ;
- 4) Communicate by blocking .
In this case , There is no guarantee that the message sent first will be processed first , And then be shown to the receiver of the message first ?
Yes , Sure !
But this is unlikely to happen in actual production IM System , It will be sent to the delivery party 、 Single receiver 、 single socket Connect 、 Blocking mode , In this way IM Once it's done , The product manager will show you immediately ...
6、 Practical optimization ideas
6.1 One to one chat message consistency assurance ideas
Suppose two people talk one on one , The sender A In turn msg1、msg2、msg3 Three messages to the receiver B, How to ensure the consistency of display timing of these three messages ( Send and display in the same order )?
We know , The sender A In turn msg1、msg2、msg3 Three messages , After the end of the service , When it is transferred by the server , This sequence is due to the problem of multithreaded networks , It's possible to get out of order .
So the result could be :
As shown in the figure above , There will be a problem that the timing of the message is inconsistent with that when it is sent out ( The order of messages received is :msg3、msg1、msg2).
however , Actually, two people who are talking one on one , There is no need for global message timing consistency ( Because chatting only happens in the same conversation between two people ), Just for the same sender A, issue B It's just that the message sequence is consistent .
Common optimization schemes , stay A Go to B In the message sent out , Plus the sender A A local absolute time sequence ( For example, the local timestamp ), To show the receiver B The timing of the presentation of .
So when the receiver B After receiving the message , Even in extreme cases, messages may arrive out of order , But because of this disorder time difference, for ordinary users, the sense of body is very short , stay UI The presentation layer is sorted according to the absolute sequence in the message and then displayed , In fact, there are not too many users' perception .
6.2 How to ensure the message consistency of many to many group chats
hypothesis N A group of friends in a IM Chat in the group , How to ensure the consistency of display timing of messages received by all group members ?
First : You can't use the absolute timing of the sender to ensure the message order as a pair of chats , Because the sender of group chat is not only , Time is not the same .
Maybe : We can use the server's single point to do serialization .
As shown in the figure above , here IM The sending process of group chat is :
- 1)sender1 issue msg1,sender2 issue msg2;
- 2)msg1 and msg2 Through the access cluster , Service cluster ;
- 3)service Layer to floor take a unique seq, To determine the timing of the receiver's presentation ;
- 4)service Get msg2 Of seq yes 20,msg1 Of seq yes 30;
- 5) Send messages to multiple group friends through the delivery service , Even if you receive msg1 and msg2 The time is different , But it can be unified according to seq To show .
This method :
- 1) Advantage is : It can realize that the message display time sequence of all group friends is the same ;
- 2) The disadvantage is that : This service, which generates a global increment sequence number, can easily become a system bottleneck .
Is there any further optimization method ?
From a technical point of view : In fact, group messages do not need to ensure that the global message sequence is orderly , As long as the messages in a group are in order , In this case ,“ news id serialize ” It's a good idea .
Above, in this scenario ,service Layers no longer need to go to a unified back end to get the overall situation seq( Serial number ), But in service The connection pool level has been slightly modified , Make sure that messages from a group fall on the same service On , This service You can use local seq To serialize all messages of the same group , Ensure that all group members see the message in the same time sequence .
About IM How to realize message serialization under the system architecture of , Or global news ID The generation scheme of , This is another very popular technology topic .
have interest in , Read the following series in depth :
In this series , In particular, the trend of wechat is increasing ID Generate ideas ( Be careful : Increasing trend is not strictly increasing , Increasing trend means that there is ID It's OK to be skipped ), For distributed IM The news of ID It's very practical .
Yes , about IM system , The absolute sense of timing is difficult to guarantee , But through the monotonous incremental message generated by the server ID The way , Using incremental ID To ensure timing , It's also a very feasible plan .
7、 To sum up
IM System architecture , The absolute timing of messages is difficult , There are many reasons , such as : There is no global clock 、 Multiple senders 、 Multiple recipients 、 Multithreading 、 Network transmission uncertainty, etc .
When talking one-on-one , In fact, we only need to ensure that the timing of sending is consistent with that of receiving , It can basically make the user feel out of order .
In the case of many to many group chats , Ensure that all receivers in the same group have the same timing , It can also make users feel out of order , There are two ways , A single point absolute timing , Another implementation message id Serialization ( That is to implement a global incremental message ID).
8、 Reference material
[1] How to ensure IM Real time message “ Timing ” And “ Uniformity ”?, author : Shen Jian
[2] A low cost guarantee IM Discussion on the method of message timing , author : Feng Yu
This article has been published on “ Instant messaging technology circle ” official account :
Links are :http://www.52im.net/thread-3189-1-1.html
版权声明
本文为[JackJiang2020]所创,转载请带上原文链接,感谢
边栏推荐
- Biden wins the US election! Python developers in Silicon Valley make fun of Ku Wang in this way
- When Python calls ffmpeg, 'ffmpeg' is not an internal or external command, nor a runnable program
- Log analysis tool - goaccess
- 接口测试如何在post请求中传递文件
- 实现商品CRUD操作
- 基于synchronized锁的深度解析
- Composition - API
- 使用流读文件写文件处理大文件
- 从编码、网络传输、架构设计揭秘腾讯云高质量、高可用实时音视频技术实践...
- 1450. 在既定时间做作业的学生人数
猜你喜欢
随机推荐
做用户,绕不开画像!
Android权限大全
Looking for better dynamic getter and setter solutions
解决IDEA快捷键 Alt+Insert 失效的问题
Handwritten digital image recognition convolution neural network
利用 Python 一键下载网易云音乐 10W+ 乐库
解决python调用 ffmpeg时 ‘ffmpeg‘ 不是内部或外部命令,也不是可运行的程序
Open source ERP recruitment
Understanding data structures starts with this article~
捕获冒泡?难道浏览器是鱼吗?
Windows环境下如何进行线程Dump分析
Depth analysis based on synchronized lock
Composition - API
Android Development - service application, timer implementation (thread + service)
嘉宾专访|2020 PostgreSQL亚洲大会阿里云数据库专场:樊文凯
医疗项目管理的三种实用技巧
1450. 在既定时间做作业的学生人数
After SQL group query, get the first n records of each group
使用CopyMemory API出现 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
Implement crud operation