Monday, April 23, 2012

WordPress Install Memcached Object Cache Plugin To Speed Up Blog

How do I install memcached cache plugin under UNIX / Linux to speed up my dynamic database-driven wordpress blog by caching data and objects in RAM to reduce the number of times an external data source must be read?

You need to install memcached caching server only if your wordpress based blog get a large numbers of hits. You need to install the following software(s) on server:
  1. memcached server : It is a caching daemon designed especially for dynamic web applications to decrease database load by storing objects in memory.
  2. php-pecl-memcache: PHP extension to work with the Memcached caching daemon through handy OO and procedural interfaces. Memcache can be used as a PHP session handler too.
  3. WordPress memcached object cache plugin : This plugin provides a persistent backend for the WordPress object cache.

Our Sample Setup

Internet
\\
\\
ISP Router +--------+
// +-------| mysql1 | 10.10.1.1
// | +--------+
// |
+-----+ | +------+
| lb0 |--------+-------| www1 | 10.10.1.2
+-----+ | +------+
vip:202.54.1.1 |
| +------+
|-------| www2 | 10.10.1.3
| +------+
|
| +------+
+-----+ |-------| www3 | 10.10.1.4
| lb1 |--------+ +------+
+-----+ |
| +-----------+
|-------| memcache1 | 10.10.1.5
+-----------+
Where,
  • lb0 : It is a reverse proxy server that is installed on a UNIX / Linux in front of Apache Web servers hosted on www1, www2, www3. You can use nginx or any other reverse proxy server with virtual ip (connection redundancy) managed by Keepalived. This is master load balancer.
  • lb1: This is backup load balancer with nginx and keepalived.
  • www1, www2, www3 : Apache web server with php. You need install "php-pecl-memcache" and "Memcached Object Cache wordpress plugin" on all three servers.
  • mysql1 : This is MySQL server with RAID10 and fast scsi / sas disk. This server in under load and you are going to reduce the load by caching queries in RAM.
  • memcache1 : This server runs memcached with lots of RAM and it will cache mysql1 queries to decrease database load by storing objects in memory. You need to install memcached server on memcahce1. You can add memcach2 for redundancy purpose.
All of the following instructions are tested on RHEL and CentOS 5.x 64 bit servers.

Install Memcached Server

Type the following command on memache1 server. First, you need to turn on EPEL repo:
# rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm
Now, install memcached using the yum command, enter:
# yum install memcached

Configure memcached

You need to edit /etc/sysconfig/memcached file, enter:
# vi /etc/sysconfig/memcached
Edit as follows:
 
PORT="11211"
USER="memcached"
MAXCONN="2048"
CACHESIZE="4096"
OPTIONS="-l 10.10.1.5"
 
Where,
  • PORT: Listen on TCP port # 11211, the default is port 11211.
  • USER: Run memcached server as memcached user.
  • MAXCONN: Use 2048 max simultaneous connections; the default is 1024.
  • CACHESIZE: Use 4096 MB (4GB) memory max to use for object storage; the default is 64 megabytes.
  • OPTIONS="-l 10.10.1.5": Listen on 10.10.1.5. 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. In this example, IP address 10.10.1.5 is only accessible using LAN and is behind firewalled host.
Make changes as per your setup and requirements. Save and close the file.

Turn On Service

Type the following chkconfig command to turn memcached service, enter:
# /sbin/chkconfig memcached on
## command to START the server ##
# /sbin/service memcached start
## command to STOP the server ##
# /sbin/service memcached stop
## command to RESTART the server ##
# /sbin/service memcached restart

Install php-pecl-memcache

Type the following commands on www1, www2, and www3 Apache server:
# yum -y install php-pecl-memcache
# /sbin/service httpd restart

Install Memcached Object Cache Plugin

