Memcached – Pages/Chunks and Rebalancing

It’s important to know how Memcached organizes the available memory and how Objects are saved to understand “strange” effects.

Memcached has Pages that have a size of 1Mb (in newer version you can configure them from 1Mb to 128Mb), these Pages are divided in Chunks.

The size of the Chunks is created dynamically after start, depending on the size of the Objects you are saving. Memcached tries to save a Object in a Chunk were the basted space is minimal (An 49Kb Object will be saved in an 50Kb Chunk)

These behavior is ok, but it’s a Problem when you change the size of your Objects, because Memcached doesn’t recalculate the Chunk Size, he will use the Chunk Size that is available and because of that the available Memory will not used optimally.

An Example:

  • We start Memcached and save 5-50Kb Objects, so the Chunks will take an Size of 5-50Kb and for bigger Objects there would be only one or two Pages available.
  • We change our Objects and we want to save 500Kb Objects. Memcached only has for this Object size one or two Pages available, and therefore this big Objects will be soon Evicted, because we have Space for maximum 2x500Kb for each Page. That means if we have 2 Pages with 500Kb Chunks, we can save 4 500Kb Objects, but if we save a fifth element it will cause that the first Object we saved is Evicted.

This behavior will cause that the small and big Objects will be saved correctly in Memcached, but when we try to get the entry from cache, it could happen we only have the small Objects in cache and the big Objects are not available.

Here an example with 300 seconds lifetime:

Item count: 2

Chunk Size (max item size): 448.2 KBytes

Chunks Per Page (items per 1MB): 2

Pages Allocated: 1

Total Chunks (capacity): 2

Used Chunks (capacity): 2

Free Chunks (free capacity): 0

Evicted: 1172130

Age: 1 minute

Items: item

1968214dc63043688cf883c794c118ecc29e6

19682_039f86925e86b5a3da4cff58c252dff4

After 1 minute the Objects are not available and we have two new Objects:

Item count: 2

Chunk Size (max item size): 448.2 KBytes

Chunks Per Page (items per 1MB): 2

Pages Allocated: 1

Total Chunks (capacity): 2

Used Chunks (capacity): 2

Free Chunks (free capacity): 0

Evicted: 1172133

Age: 0 minutes

Items: item

19682_63de2d8620816234fd044ca48c0578bd

19682_f86a0ea59a264a397233232a20a080f4

Unfortunately it seems that the actual version 1.4.5 doesn’t have rebalancing implemented, so if you change your cache Object size you must restart your Memcached instance (a flush will not help you).

Here two Screenshots were we can appreciate this behavior.

Only small Objects:

Memcached - Small Objects

After restart with small and big Objects:

Memcached - Small and Big Objects


Spidermonkey – Execute Javascript From Console
ChiliProject 1.4.0 + Ruby Enterprise + Passenger + Apache2
comments powered by Disqus