marein / php-gaming-website

A gaming website where people can play against each other.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Reduce TCP TIME_WAIT to MySQL

marein opened this issue · comments

With constant pressure of 1000 requests per second, at some point PHP can no longer open local sockets to MySQL because they are all hanging in the TIME_WAIT state. This should be solved by keeping the connection to MySQL open. There're also some TCP tweaks like tw_reuse (how to configure with docker), but we should first try something else before overriding system defaults.

First possible solution
Use ProxySQL as an external connection pool, preferably as a sidecar. PHP would have short lived connection via unix sockets to ProxySQL and ProxySQL maintains long lived connections to the MySQL upstream.

Second possible solution
Use persistent connections. This would be an environment variable fix by adding ?persistent=1 to each DOCTRINE_DBAL_URL. Persistent connections suffer when there's an unclean connection state, such as open transactions, locks etc., which should be fixed as the app relies on them. The mysqli driver provides automatic cleanup for such scenarios. The connection string would look like mysqli://root:password@mysql:3306/connect-four?persistent=1.

Third possible solution
Deploy more short-sized php-fpm servers. The performance test that suffers from this problem deploys 5 php-fpm instances on 8 core servers with 12 workers each. This solution would live with the TIME_WAIT fact, but deploys more short-sized php-fpm servers, e.g. on 4 core with 6 workers, to distribute the TIME_WAIT sockets across each of them. Not preferred for now, but possible. The first two solutions seem more viable.

This issue is connected to #105.