Friday, September 19, 2008

How do you know when you need more memcache servers?

Let me first start of with the disclaimer, that I do not use memcache to scale, I use it to reduce latency. I'm of firm belief that the database layer should be able to handle the requests, while memcache is used to keep frequent requests returning in a consistent time frame i.e. reduce I/O spikes.

Capacity planning is key to making sure your site can serve the requests to users. If the site is slow, or down that is loss revenue in any revenue model used to monetize your product.


How to determine when you need to add more memcache servers.

The stats I look at are system stats and memcache stats.

Memcache is Memory / network heavy. CPU spikes are very low, and if the CPU starts maxing out that is probably due to some sort of network driver issue or huge context switching or large values stored in memcache.

So on the system side I look at vmstat


[root@memcached1 ~]# vmstat 5
procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu----
r b swpd free buff cache si so bi bo in cs us sy id wa
0 0 208 207400 41452 50552 0 0 10 198 0 0 2 9 87 2
0 1 208 207408 41452 50552 0 0 0 81 15617 17671 3 13 84 0
0 0 208 207352 41452 50552 0 0 0 56 15508 17514 3 13 84 0
1 0 208 207248 41452 50620 0 0 0 310 15295 16762 3 12 84 0
0 0 208 207248 41452 50620 0 0 0 31 15512 17167 2 13 84 0
0 0 208 207256 41452 50620 0 0 0 3 15925 18214 3 14 84 0
0 0 208 207264 41452 50620 0 0 0 0 15456 16923 3 13 85 0
0 0 208 207264 41452 50620 0 0 0 213 15782 17604 3 13 84 0
0 0 208 207264 41452 50620 0 0 0 40 15860 18036 2 13 84 0
2 0 208 207272 41452 50620 0 0 0 214 15926 18248 3 14 84 0
0 0 208 207288 41452 50620 0 0 0 77 15781 17617 3 13 84 0


This server dedicated to memcache. The context switching is huge due to all of the constant requests-but we are talking about modern day CPU's which can context switch like crazy. The thing that is bugging me is that requests are starting to go into the run queue, not at a alarming rate but still this is an indication of some possible issue.

This is something that is graphed on ganglia. If the run queue on average starts increasing, there is some problem.


Next stats from memcache.


/**
* @desc get extended status from all servers
*/
public function CacheGetStats(){

if ($GLOBALS[cfg][disable_feature_memcache]){

return true;

}

return $this->memcache_obj->getExtendedStats();
}



I have a class called Cache which is a wrapper around memcache class calls. Cal Henderson would kill me if I was using classes at Flickr. Don't get me wrong I agree with Cal 100% but the environment I am in now requires classes-so I have to use it. The reason why we don't like classes is for another post.

So the output.

[pid] => 17696
[uptime] => 2748911
[time] => 1221850214
[version] => 1.2.2
[pointer_size] => 64
[rusage_user] => 135944.231335
[rusage_system] => 420733.419798
[curr_items] => 6012187
[total_items] => 2362145406
[bytes] => 4737438938
[curr_connections] => 654
[total_connections] => 4128179078
[connection_structures] => 7293
[cmd_get] => 12681552588
[cmd_set] => 2362145408
[get_hits] => 9880855733
[get_misses] => 2800696855
[evictions] => 0
[bytes_read] => 2564412782739
[bytes_written] => 12893067371405
[limit_maxbytes] => 5242880000
[threads] => 4



Notice on this server we have a good hit rate and no evictions. Yet looking at one server is not good enough, look at them all- the reason more memcache servers means more memory to store data for your application. The CRC32 hash that the PHP memcache client uses is not very even and some keys may be requested more.



[pid] => 13956
[uptime] => 4228079
[time] => 1221850213
[version] => 1.2.2
[pointer_size] => 64
[rusage_user] => 268369.193681
[rusage_system] => 711491.537845
[curr_items] => 5219411
[total_items] => 3686853272
[bytes] => 4751658935
[curr_connections] => 675
[total_connections] => 4154000955
[connection_structures] => 9981
[cmd_get] => 19489963453
[cmd_set] => 3686853275
[get_hits] => 15062084538
[get_misses] => 4427878915
[evictions] => 11210410
[bytes_read] => 3908139025173
[bytes_written] => 10744393525089
[limit_maxbytes] => 5242880000
[threads] => 4



Take a look at this server. The evictions are high, indicating that memcache needs to make room for new objects. This is not good, its an indication that the LRU is evicting objects out faster then their expire time. Additionally the memcache gets are much greater then the hits. This is an indication that memcache is not really working as good as it can.

