Memcached modules behaviour

agentzh agentzh at
Thu Nov 12 04:51:59 MSK 2009

On Tue, Nov 10, 2009 at 2:23 AM, Pedro Mata-Mouros Fonseca
<pedro.matamouros at> wrote:
> I have a pool of two memcached servers that I use in nginx for querying
> (using Valery's eval module) and, in case of cache miss, for setting with
> the key/value pair in a PHP script. How does nginx/eval module query this
> memcached pool?

The nginx "memcached" module will first try the first upstream
memcached server, if hit by any of the error conditions specified by
"memcached_next_upstream", then try the next. (The actual logic is
actually implemented in the "upstream" module which "memcached" is
relied on.)

> Any kind of hashing is done? Round-robin?

Nope, no magic will happen, completely different from libmemcached ;)

> While we're at it,
> does eval module reuse the default nginx memcache module implementation?

Yup, behind the scene, the "eval" module creates a temporary
"location" for the memcached settings, and directly sends a subrequest
to that "location", and in its "post_subrequest" handler, "eval" just
reads the buffered response generated by the "memcached" module (well,
it requires the actual upstream module supports "in-meomory
subrequests" and "memcached" fortunately meets the requirement here
but "fastcgi" does not IIRC.)

> The original problem is that I'm having memcached key/value sets in PHP
> being done to a specific server (some key hashing algorithm is used with the
> PECL memcache extension), while later on nginx appears to be querying both
> servers (causing a cache miss when contacting the wrong server).

This is the expected behavior from the perspective of the actual
implementation :)

> Any suggestions on how to solve this? Right now, the options I'm seeing are:
> 1. extend either nginx or PECL's memcache (or memcached)

I'd vote for this option. And I'm going to work on this myself because
it's now part of my $work.

A note to the implementation details: be careful to use exactly the
same escaping function as the nginx memcached module, or you'll have
mysterious cache miss even if you get the key-hashing-to-host process
right. Please ensure you have grokked the source of the "memcached"
module (and possibly the "eval" module). They're *very* concise after
all ;)

Hope this helps :)


More information about the nginx mailing list