inconsistent hashing?

32 views
Skip to first unread message

Stuart

unread,
Mar 28, 2009, 8:37:44 AM3/28/09
to spymemcached
I'm new to memcached, I've written a simple application (using spy
memcached), and I'm seeing behavior that confuses me.

I start memcached on 4 nodes using the -vv option. I run a simple
utility that puts a single key/value pair into the cache (key="foo",
value="bar"),

MemcachedClient memcachedClient = new MemcachedClient
(AddrUtil.getAddresses("ngcwydev102:11211 ngcwydev103:11211
ngcwydev104:11211 ngcwydev105:11211"));

Future<Boolean> result = memcachedClient.set("foo",
Integer.MAX_VALUE, "bar");
System.out.println("Result: " + result.get(2, TimeUnit.SECONDS));

Then, I repeatedly run a simple utility that gets the value of
"foo".

MemcachedClient memcachedClient = new MemcachedClient
(AddrUtil.getAddresses("ngcwydev102:11211 ngcwydev103:11211
ngcwydev104:11211 ngcwydev105:11211"));
String value = (String) memcachedClient.get("foo");
System.out.println("foo=" + value);

Most of the time, "bar" is returned as expected. However, many times
"null" is returned. Because I launched the memcached processes using -
vv, I can see that, sure enough, when "bar" gets returned, one
memcached service is handling the request, and when "null" gets
returned, a different memcached service is handling the request.

It appears the "foo" key is getting hashed differently during
different runs of my client, resulting in it being sent to different
memcached services. Is this behavior to be expected?

Dustin

unread,
Mar 28, 2009, 4:23:58 PM3/28/09
to spymemcached

On Mar 28, 5:37 am, Stuart <stuart.whi...@gmail.com> wrote:

>    MemcachedClient memcachedClient = new MemcachedClient
> (AddrUtil.getAddresses("ngcwydev102:11211 ngcwydev103:11211
> ngcwydev104:11211 ngcwydev105:11211"));
>    String value = (String) memcachedClient.get("foo");
>    System.out.println("foo=" + value);
>
> Most of the time, "bar" is returned as expected.  However, many times
> "null" is returned.  Because I launched the memcached processes using -
> vv, I can see that, sure enough, when "bar" gets returned, one
> memcached service is handling the request, and when "null" gets
> returned, a different memcached service is handling the request.
>
> It appears the "foo" key is getting hashed differently during
> different runs of my client, resulting in it being sent to different
> memcached services.  Is this behavior to be expected?

No, it should always go to the same server if it's available.

Are you actually establishing the connection in the loop? That
could be the problem, as it may be failing to talk to the ``correct''
server sometimes.

Stuart White

unread,
Mar 28, 2009, 4:59:21 PM3/28/09
to spymem...@googlegroups.com
>  No, it should always go to the same server if it's available.
>
>  Are you actually establishing the connection in the loop?  That
> could be the problem, as it may be failing to talk to the ``correct''
> server sometimes.

How can I determine whether I've established the connection
successfully? Thanks!

Dustin

unread,
Mar 28, 2009, 5:28:57 PM3/28/09
to spymemcached

On Mar 28, 1:59 pm, Stuart White <stuart.whi...@gmail.com> wrote:

> How can I determine whether I've established the connection
> successfully?  Thanks!

I wouldn't worry about it too much if you're seeing the problem when
you're making a lot of short-lived connections. That's not a good
idea for a variety of reasons, so you should fix that and verify that
it does what you want.

You can use a connection observer to decrement a latch to confirm
that all (or enough) of your servers are up before doing your test. I
do that in one of my unit tests.
Reply all
Reply to author
Forward
0 new messages