Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens in TCP when the internal buffer fills up

Let's say we have the following TCP socket setup where client sends arbitary data to the server. Treat the following as a pseudocode.

def client():
    while True:
        data = source.get_data()
        client_socket.send(data)

The server reads the data and uses it to do something...

def server():
    while True:
        data += socket.recv(4096)
        parsed_data = parse_data(data)  
        cpu_intensive_task(parsed_data)

Let's assume that client will send the data much faster than the server can process. Can the internal network buffer fill up? I assume the answer is yes...

If so, then does the TCP protocol specify what will happend in this scenario? Are these discared packets treated as if they were lost in transit and just re-transmitted like any other lost packages?

Or are these packets truly lost and this something I have to consider when desiging my own communication protocol on top of the TCP?

Does the answer vary between operating systems?

For a record, the system above should have some kind of congestion control mechanism, which server could use to tell the client that it's under heavy load or it's free again. However, I'm curious to know how the TCP by the default would behave in this scenario.

like image 288
TukeV Avatar asked Apr 04 '18 09:04

TukeV


People also ask

What happens when TCP buffer is full?

As the receive buffer becomes full, new data cannot be accepted from the network for this socket and must be dropped, which indicates a congestion event to the transmitting node.

How do TCP buffers work?

TCP keeps the data in its receive buffer until the receiving application reads it from that buffer. After the receiving application reads the data, that space in the buffer is available for new data. The amount of free space in the buffer is advertised to the sending system, as described in the previous paragraph.

Why TCP needs to have a send buffer?

The reason to have buffers in user space is because TCP makes also flow control, so when it's not able to send data, it has to be put somewhere to cope with it. You'll have to decide if you need your process to save that data up to a limit or you can block sending data until the receiver is able to receive again.

What is the maximum buffer size of TCP?

The maximum size is 8 MB (8096 KB). The optimal buffer size depends on several network environment factors including types of switches and systems, acknowledgment timing, error rates and network topology, memory size, and data transfer size.


1 Answers

Can the internal network buffer fill up?

There are two internal buffers: the send buffer and the receive buffer. Both can fill up.

I assume the answer is yes...

Yes.

If so, then does the TCP protocol specify what will happen in this scenario?

Yes.

Are these discarded packets

There are no discarded packets. TCP does not discard packets in this circumstance. What happens when the send buffer fills depends on whether you are in blocking or non-blocking mode, or whether you are using an asynchronous API:

  • blocking mode: the sender blocks
  • non-blocking mode: the sender gets an error EAGAIN/EWOULDBLOCK
  • asynchronous: the operation continues to be deferred.

treated as if they were lost in transit and just re-transmitted like any other lost packages?

No. See above.

Or are these packets truly lost

No. See above.

and this something I have to consider when desiging my own communication protocol on top of the TCP?

No. See above. But the various conditions are certainly something you have to consider in your implementation of the protocol.

Does the answer vary between operating systems?

No.

For a record, the system above should have some kind of congestion control mechanism

TCP already has a congestion control mechanism.

which server could use to tell the client that it's under heavy load

How? if the client isn't reading, how can the server tell it anything?

or it's free again. However, I'm curious to know how the TCP by the default would behave in this scenario.

See above.

like image 65
user207421 Avatar answered Nov 25 '22 23:11

user207421