Flushing memcached servers the easy way

This is easy right?  Can’t you just restart the memcached server? Well yes, but you may cause errors in applications that are already connected to it. You can follow your memcached restart with an application restart, eg for a Ruby on Rails app:

# /etc/init.d/memcached restart && mongrel_rails cluster::restart

Of course if you have more than one application server you have to restart your app on every single one. This would work on an engineyard slice assuming you have the eycap gem installed:

$ cap production memcached:restart
$ cap production mongrel:restart

Restarting your application is not ideal however, you will lose anything cached in memory, cause delays to users trying to access your site, that sort of thing.

So what can be done? The answer is really simple. Assuming a memcached running on the local machine on the default port:

$ echo “flush_all” | nc localhost 11211


7 thoughts on “Flushing memcached servers the easy way

  1. Morten says:

    Nice Will. How about adding memcached:flush to the eycap gem? :-)

  2. Shai says:

    Genius Post! gotta <3 it. :)

  3. Fred says:

    Very Nice tip.

    you can also telnet to memcached and run many command such as stats

    $ telnet localhost 11211


  4. teo caber says:

    nice, thx

    as sidenote, you can do it throu telnet too:
    telnet localhost 11211

  5. Luca de Marinis says:

    Hi, I noticed that nc hangs (for longer than I cared to check!) after sending the command.. how about adding -w 3 to have it close the socket after 3 seconds of inactivity? (I also tried -q, which looked even better, but sometimes it closed before I could read any answer back)

  6. […] Flushing memcached servers the easy way I highlighted a way to flush a memcached server without restarting […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s