Search code examples
rubymemcachedtelnetdalli

Can't read key from memcached using dalli


dalli refuses to connect and read from a remote memcached server, but telnet works just fine.

I have connected to a remote server via SSH, and forwarded a memcached port over to my machine like this:

Host access.production
  HostName 1.2.3.4
  LocalForward 10001 9.8.7.6:11211

Now that I try to read keys from memcached using telnet, it works just fine:

$ telnet localhost 10001
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.

However, the following Ruby script using dalli will fail:

require 'dalli'

client = Dalli::Client.new('localhost:10001')
client.get('my_key')

Why dalli wouldn't connect to memcached, while telnet – will?

Updated. dalli returns this error:

I, [2015-07-17T11:22:36.839807 #4286]  INFO -- : localhost:10001 failed (count: 0) Timeout::Error: IO timeout: {:host=>"localhost", :port=>10001, :down_retry_delay=>1, :socket_timeout=>0.5, :socket_max_failures=>2, :socket_failure_delay=>0.01, :value_max_bytes=>1048576, :compressor=>Dalli::Compressor, :compression_min_size=>1024, :compression_max_size=>false, :serializer=>Marshal, :username=>nil, :password=>nil, :keepalive=>true}
/Users/gmile/.rvm/gems/ruby-2.1.4@portal/gems/dalli-2.7.2/lib/dalli/ring.rb:45:in `server_for_key': No server available (Dalli::RingError)
    from /Users/gmile/.rvm/gems/ruby-2.1.4@portal/gems/dalli-2.7.2/lib/dalli/client.rb:328:in `perform'
    from /Users/gmile/.rvm/gems/ruby-2.1.4@portal/gems/dalli-2.7.2/lib/dalli/client.rb:53:in `get'
    from dali_all_keys.rb:4:in `<main>'

Solution

  • The default Dalli timeout is 500ms. Given that memcache response times are typically single digit milliseconds, and that you put your memcache instances close to your servers this is usually ample.

    On the other hand given that you are connecting via an ssh tunnel you might see much greater latencies you might want to increase the timeout:

     Dalli::Client.new("127.0.0.1:10001", socket_timeout: 2.0)