# Memcached modules behaviour

**URL:** <https://www.ruby-forum.com/t/memcached-modules-behaviour/178700>\
**Category:** NGINX\
**Created:** [November 9, 2009, 7:26pm UTC](https://www.ruby-forum.com/t/memcached-modules-behaviour/178700 "2009-11-09T19:26:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pedro\_Mata-Mouros\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dec6dc/32.png) [@Pedro\_Mata-Mouros\_F](https://www.ruby-forum.com/u/Pedro_Mata-Mouros_F)\
**Post date:** [November 9, 2009, 7:26pm UTC](https://www.ruby-forum.com/t/memcached-modules-behaviour/178700/1 "2009-11-09T19:26:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Pedro\_Mata-Mouros\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dec6dc/32.png) [@Pedro\_Mata-Mouros\_F](https://www.ruby-forum.com/u/Pedro_Mata-Mouros_F)\
**Post date:** [November 12, 2009, 3:02am UTC](https://www.ruby-forum.com/t/memcached-modules-behaviour/178700/2 "2009-11-12T03:02:55Z")

</div>

On Tue, Nov 10, 2009 at 2:23 AM, Pedro Mata-Mouros Fonseca  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 🙂

Cheers,  
-agentzh

---

<div class="post-metadata">

**Author:** ![Pedro\_Mata-Mouros\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dec6dc/32.png) [@Pedro\_Mata-Mouros\_F](https://www.ruby-forum.com/u/Pedro_Mata-Mouros_F)\
**Post date:** [November 12, 2009, 12:58pm UTC](https://www.ruby-forum.com/t/memcached-modules-behaviour/178700/3 "2009-11-12T12:58:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Pedro\_Mata-Mouros\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dec6dc/32.png) [@Pedro\_Mata-Mouros\_F](https://www.ruby-forum.com/u/Pedro_Mata-Mouros_F)\
**Post date:** [November 17, 2009, 4:50am UTC](https://www.ruby-forum.com/t/memcached-modules-behaviour/178700/4 "2009-11-17T04:50:16Z")

</div>

On Thu, Nov 12, 2009 at 7:13 PM, Pedro Mata-Mouros Fonseca  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks for the explanation.

You’re very welcome 🙂

> 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 😃  
I’ll try it out myself.

Thanks!  
-agentzh

---

<div class="post-metadata">

**Author:** ![Pedro\_Mata-Mouros\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dec6dc/32.png) [@Pedro\_Mata-Mouros\_F](https://www.ruby-forum.com/u/Pedro_Mata-Mouros_F)\
**Post date:** [November 17, 2009, 6:10am UTC](https://www.ruby-forum.com/t/memcached-modules-behaviour/178700/5 "2009-11-17T06:10:27Z")

</div>

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/](http://3c.msn.com.tw/mobile/)
