Skip to content

read_multi 50x slower when one of the keys is not found #106

@orslumen

Description

@orslumen

If all keys are present, the read_multi call is very fast. But surprisingly, when one of the requested keys is missing, it becomes more than 50x slower, see example below. The memcache-client gem does not have that problem.

I found that the time is spend in memcached itself, so I upgraded to the latest version (1.4.6) but that does not help.
I think the problem is in the way Dalli communicates with memcached.

The following example assumes Rails is loaded in the IRB

> mstore = ActiveSupport::Cache.lookup_store(:mem_cache_store, '127.0.0.1:11211', {:namespace => 'mtest'})
 => #<ActiveSupport::Cache::MemCacheStore:0xcab46fc @options={:namespace=>"mtest"}, dataMemCache: 1 servers, ns: nil, ro: false 
> dstore = ActiveSupport::Cache.lookup_store(:dalli_store, '127.0.0.1:11211', {:namespace => 'dtest'})
 => #<ActiveSupport::Cache::DalliStore:0xca9bd00 @options={:namespace=>"dtest"}, data#<Dalli::Client:0xca910f8 @options={:expires_in=>0}, ringnil, servers["127.0.0.1:11211"] 

> mstore.write('key1', 'value'); mstore.write('key2', 'value');
> dstore.write('key1', 'value'); dstore.write('key2', 'value');

> # MemCacheStore.read_multi with all known keys
> t=Time.now; 1000.times{ mstore.read_multi('key1', 'key2') }; Time.now - t
 => 0.662803 

> # DalliStore.read_multi with all known keys
> t=Time.now; 1000.times{ dstore.read_multi('key1', 'key2') }; Time.now - t
 => 0.320749 

> # MemCacheStore.read_multi with one missing key
> t=Time.now; 1000.times{ mstore.read_multi('key1', 'key3') }; Time.now - t
 => 0.605206 

> # Dalli.read_multi with one missing key
> t=Time.now; 1000.times{ dstore.read_multi('key1', 'key3') }; Time.now - t
 => 32.166594

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions