Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memcached best practices - small objects and lots of keys or big objects and few keys?

Tags:

I use memcached to store the integer result of a complex calculation. I've got hundreds of integer objects that I could cache! Should I cache them under a single key in a more complex object or should I use hundreds of different keys for the objects? (the objects I'm caching do not need to be invalidated more than once a day)

like image 711
jb. Avatar asked Jan 13 '09 02:01

jb.


People also ask

How many key-value pairs are stored on the memcached server?

For example, the total number of items (i.e. key-value pairs) stored in Memcached, can be useful information to move forward. The total number of items stored in Memcached is 10.

What are the some important features of Memcached?

Memcached can serve cached items in less than a millisecond, and enables you to easily and cost effectively scale for higher loads. Memcached is popular for database query results caching, session caching, web page caching, API caching, and caching of objects such as images, files, and metadata.

What happens when memcache is full?

First, when memcached gets full, it will start removing items from the cache using the Least Recently Used algorithm. Second, you can have multiple instances of memcached running, adding them gets complicated because it will change the hashing algorithm used to determine which cache the data is in.

Is Memcache a key-value store?

Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering. Memcached is simple yet powerful. Its simple design promotes quick deployment, ease of development, and solves many problems facing large data caches.


1 Answers

I would say lots of little keys. This way you can get the exact result you want in 1 call with minimal serialization effort.

If you store it in another object (an array for example) you will have to fetch the array from cache and then fetch the item you actually want again from that array, plus you have the overhead of serializing/deserializing the whole complex object again. Depending on your language of choice this might mean manually writing a serialization/deserialization function from scratch.

like image 160
Alex Avatar answered Oct 02 '22 00:10

Alex