Introduction to distributed cache-memcached

Source: Internet
Author: User

[1] memcached uses the memory buffer based on key-value pairs. Instead of using the disk buffer to act as the buffer, memcached uses the actual physical memory.

[2] memcached needs to specify the allocated memory size at startup. Command example: memcached-D-M memory size (in MB)-l IP address-P Port

[3] memcached is a single-index structured data organization form. All data items are independent from each other (do not want the traditional data to be relational). Each data item uses the key as the unique index, do not treat cache with relational thinking

[4] memcached uses the key-based hash algorithm to store data. The query time complexity reaches O (1)

[5] memcached uses the LRU algorithm to remove buffered data items, but allows users to set the cache expiration time, which depends on the test.

[6] memcached uses the libevent function library to implement the network concurrency model. It supports cache operations in a large number of concurrent users.

[7] memcached uses Object serialization technology to serialize objects into binary data and transmit them over the network.

[8] memcached can be combined with JSON format to express the objects in the memory in JSON format, and then serialize the objects into strings and cache them.

[9] The client API of memcached can store objects directly in the cache, or retrieve objects directly from the cache, hiding the conversion details.

[10] a core issue of using memcached is: what content can be cached? Nothing is necessary to happen in real time

[11] Do not use locks on memcached to prevent thread competition. Instead, use the atomic increment operation provided by memcached.

[12] Among memcached status parameters, total_items, bytes, get_hits, bytes_read, bytes_written, and limit_maxbytes are common reference data.

[13] memcached status focuses on space usage, cache hit rate, and Io traffic.

[14] memcached caches data partitions. It is best not to use a partition algorithm unrelated to the business based on the type of business data content. Otherwise, load imbalance may occur.

[15] After memcached extension, you need to use a "cache connector" on the front end to allocate cache data based on the hash algorithm.

[16] The data of memcached comes theoretically from the underlying persistent resources. Therefore, when the cache is extended, it can be cleared. The premise is that the necessary data has been persisted.

[17] cache is not a persistence facility. Do not treat cached data with persistence. It already has a corresponding persistent resource.

[18] data in the cache should preferably be temporary data (persistence is not necessary, but only used for process control) or existing persistent resources (reconstruction can be performed if necessary)

[19] memcached adopts the "Block allocation" memory allocation mode, and the key size is limited to 250 bytes, and the value size is limited to 1 MB.

[20] memcached adopts the lazy replacement method. It does not enable additional processes to monitor and delete outdated kV pairs in real time. Instead, it only needs to insert new data, at this time, there is no extra space to clear.

[21] Once the database data detects changes, we must update the data in the cache in time to ensure that the app reads the correct synchronized data. Of course, we can record the expiration time of the data in the cache through the timer. Once the time passes, the event will be triggered to update the cache, but there will always be a delay in time, as a result, the app may read dirty data from the cache, which is also known as a dog hole problem.

[22] when data is lost in one ms, the app can still retrieve data from the database. However, it is more cautious to provide additional ms to support cache when some MS cannot work normally, in this way, the database will not be overloaded because the app cannot retrieve data from the cache.

[23] With cache support, we can add a cache layer between the traditional app layer and the DB layer. Each app server can bind a MC, each data read can be obtained from Ms. If no data is read from the DB layer. When data is updated, in addition to sending the update SQL to the DB layer, it also needs to send the updated data to MC so that MC can update the data in ms.

[24] in order to minimize the load pressure on the database, we can deploy database rewrites and use the slave database to complete read operations. The master database is always responsible for only three tasks: 1. update data; 2. synchronize the slave database; 3. update cache.

[25] Because keys are hashed on different servers, It is very troublesome to clean up a type of keys in large batches. Because memcached itself is a large hash table, it does not have the key retrieval function. Therefore, memcached does not know how many keys are stored in a certain type of key and which servers are stored. Such functions are often used in practical applications.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.