Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is TCP Guaranteed to arrive in order?

If I send two TCP messages, do I need to handle the case where the latter arrives before the former? Or is it guaranteed to arrive in the order I send it? I assume that this is not a Twisted-specific example, because it should conform to the TCP standard, but if anyone familiar with Twisted could provide a Twisted-specific answer for my own peace of mind, that'd be appreciated :-)

like image 605
Smashery Avatar asked Nov 06 '09 23:11

Smashery


People also ask

Is TCP always in order?

TCP is connection-oriented and offers its Clients in-order delivery. Of course this applies to the connection level: individual connections are independent. You should note that normally we refer to "TCP streams" and "UDP messages".

Can TCP receive packets out of order?

If too many packets are received out of order, TCP will cause a retransmission of packets similar to what happens with dropped packets. As such, the impact of out of order packets on goodput is similar to the impact of packet loss.

What causes TCP out of order?

Out-of-order delivery can be caused by packets following multiple paths through a network, by lower-layer retransmission procedures (such as automatic repeat request), or via parallel processing paths within network equipment that are not designed to ensure that packet ordering is preserved.

Is TCP out of order normal?

It is not generally a problem. It probably indicates there are multiple paths between source and destination - and one travels a through a longer path. It means TCP has slightly more work to reassemble segments in the correct order.


2 Answers

As long as the two messages were sent on the same TCP connection, order will be maintained. If multiple connections are opened between the same pair of processes, you may be in trouble.

Regarding Twisted, or any other asynchronous event system: I expect you'll get the dataReceived messages in the order that bytes are received. However, if you start pushing work off onto deferred calls, you can, erm... "twist" your control flow beyond recognition.

like image 89
Jeffrey Hantin Avatar answered Sep 21 '22 15:09

Jeffrey Hantin


TCP is connection-oriented and offers its Clients in-order delivery. Of course this applies to the connection level: individual connections are independent.

You should note that normally we refer to "TCP streams" and "UDP messages".

Whatever Client library you use (e.g. Twisted), the underlying TCP connection is independent of it. TCP will deliver the "protocol messages" in order to your client. By "protocol message" I refer of course to the protocol you use on the TCP layer.

Further note that I/O operation are async in nature and very dependent on system load + also compounding network delays & losses, you cannot rely on message ordering between TCP connections.

like image 41
jldupont Avatar answered Sep 23 '22 15:09

jldupont