Golang redis was compiled when creating a connection pool there
Get a connection from the connection pool every time you query
Or do you connect to the database and defer close every query?
After the compiled go executable program is started for the first time, the go server process is created. This process will initialize the redis connection pool, which has a certain number of redis connections. When your program accepts the request and needs to query redis, it will first retrieve an idle redis connection from the redis connection pool and then access redis server through this connection. After use, put the connection back into the connection pool.
In this way, it is avoided to reconnect every time redis is used (think of TCP’s three-way handshake, three one-way packet transfers), so the program performance will be improved.
There are still some problems: for example, if the connection is idle for a long time, the redis server automatically closes down, what if the connection fails? If it doesn’t work, just reconnect. Connection pooling needs to ensure the validity of each connection.
What if the number of concurrent connections is too large and the connection is not enough? Wait.