Memcached client cluster Solution
By Mingjun Hou
- Cluster implementation
As a centralized cache, memcached has a central critical issue: single point of failure (spof). memcached supports the distribution of multiple instances on multiple machines, which only solves the problem of full data loss, however, when one of the machines fails, some data may be lost, and some eggs may be broken when a basket falls onto the ground. Therefore, we need to implement a backup mechanism to ensure that after some memcached fails, data can still be used, because this is also necessary for cluster.
(1) The application passes in the key to be operated, and obtains the client configured in the cluster through cachemanager.
(2) After obtaining the cache client, perform the cache operation.
(3) A. If it is a read operation, when it cannot hit, it will get data from other cache clients in the cluster. If it gets data, it will try to write it to the cache client obtained this time and return the result. (To achieve data recovery)
B. If it is an update operation, after the retrieved cache client executes the update operation, it returns immediately. the asynchronous update thread that submits commands from other machines in the update cluster to the client executes the column asynchronously. (Because the cache is obtained based on the key, asynchronous execution does not affect the query operation of this primary key)
Problem: If the timeout data is set, the copied data will become permanently valid after the data is lost.
2 localcache is used in combination with memcached to improve data acquisition efficiency ., Memcached does not have completely Zero loss. memcached communicates through socket data interaction. Therefore, the bandwidth, network I/O, and socket connections of the machine are all obstacles that restrict memcached from playing its role. One of the outstanding advantages of memcache is the timeout setting, that is, the data that is put in can be set to the validity period, which will automatically expire, in this way, some insensitive data can not be updated within a certain tolerable period, improving efficiency. According to this idea, each memcached client in the cluster can also use a local cache to cache the obtained data and set a certain expiration time to reduce the number of accesses to memcached, improve overall performance.
Therefore, each client has a built-in local cache with a timeout mechanism (using the lazy timeout mechanism). When obtaining data, first query whether the data exists locally, if it does not exist, initiate a request to memcache. After obtaining the data, cache it locally and set the validity period.