How to install and configure memcached
Environment
- Red Hat Enterprise Linux 6
- Red Hat Enterprise Linux 7
Issue
- How to install memcached.
- How to configure memcached.
- How to start and stop memcached.
- How to capture memcached logs in /var/log/memcached.
- How to see memcached slabs and stats.
- High CPU utilization on memcached servers.
Resolution
What is Memcached.
Memcached is an open source, high-performance, distributed memory object caching system. While it is generic in nature, it is intended for use in speeding up dynamic web applications by alleviating database load.
Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering.
Memcached allows applications to take memory from parts of system where it has more than it needs and make it accessible to areas where applications have less than they need.
How to install memcached.
rhel-6-server-rpms repository from Red Hat:
# yum install memcached
How to configure memcached.
Configure a firewall
First and foremost, set up a firewall in your LAN if memcached servers should be accessible only from within your local network. Do not allow external traffic to ports used by memcached (e.g. 11211 as used by default).
Disable UDP
If you are not required to use UDP for memcached, we highly recomment switching to TCP connections for your memcached server (add "-U 0" to the OPTIONS variable in /etc/sysconfig/memcached). If you do require the use of UDP and require remote access to memcached servers (for example, in a clustering scenario), it is recommended to configure a firewall to allow connections only from trusted hosts.
Configure memcached
To configure memcached, edit the file /etc/sysconfig/memcached as follows
# vi /etc/sysconfig/memcached
Change the values as shown below and add the IP address of your server on which memcached is installed.
PORT="11211"
USER="memcached"
# max connection 2048
MAXCONN="2048"
# set ram size to 2048 - 2GiB
CACHESIZE="4096"
# disable UDP and listen to loopback ip 127.0.0.1, for network connection use real ip e.g., 10.0.0.5
OPTIONS="-U 0 -l 127.0.0.1"
How to configure your logs to /var/log/memcached.
To configure logs
In order to capture memcached logs, configure /etc/sysconfig/memcached as follows:
OPTIONS="-vv >> /var/log/memcached 2>&1"
Start memcached
Start or Stop memcached
After the above configurations, it is time to start the memcached service as below:
# chkconfig memcached on
# service memcached start
In order to stop/restart memcached, run the following commands:
# service memcached stop
# service memcached restart
Root Cause
- High resource utilization can happen due to low configurations settings for maximum connections and cachesize.
Increase the value of "maxconn" and "cachesize"
# cat /etc/sysconfig/memcached*
PORT="11211"
USER="memcached"
# max connection 2048
MAXCONN="2048"
# set ram size to 4096 - 4GiB
CACHESIZE="4096"
Diagnostic Steps
How to use memcached-tool to verify slabs and stats.
To check and verify memcached slabs
# memcached-tool IP_ADDRESS:Port
# memcached-tool IP_ADDRESS:Port display
# memcached-tool 127.0.0.1:11211
# memcached-tool 127.0.0.1:11211 display
Sample output.
# Item_Size Max_age 1MB_pages Count Full?
1 104 B 5134 s 1 10 no
2 136 B 5135 s 1 40 no
3 176 B 0 s 1 0 no
4 224 B 2648 s 1 7 no
8 552 B 1810 s 1 12 no
9 696 B 1810 s 1 6 no
10 872 B 2935 s 1 8 no
Verify memcached stats
In order to verify memcached statics, run the following commands:
# memcached-tool IP_Address:Port stats
# memcached-tool 127.0.0.1:11211 stats
Sample output from above command:
#127.0.0.1:11211 Field Value
accepting_conns 1
auth_cmds 0
auth_errors 0
bytes 0
bytes_read 87
bytes_written 3205
cas_badval 0
cas_hits 0
cas_misses 0
cmd_flush 0
cmd_get 0
cmd_set 0
conn_yields 0
connection_structures 11
curr_connections 10
curr_items 0
decr_hits 0
decr_misses 0
delete_hits 0
delete_misses 0
evictions 0
get_hits 0
get_misses 0
incr_hits 0
incr_misses 0
limit_maxbytes 4294967296
listen_disabled_num 0
pid 7679
pointer_size 64
rusage_system 0.030995
rusage_user 0.048992
threads 4
time 1407726751
total_connections 17
total_items 0
uptime 4999
version 1.4.4
References:
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.
Comments