Distributed cache: memcached (write Cache)

Source: Internet
Author: User
Tags abstract data structures memcached serialization thread
front-end buffers for databases

The file system kernel buffer, which is located in the kernel address space of the physical memory, passes through it all the read and write operations of the disk file, or it can be seen as the front-end device of the disk.
This kernel buffer actually consists of 2 parts: Read buffers, write buffers.

The read buffer holds the data that was recently read from the disk by the system, and once the data is read the next time, the kernel will be available directly from here without having to access the disk.
The purpose of writing buffers is mainly to reduce the physical write operation of the disk, the kernel buffer can accumulate multiple write instructions, through the movement of a physical head to complete. Of course, the write buffers cause the data to actually write to the disk with a delay of a few seconds, which is called the Dirtry Page before actually writing.

We can create a buffer between the database and the dynamic content, which can be deployed on separate servers to speed up the operation of database reads and writes, a buffer that is actually controlled by dynamic content. memcached Key-value

In order to achieve cache, we do not put the cache content on disk, memcached use physical memory as a buffer, when we start memcached, we need to specify the memory size allocated to the buffer.
For example, we allocate 4GB of memory:

Memcached-d-M 4086-l 192.168.88.88-p 11211

If there is anything that memcached needs, there is no doubt that it is memory.

Memcached uses Key-value to store data, and we combine each key and the corresponding value together as a data item.
Memecached uses an efficient key-based hash algorithm to design storage data structures and uses a well-designed memory allocator, which means that no matter how many data items you store, the time it takes to query any data item is the same. data item Expiration Time

Since the buffer space is limited, once the buffers do not have enough controls to store new data items, memcached will try to eliminate some of the data items to make room for the recently infrequently accessed data items out.

Of course, we are more responsible for setting the expiration time for data items. If you are using PHP to write dynamic content:

$mem = Memcache_connect ("192.168.88.88", 11211);
$mem->add ("Item_key", "Item_value", false,30);

Set the expiration time of this data item to 30 seconds for Item_key. Network concurrency Model

Memcached is a distributed cache system that can be run on a standalone server, with dynamic content accessing it through a TCP socket. Memcached uses the Libevent library to implement the network concurrency model, and you can still use memcached in the context of a large number of concurrent users. serialization of Objects

We are transmitting binary data in the network, so whether an abstract data type such as an array or an object can be stored in memcached.
The answer is yes. Because of the serialization (Serialize) mechanism, we can convert the abstract data type to a binary string to enter the cache server over the network, and while reading the data, the binary string can be converted back to the original data type.
This "conversion" process is more complex, but in a scripting language with dynamic characteristics (such as PHP), this process does not need to be implemented.

Here is a simple example of writing an object to the memcached server:

<?php
class  person
{public
    $name;
    Public Function SetName ($name) {
        $this->name = $name;
    }
}

Instantiate
$p = new Person ();
$p->setname ("Jack");//Assign value to object property

//Connect memcached
$mem = Memcache_connect ("192.168.88.88", 11211);
$mem->add ("P_jack", $p, false,0); Write
$obj = $mem->get ("P_jack");//Read

echo $obj->name;//output: Jack.
Write operations Cache

Read cache Everyone must be familiar with it, and the writable cache is also critical.

Suppose there is a requirement: Take our site traffic statistics function, we need to record the cumulative number of visits per URL, so each page refresh will accompany the increase in the number of visits.

$page = "artical20180120.html";
$sql = "Update Page_view set view_count=view_count+1 where page= ' {$page} '";
$conn = Mysqli_connect ("localhost", "root", "root", "db_page");
mysqli_select_db ("Db_main", $conn);
Mysqli_query ($sql, $conn);

There is no caching method just like the above code, which we call "direct update". thread safety and lock contention

Let's take a look at a distributed add operation, the following code fragment, first retrieve a value from the cache server, but in local +1, then write back to the cache server.

$count = $mem->get ($key);
$count + +;
$mem->set ($key, $count, false,0);

Well, it doesn't seem to be a problem. However, you can not forget that there may be multiple users at the same time to trigger such a calculation, you can certainly imagine what the bad consequences, the final cumulative traffic is always less than the actual traffic.

In fact, this does not involve memcacahed itself as a thread-safety issue, but the above addition is not thread-safe. If this addition is to be ensured at the same time, a certain transaction isolation mechanism should be considered.
The simplest way is to use lock competition, and keep the lock in memcached, the dynamic content of the competitive relationship can compete for this lock, a single session to grab the lock, then the other sessions must wait.
But we do not encourage this, as the waiting time brought by lock competition is intolerable. Atomic addition

Memcached provides atomic increment operations, and it is precisely because of this feature that we introduce write caching in applications where traffic is incrementally updated.
Let's Change the code:

$page = "artical20180120.html";

$mem = Memcache_connect ("192.168.88.88", 11211);
$count = $mem->increment ($page, 1); Accumulate 1
if ($count = = = False) {
    $mem->add ($page, 1);
    Exit (1);
}
if ($count = =) {
    $mem->set ($page, 0,false,0);
    $sql = "Update Page_view set view_count=view_count+{$count} where page= ' {$page} '";
    $conn = Mysqli_connect ("localhost", "root", "root", "db_page");
    mysqli_select_db ("Db_main", $conn);
    Mysqli_query ($sql, $conn);
}

The above code completely changed the "direct Update" method, which did the following:
1, for the corresponding data item in the memcached cache plus 1, if the data item does not exist, the data item is created and assigned a value of 1, which represents the first time the page is accessed;
2, if there is a corresponding data item in the memcached cache, and the accumulated value is 1000, then set the data item to 0, and update the database, the database corresponding value plus 1000.

That is fire, after the transformation of the program every 1000 times after the database is written once. This greatly reduces the direct operation of the database. If your database is busy because of a lot of write operations, you should carefully consider which writes can be cached in memcached.

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.