Hashing uses port number?

Chris Newland chrisn at allipo.com
Mon Sep 4 10:11:22 UTC 2006


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