📜 ⬆️ ⬇️

Simple additional control of memcached data status

image Monitoring memcached is not the last thing. Both at the stage of testing and at the stage of maintenance of an already working loaded resource. There are not so many tools out of the box for this, and if you are working with PHP, you are often limited to memcache (or memcached), namely

Memcache :: getStats ()

$memcache = new Memcache;
$memcache->connect('localhost',11211);
print_r($memcache->getStats());

')
which returns a typical data set

Array ( [pid] => 25722 [uptime] => 4487286 [time] => 1308323074 [version] => 1.2.2 [pointer_size] => 64 [rusage_user] => 2646.005365 [rusage_system] => 17108.873237 [curr_items] => 37761 [total_items] => 10764857 [bytes] => 140070186 [curr_connections] => 5 [total_connections] => 17360659 [connection_structures] => 31 [cmd_get] => 89154830 [cmd_set] => 10764857 [get_hits] => 83452021 [get_misses] => 5702809 [evictions] => 0 [bytes_read] => 3527860756618 [bytes_written] => 4234517241183 [limit_maxbytes] => 2147483648 [threads] => 1 )

It seems all is well.
We see that we have 133.5 Mb of 2 Gb allocated for memcached and about 37 thousand keys.
Hits misses refer to as 83/5, which also does not inspire fear.


But! What we see is not what we need! (Well, or at least I needed it). Indeed, among these keys are those that are outdated. And, running a little ahead, I will say that there are even more of them than the “live” values, and usually 10 times (unless of course the toad strangled you to allocate an extremely sufficient amount of memory). For the garbage collector kills outdated (or not outdated) keys for 2 reasons:
1) Out of free memory. (here and current can fly away)
2) The data was requested and turned out to be expired.

There is a way out for this, and it is simple to ugliness - periodically polling all the keys.
PHP is not the best option for this. I wanted something “closer to the body”.
Solved. We write on bash / sh and put it on cron. In my case, 1 launch is enough at the end of the main “loaded” time (we have about 21-30 Moscow time).

As a server - quite pop Debian Lenny.

So, we need libmemcached, its memdump (displaying a complete list of keys) and memccat (data request by key), bash / sh, and a couple of minutes ...

#!/bin/sh
cd /usr/local/src/libmemcached-0.49/clients

./memdump --servers=127.0.0.1 >/home/memdump.dat

while read i; do

memccat $i --servers=127.0.0.1 >/dev/null 2>&1

done </home/memdump.dat

#-----------------------------------------
# ,
#
# - 1 .
# too long....


It is especially convenient to see the real number of “live” sessions that we also have in the memkesh.

It remains to add: on the production server, once a day, about 850-1000 Mb of data of the keys is killed (from 2 GB allocated) and I almost always have real data about the state of memcached.

Source: https://habr.com/ru/post/122094/


All Articles