lockd, a memcached like locking daemon in php

You might have seen my last post about this code (here) if you are one of my 2.3 loyal readers 😉 . It wasnt nearly so complete or robust then (segfaulting? come on! right?)

The segfaulting was caused by stupidly using $this->function() from inside of $this->function() for recursion, overloading the stack. Note to self: don’t be lazy about recursion 😀

If you’re interested in the project I have it setup over here: http://code.svn.wordpress.org/lockd/ so please feel free to try it out.

 

What is it?  It’s a network based locking mechanism.  You can connect to it over TCPIP and get either exclusive or shared locks for as many arbitrary strings as you like.  Shared locks have the added benefit of being counted.  You can query to see if a string is locked (and how many times for shared locks.) You can also release locks.

 

All of this is useful, but… The nice thing about lockd, though, is that if your client disconnects (say the process which had locked a string quit unexpectedly) lockd will automatically orphan those locks for you.  So you have no need to try and cleanup and guess the lock state of a failed process.   And since this runs as a network daemon you can have as many of them as you like.  You can use them for local (locks on the same box) or distributed (locks on the same resource from many boxes.)  It also comes with built in stats which you could hook up to munin, or nagios, via netcat without too much trouble.

 

The protocol is also extremely simple

  • g (string) — get an exclusive lock on the string
  • sg (string) — get a shared lock on the string
  • r (string) — release an exclusive lock
  • sr (string) — release a shared lock
  • i (string) — inspect an exclusive lock
  • si (string) — inspect a shared lock
  • q — query stats
  • q full — query stats and dump the exclusive and shared lock arrays

 

Thats it.  “(int) (string)” is returned for these commands. where (int) is for programmatic use, and (string) is for human consumption.

 

We use this with the Jobs system (also available as an OSS project) for WordPress.com, Intensedebate, Gravatar, and more, and is currently handling nearly a million operations per day… per server…

 

This is Itch-Scratch-Ware at its finest.

2 thoughts on “lockd, a memcached like locking daemon in php

  1. Pingback: ehcache.net

Leave a Reply