Mysql高性能之Memcached(2)(四)

2015-01-23 22:07:57 · 作者: · 浏览: 21
stance is identified as being available.
For example, removing a memcached instance when the client notices that the instance can no longer
be contacted can cause the server selection to fail as described here.
To prevent this causing significant problems and invalidating your cache, you can select the hashing
algorithm used to select the server. There are two common types of hashing algorithm, consistent and
modula.
With consistent hashing algorithms, the same key when applied to a list of servers always uses the
same server to store or retrieve the keys, even if the list of configured servers changes. This means
that you can add and remove servers from the configure list and always use the same server for a
given key. There are two types of consistent hashing algorithms available, Ketama and Wheel. Both
types are supported by libmemcached, and implementations are available for PHP and Java.
Any consistent hashing algorithm has some limitations. When you add servers to an existing list of
configured servers, keys are distributed to the new servers as part of the normal distribution. When you
remove servers from the list, the keys are re-allocated to another server within the list, meaning that
the cache needs to be re-populated with the information. Also, a consistent hashing algorithm does not
resolve the issue where you want consistent selection of a server across multiple clients, but where
each client contains a different list of servers. The consistency is enforced only within a single client.
With a modula hashing algorithm, the client selects a server by first computing the hash and then
choosing a server from the list of configured servers. As the list of servers changes, so the server
selected when using a modula hashing algorithm also changes. The result is the behavior described
above; changes to the list of servers mean that different servers are selected when retrieving data,
leading to cache misses and increase in database load as the cache is re-seeded with information.
If you use only a single memcached instance for each client, or your list of memcached servers
configured for a client never changes, then the selection of a hashing algorithm is irrelevant, as it has
no noticeable effect.
If you change your servers regularly, or you use a common set of servers that are shared among a
large number of clients, then using a consistent hashing algorithm should help to ensure that your
cache data is not duplicated and the data is evenly distributed.

Memory Allocation within memcached:
When you first start memcached, the memory that you have configured is not automatically allocated.
Instead, memcached only starts allocating and reserving physical memory once you start saving
information into the cache.
When you start to store data into the cache, memcached does not allocate the memory for the data
on an item by item basis. Instead, a slab allocation is used to optimize memory usage and prevent
memory fragmentation when information expires from the cache.
With slab allocation, memory is reserved in blocks of 1MB. The slab is divided up into a number of
blocks of equal size. When you try to store a value into the cache, memcached checks the size of the
value that you are adding to the cache and determines which slab contains the right size allocation for
the item. If a slab with the item size already exists, the item is written to the block within the slab.
If the new item is bigger than the size of any existing blocks, then a new slab is created, divided up into
blocks of a sui