当前位置:网站首页>Interview shock 69: is TCP reliable? Why?
Interview shock 69: is TCP reliable? Why?
2022-07-29 00:57:00 【Wang Lei】
Compared with UDP Come on ,TCP Its main characteristics are three : There is a connection 、 reliable 、 Data flow oriented . So-called “ There is a connection ” refer to TCP Connection management mechanism in , That is, the famous three handshakes and four waves , It's like making a phone call , Want normal communication , You must first establish a connection with the other party , That's what's called “ There is a connection ”, We will talk about the data flow oriented mechanism later , Our topic today is :TCP How to ensure reliability ? TCP The reason why reliability can be guaranteed , Mainly through the following 6 A means :
The checksum Confirm response Over time retransmission flow control Congestion control Discard duplicate data
Next , Let's take a detailed look at the specific implementation of these means .
1. The checksum
TCP The data format of the protocol is shown in the figure below :
( picture source : Xu Rusheng xxrs) As can be seen from the above figure “ The checksum ” Is stored in the TCP A data in the header ,TCP The sending end and receiving end of will adopt the same algorithm , According to the data sent, calculate a 16 Bit checksums , And the checksum will be sent to the receiving end together with the data . After receiving the data , A new checksum will be generated based on the received data , Then compare the new checksum with the passed checksum , If the checksums are the same , Then it means that there is no change in the data transmission process , It's a valid data , Otherwise, it is invalid data , Just give up .
Basic checksum algorithm
TCP/UDP/IP The checksum algorithm of such protocols is the same , The data flow is regarded as 16 Repeat the superposition calculation for bit integer stream . To calculate the test and , First, set the inspection and field to 0, then , For each in the valid data range 16 Bit for binary inverse code summation , The results are in the tests and fields , If the data length is odd, fill in one byte 0. When you get the data , Also for each in the valid data range 16 The sum of the binary inverse code of the digits . Because the receiver includes the verification and the , therefore , If there is no error in the transmission of the first part , Then the receiver's calculation result should be all 0 Or all 1( To be specific, we have realized , The essence is the same ) . If the result is not all 0 Or all 1, That means the data is wrong .
2. Confirm response
Confirmation and response mechanism is the key means to ensure the reliability of message delivery , It is also almost all message oriented middleware (MQ) in , One of the most commonly used technologies , For example, mainstream message oriented middleware RabbitMQ、Kafka、RocketMQ There is a confirmation and response mechanism in , That's what we often say ACK(ACKnowledge Character, Confirmation character ). The confirmation response mechanism is TCP in , The core mechanism to ensure the reliability of messages . How can you confirm that the other party must have received your message ? Undoubtedly, the most effective way is for the other party to tell you , It has received , This is the confirmation response . The process of confirmation and response is shown in the following figure : 
3. Over time retransmission
There may be two problems during the confirmation of the message : First of all , The message was lost when it was sent , second , The message was lost when confirming the reply , As shown in the figure below :
obviously , Even if there is a confirmation and response mechanism, it can not guarantee that the message will not be lost , Then what shall I do? ? It doesn't matter if the message is lost , After the sender confirms the loss of the message , It is not enough to compensate the receiver for the same message ? This is the retransmission timeout mechanism .
Clever timeout retransmission mechanism
TCP The timeout retransmission mechanism of is also very clever in design , It is to ensure that messages are in any environment , Can communicate efficiently , therefore TCP It's using “ Dynamic time ” Timeout retransmission mechanism of . For example, if the message is lost for the first time , Then the sender will be 500ms Then send a message , If the second message sent is also lost , Then the sender will be 1000ms Then send a message , If the third message is also lost , Then it will 2000ms Then send a message , If you accumulate a certain number of times , The message has not been successfully sent , that TCP You will think that the other host is abnormal , Will force the connection to close , This is it. TCP The main execution process of overtime retransmission .
4. flow control
The speed of data processing at the receiving end is limited , If the sender sends too fast , This will cause the buffer at the receiving end to be full , At this time, if the sender continues to send , It will cause packet loss , Then it causes a series of chain reactions such as packet loss and retransmission . therefore TCP According to the processing of the receiving end , Dynamically adjust the size of the sent data , This mechanism is called flow control (Flow Control).
5. Congestion control
Congestion control refers to TCP According to the current network situation , Dynamically control the amount of data sent , Transfer data at a suitable speed . Imagine , If TCP In an environment that is not clear about the network , Rashly send a large amount of data to the receiving end , This will lead to more packet loss and overtime retransmission , Thus causing a series of chain reactions , Cause data transmission to slow down . and TCP What is taken is “ Slow start ” Mechanism , Start with a small amount of data , Explore the way , Find out the current state of network congestion , And then decide how fast to transmit data , This is the congestion control mechanism . If more data is transferred , There are a lot of packet losses , that TCP It will reduce the amount of data sent , Then try to slowly increase the amount of data sent , Through this form of dynamically sending data packets , To achieve data transmission suitable for the current network speed , This is it. TCP The concrete realization of congestion control .
6. Discard duplicate data
Through the previous knowledge, we know , When confirming the response , Due to the loss of acknowledgement message , Then the receiver may receive duplicate data from the sender , As shown in the figure below :
At this time, for the business side , Only one data is needed , therefore TCP There is also a mechanism , Mechanism for discarding duplicate data , This will ensure that the data received by the business party is correct . TCP A number will be added to each package sent , If a packet with the same number is received , Then it means that the receiving end gets duplicate packets , Just throw it away .
summary
TCP The main means to ensure reliability are 6 individual : The checksum 、 Confirm response 、 Over time retransmission 、 flow control 、 Congestion control 、 Discard duplicate data . Among them, flow control and congestion control are easy to confuse , We need to know clearly , Flow control is a control mechanism for the receiving capacity of the receiving end , Congestion control is a control mechanism for the current network , So don't mix it up .
Reference resources & Acknowledgement
blog.csdn.net/namelcx/article/details/6866720
It's up to you to judge right and wrong , Disdain is to listen to people , Gain or loss is more important than number .
official account :Java Analysis of the real interview questions
Interview collection :https://gitee.com/mydb/interview
边栏推荐
- Surfacecontrol and surfaceflinger communication
- 第二轮1000个Okaleido Tiger,再次登录Binance NFT 1小时售罄
- 双链表的定义 ~
- Several methods of multi-threaded sequential operation can be asked casually in the interview
- 如何给女友讲明白JS的bind模拟实现
- SQL server only has database files and no log files. The solution to the 1813 error in restoring data times
- 【commons-lang3专题】003- RandomStringUtils 专题
- UE4 调试常用的打印信息方法
- The method of tracking the real-time market of London Silver
- Xinchi technology released the latest flagship product of G9 series, equipped with six A55 cores with 1.8GHz dominant frequency
猜你喜欢