But one server is not an indication that there is a problem. Looking at the system as a whole is to determine if a problem exists. My rule of thumb is if the 30-40% of the servers have a high eviction rate, its time to add 30-40% more servers or memory.

Now allot of this can be tuned by changing the slab size, but learning from John Allspaw, don't make a plan based on a possible gain, make a plan based on the current usage. Then if the possible gain works your golden.

How do you base your stats on adding more memcache servers?

8 comments:

Chris Barber said...

So, what's wrong with classes? Are you talking about the old days of PHP4 or are you saying classes are bad in PHP5 too?

Anonymous said...

I'm new to PHP and recently setup my local machine with PHP and MySQL for doing development. I was sort of stuck when I needed to post my work for the user to test and review. After looking around a bit I found a site that hosts PHP and MySQL apps. I was surprised that it was free - it seems they're offering the service at no cost until 2012. At that point they'll change over to a fee-based service. However, in the meantime, it's a great place to do anything from demo and sandbox right up to posting sites for real.

Their pitch is as follows:

"This is absolutely free, there is no catch. You get 350 MB of disk space and 100 GB bandwidth. They also have cPanel control panel which is amazing and easy to use website builder. Moreover, there is no any kind of advertising on your pages."

Check it out using this link:

http://www.000webhost.com/83188.html

Important: There's one catch in that you must make sure you visit the account every 14 days - otherwise the account is marked 'Inactive' and the files are deleted!!!

Thanks and good luck!

sK said...

Great article, thanks!

But as a PHP programmer I just can't ignore one thing - your missuse of PHP constants.

$GLOBALS[cfg][disable_feature_memcache] - this makes PHP search for two constants: cfg and disable_feature_memcache. And throws two notices, which are probably ignored. First, these should be used as strings (eg. $GLOBALS['cfg']['disable_feature_memcache']) becouse "it is the right way" :) and secondly, it reduces the overhead of PHP searching for something that does not exist. This way you will surely get less CPU spikes you talk about from your PHP code.

Oh, and yes, I will be looking forward to reading about your opinion why objects/classes in PHP are bad.

Good luck!

Dathan said...

@sk
Yea I'm lazy with constants in this fashion-I'll fix that up.

OO PHP programming is hard to trace. If a framework is not established, its very easy for multiple coders to create a huge spaghetti mess of methods that do nested inheritance, side affect public vars, and create unnecessary overhead.

I measured that each instance of PEAR DB.php has a 4K overhead. Which is huge, and directly attributed to the object!

Now, I've seen some really good OOP PHP classes and its actually been a pleasure working in that environment.

What I found to be the easiest methods of coding with multiple developers is procedural coding in PHP.

file_name.inc
function names are

file_name_Function(vars){}

So, anytime you reference a function you know where to debug it immediately.

Anonymous said...

I'm new to PHP and recently setup my local machine with PHP and MySQL for doing development. I was sort of stuck when I needed to post my work

for the user to test and review. After looking around a bit I found a site that hosts PHP and MySQL apps. I was surprised that it was free -

it seems they're offering the service at no cost until 2012. At that point they'll change over to a fee-based service. However, in the

meantime, it's a great place to do anything from demo and sandbox right up to posting sites for real.

Their pitch is as follows:

"This is absolutely free, there is no catch. You get 350 MB of disk space and 100 GB bandwidth. They also have cPanel control panel which is

amazing and easy to use website builder. Moreover, there is not any kind of advertising on your pages."

Check it out using this link:

http://www.000webhost.com/83188.html

Important: There's one catch in that you must make sure you visit the account every 14 days - otherwise the account is marked 'Inactive' and

the files are deleted!!!

Thanks and good luck!

sexy said...

情趣用品,情趣用品,情趣用品,情趣用品,情趣用品,情趣用品,情趣用品,情趣用品,情趣,情趣,情趣,情趣,情趣,情趣,情趣,情趣,按摩棒,跳蛋,充氣娃娃,情境坊歡愉用品,情趣用品,情人節禮物,情惑用品性易購

免費A片,AV女優,美女視訊,情色交友,免費AV,色情網站,辣妹視訊,美女交友,色情影片,成人影片,成人網站,A片,H漫,18成人,成人圖片,成人漫畫,情色網,日本A片,免費A片下載,性愛

A片,色情,成人,做愛,情色文學,A片下載,色情遊戲,色情影片,色情聊天室,情色電影,免費視訊,免費視訊聊天,免費視訊聊天室,一葉情貼圖片區,情色,情色視訊,免費成人影片,視訊交友,視訊聊天,視訊聊天室,言情小說,愛情小說,AIO,AV片,A漫,av dvd,聊天室,自拍,情色論壇,視訊美女,AV成人網,色情A片,SEX

