Holding Collections in memcache
sgrimm at facebook.com
Thu Jul 5 15:44:02 UTC 2007
On 7/5/07 5:12 AM, "Paul Stacey" <paul.stacey at fatsoma.com> wrote:
> Events contains an array of event
A better way to deal with this kind of thing is with a two-phase fetch. So
instead of directly caching an array of event data, instead cache an array
of event IDs. Query that list, then use it construct a list of the keys of
individual event objects you want to fetch, then multi-get that list of
In addition to greatly increasing the maximum size of the list (memcached
has a 1-megabyte hard limit on item size, and obviously you can fit a lot
more IDs than full data in that limit) you will also, if you're using
multiple memcached servers, spread the load around by virtue of your
multi-get hitting basically every server once your key list is a certain
size. If you stick all the data in one frequently-requested item, then you
will pound the particular server that happened to get that item while all
the others sit idle.
Another advantage of a scheme like this is that you can update an item's
data without having to read then write every list that contains that item.
Just update it by ID (like you'd do in your database queries) and all the
lists that contain it will magically get the correct information.
All the above said, it is true that dealing with lists of things is not
currently memcached's strong suit. For example, there is no "append"
operation. It is much better at caching individual objects.
More information about the memcached