I'm testing out using memcached to cache django views. How can I tell if memcached is actually caching anything from the Linux command line?
14 Answers
You could use the official perl script:
memcached-tool 127.0.0.1:11211 stats
Or just use telnet and the stats command e.g.:
# telnet localhost [memcacheport]
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
stats
STAT pid 2239
STAT uptime 10228704
STAT time 1236714928
STAT version 1.2.3
STAT pointer_size 32
STAT rusage_user 2781.185813
STAT rusage_system 2187.764726
STAT curr_items 598669
STAT total_items 31363235
STAT bytes 37540884
STAT curr_connections 131
STAT total_connections 8666
STAT connection_structures 267
STAT cmd_get 27
STAT cmd_set 30694598
STAT get_hits 16
STAT get_misses 11
STAT evictions 0
STAT bytes_read 2346004016
STAT bytes_written 388732988
STAT limit_maxbytes 268435456
STAT threads 4
END

- 21,706
- 2
- 31
- 35
-
36`memcached-tool 127.0.0.1:11211 stats` - you do not need to telnet. – JMHeap Jan 21 '13 at 16:05
-
^^ This comment is the correct way to test memcached. I use this to test connectivity to my remote memcached clusters. – DrStrangepork May 20 '14 at 15:28
-
1@JMHeap I ran that command and showing some random texts, how do we confirm that memcache is running.. – shajin Sep 16 '14 at 13:33
-
10To anyone struggling to find memcached-tool - on ubuntu it's `/usr/share/memcached/scripts/memcached-tool`. – Aurelijus Rozenas Jul 22 '16 at 05:23
-
Also try `/usr/share/memcached/scripts/memcached-tool /tmp/memcached.sock stats` or `/usr/share/memcached/scripts/memcached-tool /var/run/memcached/memcached.sock stats` (socket case) – Hassan Baig Jul 24 '20 at 02:54
I know this question is old, but here is another useful approach for testing memcached with django:
As @Jacob mentioned, you can start memcached in very verbose mode (not as a daemon):
memcached -vv
To test your django cache config, you can use the low-level cache api.
First, start up the python interpreter and load your django project settings:
python manage.py shell
From the shell, you can use the low-level cache api to test your memcache server:
from django.core.cache import cache cache.set('test', 'test value')
If your cache configuration is correct, you should see output in memcache similar to this:
<32 set :1:test 0 300 10
>32 STORED

- 2,650
- 1
- 19
- 9
-
4Also - worth noting that you need to stop the already running memcache instance before you run memcached -vv Otherwise you will have two instances running and django will still be setting the cache in the first one. – Monika Sulik Apr 03 '14 at 10:38
-
1Thanks for the thorough explanation, this explains how to check the cache if you don't already know how it works – Santiago Angel Oct 15 '15 at 18:14
-
1This is the faster and most reliable way to test if memcached is working. Should be the accepted answer. – Drubio May 22 '19 at 10:29
Start memcache not as a daemon but normal, so just run memcached -vv
for very verbose. You will see when get's and sets come in to the memcache server.

- 79,954
- 26
- 128
- 166
From the command line, try the command below:
echo stats | nc 127.0.0.1 11211
If it doesn't return anything, memcache isn't running. Otherwise it should return a bunch of stats including uptime (and hit and miss counts)
The reference article is here, https://www.percona.com/blog/2008/11/26/a-quick-way-to-get-memcached-status/
To see changes every 2 seconds:
watch "echo stats | nc 127.0.0.1 11211"

- 7,484
- 4
- 35
- 44

- 327
- 3
- 8
-
simple and clear, its great to wrap it inside python code like `subprocess.Popen('echo stats | nc 127.0.0.1 11211', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE )` – Evhz Mar 21 '18 at 14:06
-
If you want to use this technique for a health check, choose carefully your netcat flavour. For example, on FreeBSD, this works for `ncat` (nmap netcat), but for `nc` (the one shipped in FreeBSD base system), connection stays open, so you won't have a clean exit with an exit code 0. – Dereckson Mar 07 '23 at 23:42
Simple way to test for memcache working was to sneak in a commented out timestamp on every page served up. If the timestamp stayed the same on multiple requests to a page, then the page was being cached by memcache.
In Django settings, I also setup the cache mechanism to use a file cache on the filesystem (really slow), but after hitting up the pages I could see that there were actual cache files being placed in the file path so I could confirm caching was active in Django.
I used both these steps to work out my caching problem. I actually did not have caching turned on correctly in Django. The newer method to activate caching is using the 'django.middleware.cache.CacheMiddleware' middleware (not the middleware with two middleware pieces that have to be the first/last middleware settings.)

- 45,039
- 49
- 151
- 227
Memcached can actually write to a logfile on its own, without having to resort to restarting it manually. The /etc/init.d/memcached
init script (/usr/lib/systemd/system/memcached.service
on EL7+; ugh) can call memcached with the options specified in /etc/memcached.conf
(or /etc/sysconfig/memcached
on EL5+). Among these options are verbosity and log file path.
In short, you just need to add (or uncomment) these two lines to that conf/sysconfig file...
-vv
logfile /path/to/log
...and restart the daemon with service memcached restart
(EL3-7) or /etc/init.d/memcached restart
(debuntus)
And then you can monitor this log in the traditional way, like tail -f /path/to/log
, for example.