selenium对接代理与seleniumwire访问开发者工具NetWork

追踪伦敦银实时行情的方法有哪些?

自制 | 纯手工自制一个16位RISC架构CPU
![[raspberry pie] how does the windows computer connect with raspberry pie](/img/d6/42685bbc4e4af757867442b63ce9c8.png)
[raspberry pie] how does the windows computer connect with raspberry pie

Outlier detection and open set identification (1)

Error reporting: when the browser clicks the modify add button, there is no response and no error reporting. Solution

我不建议你使用SELECT *

小程序毕设作品之微信校园浴室预约小程序毕业设计成品(6)开题答辩PPT

Yield Guild Games:这一年的总结与未来展望
![[untitled]](/img/28/db3b2e1985dc9acf41cdf2004ea0d5.png)
[untitled]
随机推荐
Cloud function realizes website automatic check-in configuration details [web function /nodejs/cookie]
【AD学习】本次海上航行器大赛画pcb图的历程
About 1931cie -- conversion of XYZ color coordinate graph to RGB color coordinate relationship
seleniumwire获取百度指数
Implement Lmax disruptor queue from scratch (VI) analysis of the principle of disruptor solving pseudo sharing and consumers' elegant stopping
The method of tracking the real-time market of London Silver
Jupyter notebook中5个有趣的魔法命令
B站“崩溃”始末 2021.07.13 我们是这样崩的(转载)
SurfaceControl和SurfaceFlinger通信
主线程与守护线程
Matlab02: structured programming and function definition "suggestions collection"
UE4 调试常用的打印信息方法
Outlier detection and open set identification (1)
flask结合容联云发送验证码
散列表 ~
伦敦金即时行情带来什么机会?
从零开始实现lmax-Disruptor队列(六)Disruptor 解决伪共享、消费者优雅停止实现原理解析
B+ 树 ~
Relying on cloud business to support revenue growth alone, is Microsoft still overvalued?
DRF - web development mode, API interface, API interface testing tool, restful specification, serialization and deserialization, DRF installation and use