use unix socket instead of tcp connection for memcached to speed up web servers

By default, memcached is listening on port 11211 for all connections. But for a single server, it’s better to config memcached to use unix socket which improve the performance and reduce the legacy.

On freebsd, it’s easy to change from TCP/IP to unix socket. But there are some steps you need to pay more attention.

1. Modify rc.conf to force memcached to use unix socket.

2. Modify the connection string in your php files.
For TCP connection, use

If you are using php-memcache, you should change it to

If you are using php-memcached, you should change it to

3. Another thing I found in Freebsd is that if you force memcached to use unix socket, every time you restart the service, memcached will change the socket file permission to 550 which means php can’t connect to the socket file. So you need to grant php user access to that file.

You need to do this each time after you restart memcached.

1 thought on “use unix socket instead of tcp connection for memcached to speed up web servers

Leave a Reply

Your email address will not be published. Required fields are marked *