1. How memcached supports high concurrency
The memcached uses the multiplexed I/O model. In traditional blocking I/O, the system may be waiting for a user connection to be ready for the I/O readiness, knowing that the connection is ready. If this is the case with other users connecting to the server, it is likely that the system is blocked from being unresponsive.
Multiplexing I/O is a message-notification pattern, and the system notifies us that the connection is ready for I/O when the user connects to the I/O preparation. This will not block the connection to a user. Therefore, memcached supports high concurrency.
In addition, Memcached uses the multithreaded mode, which allows you to specify the number of threads to turn on by using the '-T ' parameter when the memcached server is turned on. But not the more threads the better. It is generally set to the CPU core number, which is the most efficient. Because the more threads, the more threads the system needs to schedule. The number of CPU threads is set to the number of CPU cores, and the system is scheduled for the least time.
2. Delete Expired Item
memcached sets an expiration time for each item, but does not expire the item from memory, but instead accesses the item when it is valid to delete the item, implementing the code as follows:
<span style= "Font-family:microsoft yahei;font-size:18px;" ><span style= "Font-family:microsoft yahei;font-size:14px;" ><? Delete the expired Item code item *do_item_get_notedeleted (const char*key,const size_t nkey,bool *delete_locked) {Item * it = Assoc_find (key, Nkey); if (delete_locked) *delete_locked =false; if (it = NULL && (it->it_flags & item_deleted)) {if (!item_delete_lock_over (it)) { if (delete_locked) *delete_locked = true; it = null; }} if (it! = null &&settings.oldest_live! = 0 && settings.oldest_live <= curren t_time&& it, time <= settings.oldest_live) {do_item_unlink (IT); it = null; } if (it! = NULL && it->exptime! = 0 && It-exptime <= current_time) { Do_item_unlink (it); it = null; } if (!it = null) {It-refcount + +; DEBUG_REFCNT (it, ' + '); } return it;} </span></span>
Use the do_item_get_notedeleted function to find the specified item in memcached, as shown in the above code, when the item expires earlier than the current time, the item is deleted.
Delaying the deletion of outdated iten to lookups can improve the efficiency of memcached. This improves CPU productivity by not having to check for expired item every time.
3. Using the LRU algorithm to retire data
When you use the Slabs_alloc function to request memory failure, you begin to retire the data. The elimination rule is to start the loop from the end of the data item list, and find an item with a reference counter of 0 in the list to release the item.
Why do you want to iterate through the tail of the item list? Because memcached will put the item that has just been accessed to the item list header, the tail item is not or rarely accessed, which is the essence of the LRU algorithm.
If the item in the item list cannot be found with a counter of 0, look for an item that has not been accessed for 3 hours. Release him, if still not found, return Null (Request memory failed).
As you can see from the above analysis, when memory is low, memcached will retire the item with less access or no access for a period of time in order to free up memory space to store the new item.
For more information, refer to:
Http://www.cnblogs.com/xianbei/archive/2011/01/18/1924893.html
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Deep understanding of memcached, high concurrency, laziness and LRU (i)