Given a cname, how does memcache client resolves to the right node in the cluster


I’m trying to understand how the resolution of the right node happens when you specify the cname for the cluster.

PresenceCache getPresenceCache(CacheConfig config)
    List<InetSocketAddress> servers = "";
    try {
        return new MemcachedPresenceCache(new MemcachedClient(servers));
    catch (IOException e) {
        throw Throwables.propagate(e);

So in the above code, given the cname, I want to know how the key decides to go to a particular node. This cname has 2 nodes in it. I tried debugging, so every time you start a service, one of the node IP address is returned. Here the nodes.length is 1 and not 2. Hence I’m thinking may be the keys are written to whatever IP address the code resolves to when the application starts and then it tries reading and writing from the same node, instead of resolving to the actual node where the key lies.

private int getServerForKey(String key) {
  int rv = (int) (hashAlg.hash(key) % nodes.length);
  assert rv >= 0 : "Returned negative key for key " + key;
  assert rv < nodes.length : "Invalid server number " + rv + " for key "
    + key;
  return rv;


Here, it says that using the right library for java which spymemcached here, the auto discovery is already enabled.

But as per the above code, it everytime goes to the same node which the DNS resolves the CNAME to, when the service starts.
I’m using this dependency.


Kindly help me in understanding this.

Thanks in advance.

Hello Priyanka_Naik
spymemcached 2.8.x is 8 years old, and hasn’t been a supported client from Couchbase for a long time.
I would highly suggest grabbing the latest 3.1.0 Java SDK and using this against a Couchbase Server 6.x or 7.x cluster which provides a much richer experience with high availability, disk persistance and faster throughput.

I cannot comment on how Amazon ElastiCache works, but with a Memcached bucket in Couchbase, your memcached clients have a list of all memcached server node addresses (IP address and port) and
use a consistent hashing algorithm (ketama) to determine which memcached node caches a key. Consistent hashing forms a keyspace called the continuum.

The output range of a hash function is treated as a fixed ring. Each node is assigned a random value within this space, which represents its position on the ring. Each data item identified by a key is assigned a node by hashing the data item’s key to yield a position on the ring, and then walking the ring in a
clockwise manner to find the first node with a position larger than the item’s position. You don’t use cnames in this configuration.

Also worth mentioning, in our newer clients we have a feature called alternate addresses which let you control multiple hostnames for each Couchbase Server node if needed.

Ian McCloy (Principal Product Manager, Couchbase)