Memcached modules behaviour

Greetings,

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? Any kind of hashing is done? Round-
robin? While we’re at it, does eval module reuse the default nginx
memcache module implementation?

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).

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)
  2. use only one server for memcached
  3. have my PHP do a set for every server in the pool

#2 is of course the easiest, not necessarily the safer. Thanks in
advance.

Pedro.

On Tue, Nov 10, 2009 at 2:23 AM, Pedro Mata-Mouros Fonseca
[email protected] 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 :wink:

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 :slight_smile:

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 :wink:

Hope this helps :slight_smile:

Cheers,
-agentzh

Thanks for the explanation. In the meantime I kind of went around the
problem, using the nginx consistent hash module for memcached upstream
gets and also that same algorithm in PHP’s sets. Working flawlessly.

Thanks again!

Pedro.

On Thu, Nov 12, 2009 at 7:13 PM, Pedro Mata-Mouros Fonseca
[email protected] wrote:

Thanks for the explanation.

You’re very welcome :slight_smile:

In the meantime I kind of went around the
problem, using the nginx consistent hash module for memcached upstream gets
and also that same algorithm in PHP’s sets. Working flawlessly.

Oh, that sounds really cool! Thanks a lot for sharing this tip :smiley:
I’ll try it out myself.

Thanks!
-agentzh

Dear all:
       Does nginx has a module as apace module mod_speling?
       Thanks


隨身的 Windows Live Messenger 和 Hotmail,不限時地掌握資訊盡在指間 — Windows Live for
Mobile
http://3c.msn.com.tw/mobile/