Nginx+PHP-FPM+APC+Varnish+Memcached: High Mem/Swap consumption

memcachednginxvarnishWordpress

I've got running a VPS at Linode with 1024 MB RAM, Ubuntu 11.04 and Nginx+PHP-FPM+APC Varnish+Memcached active. I've setup a couple of test blogs with WordPress 3.2.1 and everything works, and the performance tests seem promising:

# ab -n 1000 -c 100 http://mydomain.com/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking mydomain.com (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software:        nginx
Server Hostname:        mydomain.com
Server Port:            80

Document Path:          /
Document Length:        5677 bytes

Concurrency Level:      100
Time taken for tests:   0.186 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      5989000 bytes
HTML transferred:       5677000 bytes
Requests per second:    5376.14 [#/sec] (mean)
Time per request:       18.601 [ms] (mean)
Time per request:       0.186 [ms] (mean, across all concurrent requests)
Transfer rate:          31443.08 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        6    9   0.9      8      13
Processing:     4    9   1.3      9      14
Waiting:        1    5   2.5      5      13
Total:         12   18   1.6     18      22
WARNING: The median and mean for the initial connection time are not within a normal deviation
        These results are probably not that reliable.

Percentage of the requests served within a certain time (ms)
  50%     18
  66%     19
  75%     19
  80%     19
  90%     20
  95%     21
  98%     22
  99%     22
 100%     22 (longest request)

After installing Memcached and php5-memcache packages and enabling the WP Memcache plugin I can see the memcache server is up and running, with around a 50% of hits.The memcache service is using 64 MB of memory:

# ps xa |grep memcached
12259 ?        Sl     0:01 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1

What I can see, though, is a high consumption of main memory and swap, and I'd like to know if this is normal. The server is idle most of the time (the test blogs are running, but no one nows about them so I'm the only visitor), so I can't understand why the memory and swap are so high.

#free -m
             total       used       free     shared    buffers     cached
Mem:           927        906         21          0          6         58
-/+ buffers/cache:        840         87
Swap:          255        255          0

Any ideas?

Update: Seems Memcached is not the reason. I've stopped the server, the site is of course running, but the mem/swap consumption is still almost 100%. So there should be a problem, maybe with PHP-FPM or Varnish?

Update2: Seems PHP-FPM is the problem, look at this (thanks for the command, Tomtezky)

# ps aux --sort=-rss | head -15
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
www-data 12414  0.0  9.6 247840 91772 ?        S    01:25   0:06 php-fpm: pool www                                      
www-data 12405  0.0  9.5 244012 91212 ?        S    01:25   0:06 php-fpm: pool www                                      
www-data 12418  0.0  9.0 241820 86132 ?        S    01:25   0:05 php-fpm: pool www                                      
www-data 12417  0.0  8.5 236724 81500 ?        S    01:25   0:07 php-fpm: pool www                                      
www-data 12409  0.0  8.2 241980 78784 ?        S    01:25   0:05 php-fpm: pool www                                      
www-data 12407  0.0  8.1 236968 77880 ?        S    01:25   0:05 php-fpm: pool www                                      
www-data 12400  0.0  8.1 234344 77860 ?        S    01:25   0:06 php-fpm: pool www                                      
www-data 12408  0.0  8.1 237120 77700 ?        S    01:25   0:06 php-fpm: pool www                                      
www-data 12410  0.0  8.0 232248 76224 ?        S    01:25   0:06 php-fpm: pool www                                      
www-data 12413  0.0  7.6 242092 72452 ?        S    01:25   0:07 php-fpm: pool www                                      
www-data 12416  0.0  7.1 241812 68148 ?        S    01:25   0:05 php-fpm: pool www                                      
www-data 12412  0.0  7.0 220684 66644 ?        S    01:25   0:04 php-fpm: pool www                                      
www-data 12402  0.0  6.9 241556 66156 ?        S    01:25   0:05 php-fpm: pool www                                      
www-data 12419  0.0  6.2 238016 59304 ?        S    01:25   0:04 php-fpm: pool www

Seeing that… how to reduce PHP-FPM's memory use?

Best Answer

It isn't PHP-FPM, it's the Wordpress instance loaded in PHP-FPM. Limit the number of php-fpm workers, or trim down the memory footprint of your wordpress install.