Hashing uses port number?

Mikael Johansson mikael at synd.info
Mon Sep 4 21:49:34 UTC 2006


The behavior of Memcache::connect() was to always reinitialize the pool
from scratch regardless of any previous calls to addServer(). Your last
connect() call would therefore clear out the pool and then add and
connect 127.0.0.1:11211 making it the only server.

The new behavior of Memcache::connect() (currently in pecl/memcache CVS
only) is to add the server, much like addServer() except that the server
is connected immediately instead of automatically-when-needed.

Please note that if you specify a server to addServer() and again to
connect() it will actually be added to the pool twice. In your later
example

> $mmc = new Memcache()
> $mmc->addServer('127.0.0.1', 11211);
> $mmc->addServer('127.0.0.1', 11212);
> $mmc->addServer('127.0.0.1', 11213);
>
> $mmc->connect('127.0.0.1', 11211);

Using the new code from CVS, you would have a pool looking as

 127.0.0.1:11211 (1 bucket, connected on demand)
 127.0.0.1:11212 (1 bucket, connected on demand)
 127.0.0.1:11213 (1 bucket, connected on demand)
 127.0.0.1:11211 (1 bucket, forcibly connected)

//Mikael

Chris Newland wrote:
> Hi Mikael,
> 
> Using your suggestion to not use connect() solved the problem but I
> don't understand why the data is not evenly spread when I use connect().
> I'm using random set of keys to test this behaviour.
> 
> ====================
> No Connect
> ====================
> 
> $mmc = new Memcache()
> $mmc->addServer('127.0.0.1', 11211);
> $mmc->addServer('127.0.0.1', 11212);
> $mmc->addServer('127.0.0.1', 11213);
> 
> $val =
> "doifgsidfhgilshdflghsldifglisdfhgilhsdlfighsldfighslkhvmilemhrilcftmsilemhg";
>       
> for ($i = 0; $i < 100; $i++)
> {          
>     $key = rand(0, 10000000);
> 
>     $mmc->set($key, $val);   
> }
> 
> Result as expected
> 
> 127.0.0.1:11211 curr_items = 28
> 127.0.0.1:11212 curr_items = 38
> 127.0.0.1:11213 curr_items = 34
> 
> ============================
> But when I use connect:
> ============================
> 
> $mmc = new Memcache()
> $mmc->addServer('127.0.0.1', 11211);
> $mmc->addServer('127.0.0.1', 11212);
> $mmc->addServer('127.0.0.1', 11213);
> 
> $mmc->connect('127.0.0.1', 11211);
> 
> $val =
> "doifgsidfhgilshdflghsldifglisdfhgilhsdlfighsldfighslkhvmilemhrilcftmsilemhg";
>       
> for ($i = 0; $i < 100; $i++)
> {           
> 
>     $key = rand(0, 10000000);
> 
>     $mmc->set($key, $val);    
> 
> }
> 
> Result
> 
> 127.0.0.1:11211 curr_items = 100
> 127.0.0.1:11212 not present in result of getExtendedStats()
> 127.0.0.1:11213 not present in result of getExtendedStats()
> 
> Using connect() seems to make it ignore the other servers.
> 
> Regards,
> 
> Chris
> 
> Mikael Johansson wrote:
> 
>>> Each call to Memcache::addServer() adds $weight (default = 1) number of
>>> buckets to the list of possible servers to select for a given key. When
>>> selecting a server to serve a given key, the key is hashed using (more
>>> or less) crc32(key)%total_number_of_buckets.
>>>
>>> The short answer is that yes, the port is considered in the hashing
>>> algorithm. If your all your data is stored on the first server it's
>>> because all the keys you are using map to that specific servers buckets.
>>> Using memcache_connect() or only addServer() shouldn't make a
>>> difference, though the hignest performance can be found using the
>>> extension as
>>>
>>>  $mmc = new Memcache()
>>>  $mmc->addServer('node01.example.com', 11211);
>>>  $mmc->addServer('node02.example.com', 11211);
>>>  $mmc->addServer('node03.example.com', 11211);
>>>
>>>  $mmc->set('foo', 'bar');
>>>
>>> Since addServer() will not connect to the server until actually needed
>>> (such as when the server is selected for a set/get operation)
>>>
>>> //Mikael
>>>
>>> Chris Newland wrote:
>>>   
>>   
>>>>> Hi,
>>>>>
>>>>> I'm trying to simulate my production environment by running multiple
>>>>> memcached servers on my dev machine (on different ports).
>>>>>
>>>>> I can add the servers to the pool, and getExtendedStats for all of them
>>>>> ok but when I try to write data, it only goes to the memcached that I
>>>>> have connected to. (Using getExtendedStats curr_items to determine this).
>>>>>
>>>>> Can someone let me know if the port is considered in the hashing algorithm?
>>>>>
>>>>> I hope not as that would be an easy explanation.
>>>>>
>>>>> I'm using Debian Sarge, memcached 2.0.4, PHP PECL memcache client.
>>>>>
>>>>> Many thanks,
>>>>>
>>>>> Chris
>>>>>     
>>>     
> 
> 
> 


More information about the memcached mailing list