“Memcached” High Performance Distributed Memory Object Cache Server

# yum install memcached.x86_64 php-pecl-memcache.x86_64

Edit /etc/sysconfig/memcached file, enter

# vi /etc/sysconfig/memcached

Sample outputs

PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS="

For busy servers you need to increase the values as follows:

MAXCONN: Use 1024 max simultaneous connections; the default is 1024. For busy server increase this number as per requirements.
CACHESIZE: Use 64 MB memory max to use for object storage; the default is 64 megabytes. For busy server you can set it to 512MB or 1GB (1024).
OPTIONS: You can set server IP address (listen on address) here so that apache/php/nginx based serer can connect to the server. By default it is set to INADDR_ANY. This is an important option to consider as there is no other way to secure the installation. Binding to an internal or firewalled network interface is suggested.

PORT="11211"
USER="memcached"
MAXCONN="4096"
CACHESIZE="512"
OPTIONS="-l 192.168.1.15"

Type the following commands

# /etc/init.d/memcached start
# /etc/init.d/memcached stop
# /etc/init.d/memcached restart
# /etc/init.d/memcached status

Type the following command to see if it is running or not

# pgrep memcached
# netstat -tulpn | grep :11211

Sample outputs

tcp        0      0 192.168.1.15:11211           0.0.0.0:*                   LISTEN      24964/memcached
udp        0      0 192.168.1.15:11211           0.0.0.0:*                               24964/memcached

Use the memcached-tool to get general stats

# memcached-tool 192.168.1.15:11211 stats

Sample outputs

#192.168.1.15:11211 Field       Value
         accepting_conns           1
               auth_cmds           0
             auth_errors           0
                   bytes     5481902
              bytes_read     8242409
           bytes_written    26023492
              cas_badval           0
                cas_hits           0
              cas_misses           0
               cmd_flush           0
                 cmd_get        9042
                 cmd_set        4469
             conn_yields           0
   connection_structures          48
        curr_connections          47
              curr_items        3197
               decr_hits           0
             decr_misses           0
             delete_hits           6
           delete_misses         112
               evictions           0
                get_hits        7490
              get_misses        1552
               incr_hits           0
             incr_misses           0
          limit_maxbytes   536870912
     listen_disabled_num           0
                     pid       24964
            pointer_size          64
           rusage_system    0.149977
             rusage_user    0.043993
                 threads           4
                    time  1327395688
       total_connections         171
             total_items        3226
                  uptime         198
                 version       1.4.4

Make sure iptables only allows access to your own servers. Edit /etc/sysconfig/iptables and append the following rules or add it as follows to your shell scripts

## add to your shell script ##
## assumption default INPUT policy is set to DROP ##
## only accept connection to tcp/udp port 11211 if ip is between 192.168.1.10 and 192.168.1.15 ##
iptables -A INPUT -p tcp --destination-port 11211 -m state --state NEW  -m iprange --src-range 192.168.1.10-192.168.1.15 -j ACCEPT
iptables -A INPUT -p udp --destination-port 11211 -m state --state NEW  -m iprange --src-range 192.168.1.10-192.168.1.15 -j ACCEPT