Z
Z
zekohina2016-12-11 16:46:14
System administration
zekohina, 2016-12-11 16:46:14

What does it mean to open/close a connection and why is this an expensive operation?

I often heard that you should not open a connection to the database every time, but use a connection pool. I also heard that the browser does not open a new connection each time, but uses the old one for some time.
Please explain in plain language what it means to "open/close" a connection and why this operation is considered expensive. Thank you.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
R
Rou1997, 2016-12-11
@Rou1997

Send a TCP / IP packet to the server about opening or closing the connection, when opened, the server will create a new socket and it will be possible to send data and poll it (receive data), when closed, it will delete the socket, the operation is not particularly slow, because the packets themselves are almost empty and opening /closing the socket on the server itself does not take much time, but still, the operation takes some time, so ideally it is better without it.

R
res2001, 2016-12-11
@res2001

1.Because, as a rule, there is an exchange over the network, and this is a long operation (in any case, it is longer than if both the client and the server were on the same computer).
2. Opening network connections (and database connections) consumes a certain amount of system resources. If you have a server that opens a new connection to the database for each request, then with a certain number of requests sent in a short period of time, your server will either fall or stop connecting new connections (perhaps the database server will fall first). The number of connections that a server can process at the same time usually depends on the amount of memory. But fighting DOS attacks by increasing the server's memory is a fool's errand.
3. When closing a network connection, you first need to break it with the remote side (if it is TCP), and this also generates network traffic. Then release the system resources associated with this connection.

M
Mercury13, 2016-12-11
@Mercury13

1. Any real-time data stream (including a network connection) contains I / O buffers, where data is overdue until the receiving side is ready to accept it. In general, when connected, allocate memory, when disconnected, return it.
2. The opening of the connection occurs in three stages: “preved-bear-ready”. There is such a thing as a “half-open connection”: on the initiating side, we sent “preved”, but did not receive “bear”. At the host, they sent a “bear”, but did not hear “ready”. The number of half-open connections is common to the entire system and is usually large, but can also be exhausted.
3. Any transmission over the network is not instantaneous. From our side, the connection will be considered open when “ready” has gone to the network, so if the ping is 100 ms, then after 100 ms the connection will be opened.
4. The login procedure (and the TLS handshake, if used) involves strong cryptography, which is also slow. And TLS also has its own trinity of “preved-bear-ready”.
5. When the connection is closed, the computer also tries to send an "I'm closing" packet.
6. Anti-DDoS measures, finally.

V
Vi, 2016-12-11
@redsabien

In principle, if it weren’t for the limit on the number of file descriptors, then it would be possible to open and close as much as you like
, but it can be expensive that the system does not instantly delete the closed connection, so a situation may arise when the process tries to open the connection again and the system refuses it, or it opened too a lot or a common aisle has been reached. because each open file occupies some space in memory and if you do not follow it, then the stack can at least overflow

C
CityCat4, 2016-12-12
@CityCat4

If you mean a TCP connection, then read on the topic "three-way handshake". Opening a TCP connection is both an expensive and time-consuming operation - the High Contracting Parties must agree on a lot of things before they proceed to exchange data and must disperse "culturally".

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question