情趣用品,A片,免費A片,AV女優,美女視訊,情色交友,色情網站,免費AV,辣妹視訊,美女交友,色情影片,成人網站,H漫,18成人,成人圖片,成人漫畫,成人影片,情色網


情趣用品,A片,免費A片,日本A片,A片下載,線上A片,成人電影,嘟嘟成人網,成人,成人貼圖,成人交友,成人圖片,18成人,成人小說,成人圖片區,微風成人區,成人文章,成人影城,情色,情色貼圖,色情聊天室,情色視訊,情色文學,色情小說,情色小說,臺灣情色網,色情,情色電影,色情遊戲,嘟嘟情人色網,麗的色遊戲,情色論壇,色情網站,一葉情貼圖片區,做愛,性愛,美女視訊,辣妹視訊,視訊聊天室,視訊交友網,免費視訊聊天,美女交友,做愛影片

av,情趣用品,a片,成人電影,微風成人,嘟嘟成人網,成人,成人貼圖,成人交友,成人圖片,18成人,成人小說,成人圖片區,成人文章,成人影城,愛情公寓,情色,情色貼圖,色情聊天室,情色視訊,情色文學,色情小說,情色小說,色情,寄情築園小遊戲,情色電影,aio,av女優,AV,免費A片,日本a片,美女視訊,辣妹視訊,聊天室,美女交友,成人光碟

情趣用品.A片,情色,情色貼圖,色情聊天室,情色視訊,情色文學,色情小說,情色小說,色情,寄情築園小遊戲,情色電影,色情遊戲,色情網站,聊天室,ut聊天室,豆豆聊天室,美女視訊,辣妹視訊,視訊聊天室,視訊交友網,免費視訊聊天,免費A片,日本a片,a片下載,線上a片,av女優,av,成人電影,成人,成人貼圖,成人交友,成人圖片,18成人,成人小說,成人圖片區,成人文章,成人影城,成人網站,自拍,尋夢園聊天室

sex said...

徵信社
情趣用品
情趣用品
情趣用品
情趣
情趣


SM
充氣娃娃


SM
性感睡衣


免費視訊聊天室
aio交友愛情館
愛情公寓
情色貼圖
情色文學
情色小說
情色電影
情色論壇
成人論壇
辣妹視訊
視訊聊天室
情色視訊
免費視訊

免費視訊聊天
視訊交友網
視訊聊天室
視訊美女
視訊交友
ut聊天室
聊天室
豆豆聊天室
尋夢園聊天室
聊天室尋夢園
080聊天室
080苗栗人聊天室
女同志聊天室

上班族聊天室
小高聊天室






免費視訊聊天
免費視訊聊天室
免費視訊
ut聊天室
聊天室
豆豆聊天室 聊天室尋夢園
影音視訊聊天室


色情遊戲
寄情築園小遊戲
情人視訊網
辣妹視訊
情色交友

成人論壇
情色論壇
愛情公寓
情色
色情聊天室
色情小說
做愛
做愛影片
性愛


一葉情貼圖片區
情趣用品


辣妹視訊
美女視訊
視訊交友網
視訊聊天室
視訊交友
視訊美女

Anonymous said...

A片,A片,成人網站,成人漫畫,色情,情色網,情色,AV,AV女優,成人影城,成人,色情A片,日本AV,免費成人影片,成人影片,SEX,免費A片,A片下載,免費A片下載,做愛,情色A片,色情影片,H漫,A漫,18成人

a片,色情影片,情色電影,a片,色情,情色網,情色,av,av女優,成人影城,成人,色情a片,日本av,免費成人影片,成人影片,情色a片,sex,免費a片,a片下載,免費a片下載

情趣用品,情趣用品,情趣,情趣,情趣用品,情趣用品,情趣,情趣,情趣用品,情趣用品,情趣,情趣

A片,A片,A片下載,做愛,成人電影,.18成人,日本A片,情色小說,情色電影,成人影城,自拍,情色論壇,成人論壇,情色貼圖,情色,免費A片,成人,成人網站,成人圖片,AV女優,成人光碟,色情,色情影片,免費A片下載,SEX,AV,色情網站,本土自拍,性愛,成人影片,情色文學,成人文章,成人圖片區,成人貼圖

情色文學,色情小說,色情,寄情築園小遊戲,AIO交友愛情館,情色電影,一葉情貼圖片區,色情遊戲

言情小說,情色論壇,色情網站,微風成人,成人電影,嘟嘟成人網,成人,成人貼圖,成人交友,成人圖片,18成人,成人小說,成人圖片區,微風成人區,成人網站,免費影片,色情影片,自拍,hilive,做愛,微風成人,微風論壇,AIO