- 444
- 1
- 4
- 23

- 61
- 1
- 3
In Bash, you can check the statistics of memcache by this command:
exec 3<>/dev/tcp/localhost/11211; printf "stats\nquit\n" >&3; cat <&3
To flush the cache, use memflush
command:
echo flush_all >/dev/tcp/localhost/11211
and check if the stats increased.
To dump all the cached objects, use memdump
or memcdump
command (part of memcached
/libmemcached
package):
memcdump --servers=localhost:11211
or:
memdump --servers=localhost:11211
If you're using PHP, to see whether is supported, check by: php -i | grep memcached
.
Tracing
To check what memcached process is exactly processing, you can use network sniffers or debuggers (e.g. strace
on Linux or dtrace
/dtruss
on Unix/OS X) for that. Check some examples below.
Strace
sudo strace -e read,write -fp $(pgrep memcached)
To format output in a better way, check: How to parse strace in shell into plain text?
Dtruss
Dtruss is a dtrace wrapper which is available on Unix systems. Run it as:
sudo dtruss -t read -fp $(pgrep memcached)
Tcpdump
sudo tcpdump -i lo0 -s1500 -w- -ln port 11211 | strings -10

- 155,785
- 88
- 678
- 743
-
Which package provides memdump? I tried `yum install memdump` and that failed. – Martin Jun 21 '17 at 06:59
-
@Martin I believe `memdump` is part of `memcached` (or `libmemcached`), so try: `yum install memcached`. – kenorb Jun 21 '17 at 08:50
-
-
For extend Node's response, you can use socat UNIX-CONNECT:/var/run/memcached.sock STDIN
to debug a unix socket.
Example:
$ socat UNIX-CONNECT:/var/run/memcached.sock STDIN
stats
STAT pid 931
STAT uptime 10
STAT time 1378574384
STAT version 1.4.13
STAT libevent 2.0.19-stable
STAT pointer_size 32
STAT rusage_user 0.000000
STAT rusage_system 0.015625
STAT curr_connections 1
STAT total_connections 2
STAT connection_structures 2

- 8,765
- 9
- 49
- 56
You can test memcached or any server by below script
lsof -i :11211 | grep 'LISTEN'>/dev/null 2>/dev/null;echo $?
if it returns 0 then the server is actually running or if 1 its not so if you want to know that the server is actually running on some port use the following script
lsof -i :11211 | grep 'LISTEN'>/dev/null 2>/dev/null;
if [ $? -eq 0]; then
echo "Your memcache server is running"
else
echo "No its not running"
fi

- 148
- 1
- 9
I wrote an expect
script is-memcached-running
that tests if memcached is running on a host/port combination (run as is-memcached-running localhost 11211
):
#! /usr/bin/env expect
set timeout 1
set ip [lindex $argv 0]
set port [lindex $argv 1]
spawn telnet $ip $port
expect "Escape character is '^]'."
send stats\r
expect "END"
send quit\r
expect eof
If you run your system from a Makefile
rule, you could make your startup depend on a make target that asserts it is up and running (or helps you get that state). It is verbose when the check fails to make it easy for us to debug failed ci runs, installs memcached when it's missing, and is brief and to the point otherwise:
#! /bin/bash
if [[ "$(type -P memcached)" ]]; then
echo 'memcached installed; checking if it is running'
memcached_debug=`mktemp memcache-check.XXXXX`
if is-memcached-running localhost 11211 >$memcached_debug 2>&1; then
echo 'Yep; memcached online'
else
cat $memcached_debug
echo
echo '****** Error: memcached is not running! ******'
if [[ "$OSTYPE" =~ ^darwin ]]; then
echo
echo 'Instructions to auto-spawn on login (or just start now) are shown'
echo 'at the end of a "brew install memcached" run (try now, if you did'
echo 'not do so already) or, if you did, after a "brew info memcached".'
echo
fi
exit 1
fi
rm -f $memcached_debug
else
echo memcached was not found on your system.
if [[ "$OSTYPE" =~ ^darwin ]]; then
brew install memcached
elif [[ "$OSTYPE" =~ ^linux ]]; then
sudo apt-get install memcached
else
exit 1
fi
fi

- 5,030
- 2
- 44
- 66
Following Aryashree post, this helped me to get an error if memcached not running locally:
import subprocess
port = 11211
res = subprocess.Popen(f"echo stats | nc 127.0.0.1 {port}",
shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if res.stdout:
lines = res.stdout.read()
lineArr = lines.split('\r\n')
pidlineArr = lineArr[0].split(' ')
pid = pidlineArr[-1]
print(f"[MemCached] pid {pid} Running on port {port}")
else:
raise RuntimeError(f"No Memcached is present on port {port}")

- 8,852
- 9
- 51
- 69
Can you use curl to fetch a page a few hundred times and time the results? You could also look at running a process on the server that simulates heavy CPU/disk load while doing this.

- 58,260
- 22
- 130
- 143
I'm using Mezzanine and the only answer that worked for me was Jacobs answer. So stopping the daemon and running memcached -vv

- 1,146
- 11
- 16
-
Consider comenting on the original answer or just voting for the answer that worked for you – wranvaud Sep 19 '16 at 13:50
If you're using RHEL or Centos 8 To get memcached log stuff to /var/log/messages (quick without rotation)

- 1,299
- 11
- 15