Type the following command on www1, www2, and www3 Apache server (if you are using some sort of cluster aware file system such as GFS2 or OCFS2, than just type it on any one apache web server node):
Visit this url and grab the plugin, enter:
$ cd /tmp/
$ wget http://downloads.wordpress.org/plugin/memcached.2.0.zip
$ unzip memcached.2.0.zip

Edit object-cache.php, enter:
$ vi object-cache.php
Edit memcahe server and port connection information:
 
$buckets = array('10.10.1.5:11211');
 
Save and close the file. Finally, copy object-cache.php into your wp-content directory. In our example /var/www/html/wp-content/ directory:
$ cp object-cache.php /var/www/html/wp-content/

How Do I Verify That It Is Working?

Type the following commands to display memcache slabs (please note that the following output is taken from a small memcached server with just 512MB cache for demonstration purpose only):
# memcached-tool 10.10.1.5:11211 display
Sample outouts:
  #  Item_Size  Max_age   Pages   Count   Full?  Evicted Evict_Time OOM
1 96B 38302s 1 42 no 0 0 0
2 120B 37571s 1 4 no 0 0 0
3 152B 335s 1 232 no 0 0 0
4 192B 37763s 1 40 no 0 0 0
5 240B 37804s 1 36 no 0 0 0
6 304B 37595s 1 86 no 0 0 0
7 384B 829s 4 10401 no 0 0 0
8 480B 228s 1 972 no 0 0 0
9 600B 106s 1 387 no 0 0 0
10 752B 38298s 1 288 no 0 0 0
11 944B 404s 1 143 no 0 0 0
12 1.2K 38319s 1 258 no 0 0 0
13 1.4K 12739s 1 176 no 0 0 0
14 1.8K 38322s 1 230 no 0 0 0
15 2.3K 1500s 2 491 no 0 0 0
16 2.8K 1500s 2 648 no 0 0 0
17 3.5K 828s 3 600 no 0 0 0
18 4.4K 37660s 2 322 no 0 0 0
19 5.5K 38035s 1 171 no 0 0 0
20 6.9K 38458s 1 102 no 0 0 0
21 8.7K 39002s 1 39 no 0 0 0
22 10.8K 42068s 1 35 no 0 0 0
23 13.6K 24184s 1 15 no 0 0 0
24 16.9K 41626s 1 11 no 0 0 0
25 21.2K 43426s 1 1 no 0 0 0
26 26.5K 43392s 1 1 no 0 0 0
37 308.5K 1493s 1 3 yes 0 0 0
To shows general stats, enter:
# memcached-tool 10.10.1.5:11211 stats
Sample outputs:
#10.10.1.5:11211   Field       Value
accepting_conns 1
auth_cmds 0
auth_errors 0
bytes 14945401
bytes_read 1320187573
bytes_written 3180772729
cas_badval 0
cas_hits 0
cas_misses 0
cmd_flush 0
cmd_get 1280549
cmd_set 1262345
conn_yields 0
connection_structures 73
curr_connections 72
curr_items 15724
decr_hits 0
decr_misses 0
delete_hits 11296
delete_misses 24284
evictions 0
get_hits 1156788
get_misses 123761
incr_hits 0
incr_misses 0
limit_maxbytes 536870912
listen_disabled_num 0
pid 42690
pointer_size 64
reclaimed 2
rusage_system 51.550163
rusage_user 20.861828
threads 4
time 1284368953
total_connections 558
total_items 75121
uptime 43527
version 1.4.5
To dumps keys and values, enter:
# memcached-tool 10.10.1.5:11211 dump | less

More Tips: To Increase Your WordPress Based Blog Speed

  1. Turn on MySQL query cache.
  2. Use xcache opcode cacher to accelerates the performance of PHP on servers.
  3. Use CDN (Content Delivery Network) for WordPress to speed up your blog.

Conclusion

The end result was pretty good. The database request are completed faster and loads on database servers are decreased. The above setup can be used with other php or webapps too.

No comments:

Post a Comment