title: Unable to connect to AWS ElastiCache clusters using Membase client lib for memcached
body: <p>I am having problems with get/set to an ElastiCache cluster from my EC2 instance. I am getting - <code>SEVERE: net.spy.memcached.OperationTimeoutException: Timeout waiting for value</code> - error.</p>
  
  <p>When I am trying to get or set a value. I used the same code on my local machine (albeit communicating with a local memcached server) and everything works fine. The complete stacktrace can be found here - <a href="http://pastebin.com/tYcCJ6cj" rel="nofollow">http://pastebin.com/tYcCJ6cj</a> </p>
  
  <p>I first saw that can I at least get the IP address of all the nodes of a cluster so that I can feed it in to my membase client &amp; I am indeed able to find out the node ip addresses. 
  I also made sure that all my EC2 security groups are added to the default cache cluster security group as well.</p>
  
  <p>Any pointers on this will be greatly helpful.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Code snippet used to fetch a particular record.</p>
  
  <pre class="lang-java prettyprint-override"><code>public String getCachedValue(String namespace, String key) {
      String value = null;
  
      try {
          MemcachedClient client
              = CacheConnectionUtil.connectToElastiCacheMemcachedServer();
  
          // Point of origin for the exception.
          return (String) client.get(namespace + "$" + hashKey(key));        
      } catch (IOException e) {
          e.printStackTrace();
      }
  
      return value;
  }
  </code></pre>
  
  <p>Code snippet used to connect to the ElastiCache server</p>
  
  <pre class="lang-java prettyprint-override"><code>private static MemcachedClient connectToElastiCacheMemcachedServer() 
      throws IOException {
  
      DescribeCacheClustersResult cacheClustersInfo = null;
      DescribeCacheClustersRequest cacheClusterRequest
           = new DescribeCacheClustersRequest();
      cacheClusterRequest.setShowCacheNodeInfo(true);
  
      try {
      cacheClustersInfo = AWSConnectionUtil
          .getElastiCacheObject(null)
          .describeCacheClusters(cacheClusterRequest);
      } catch (Exception e) {
          e.printStackTrace();
          throw new IOException("Unable to connect to ElastiCache Cluster.", e);
      }
  
      if (cacheClustersInfo == null) {
          throw new IOException("ElastiCache Cluster Info Object is null.");
      }
  
      List&lt;CacheCluster&gt; clusters = cacheClustersInfo.getCacheClusters();
  
      if (clusters == null || clusters.isEmpty()) {
          throw new IOException("No ElastiCache Clusters available.");
      }
  
      List&lt;String&gt; serverList = new ArrayList&lt;String&gt;();
      for (CacheCluster cluster : clusters) {
          if (cluster != null 
              &amp;&amp; AWSConstants
                     .CACHE_CLUSTER_ID
                     .equalsIgnoreCase(cluster.getCacheClusterId())) {
  
              List&lt;CacheNode&gt; nodes = cluster.getCacheNodes();
              if (nodes != null ) {
                  for (CacheNode node : nodes) {
                      if (node != null) {
                          Endpoint endpoint = node.getEndpoint();
                          if (endpoint != null
                              &amp;&amp; endpoint.getAddress() != null) {
                              serverList.add(endpoint.getAddress()
                                             + ":"
                                             + endpoint.getPort());
                          }
                      }
                  }
              }
          }
      }
  
      if (serverList.isEmpty()) {
          throw new IOException("No Cached nodes available for cluster - "
                                + AWSConstants.CACHE_CLUSTER_ID); 
      }
  
      return new MemcachedClient(AddrUtil.getAddresses(serverList));
  }
  </code></pre>