I take you to understand the principle of Memcache in-depth understanding

Source: Internet
Author: User
Tags epoll

Deep understanding of the memcache principle


1. Why Use Memcache


Because of the high concurrent read and write requirements of the website, the traditional relational database starts to bottleneck, for example:

1) High concurrent read and write to the database :

The relational database itself is a monster, and the process is time consuming (parsing SQL statements, transaction processing, etc.). If a relational database is highly concurrent read-write (tens of thousands of accesses per second), it is unsustainable.

2) Processing of massive amounts of data :

For large SNS websites, there are millions of Su generated every day (such as Twitter, Sina Weibo). For relational databases, if you look for a record in a data table with billions of data, the efficiency will be very low.


The use of memcache can be a good solution to the above problems.

In actual use, the result of database query is usually saved to Memcache, the next access is read directly from Memcache, and the database query operation is not done, which greatly reduces the burden of database.

The objects saved in Memcache are actually placed in memory, which is why memcache is so efficient.

650) this.width=650; "Src=" http://img.blog.csdn.net/20140113235214078?watermark/2/text/ ahr0cdovl2jsb2cuy3nkbi5uzxqvd3vzdw9wdujvufq=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/ Southeast "style=" Border:none;height:auto; "/>

Installation and use of 2.memcache

This online has too many tutorials, do not go into the elaboration.


3. Libevent-based event handling


Libevent is a library that encapsulates event-handling functions such as Linux's Epoll, BSD-class operating system kqueue, and so on as a unified interface. The Performance of O (1) can be played even if the number of connections to the server increases.

Memcached uses this libevent library, so it can perform its high performance on Linux, BSD, Solaris and other operating systems.

Reference:

    • libevent:http://www.monkey.org/~provos/libevent/

    • The c10k problem:http://www.kegel.com/c10k.html


4.memcache Usage Examples:


<?php
$MC = new Memcache ();
$MC->connect (' 127.0.0.1 ', 11211);

$uid = (int) $_get[' uid '];
$sql = "SELECT * from Users where uid= ' uid '";
$key = MD5 ($sql);
if (! ( $data = $MC->get ($key))) {
$conn = mysql_connect (' localhost ', ' test ', ' test ');
mysql_select_db (' Test ');
$result = Mysql_fetch_object ($result);
while ($row = Mysql_fetch_object ($result)) {
$data [] = $row;
}
$MC->add ($key, $datas);
}

Var_dump ($datas);
?>


5.memcache How to support high concurrency

Memcache uses a multiplexed I/O model, such as (Epoll, select, etc.), in traditional I/O, the system may be waiting because a user connection is not ready for I/O, knowing that the connection is ready for I/O. At this point, if there are other users connected to the server, it is likely that the system is blocked and will not be responding.

Multiplexing I/O is a message-notification pattern, and when the user connects to the I/O preparation, the system notifies us that the connection can be I/O so that it does not block the connection to a user. Therefore, memcache can support high concurrency.

In addition, Memcache uses a multithreaded mechanism. Multiple requests can be processed at the same time. The number of threads is generally set to the number of CPU cores, which is the most efficient.


6. Saving data using the Slab allocation algorithm

The principle of the slab allocation algorithm is to divide the fixed size (1MB) memory into n small chunks, as shown in:

650) this.width=650; "Src=" http://img.blog.csdn.net/20140114000840296?watermark/2/text/ ahr0cdovl2jsb2cuy3nkbi5uzxqvd3vzdw9wdujvufq=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/ Southeast "style=" Border:none;height:auto; "/>


The slab allocation algorithm calls every 1MB size of memory a slab page, each time requesting a slab page from the system, and then dividing the slab page into several small pieces of chunk (as shown) by separating the algorithm, and then assigning the chunk to the user. The segmentation algorithm is as follows (in the SLABS.C file):


    memset (slabclass, 0, sizeof (slabclass));    //  Initialize the trunk size of each slabclass_t and the number of trunks in each slab     // slabclass each Slabclass_ Trunk size of t grows to factor times     //  Note  i  from index  1  start      while  (++i < power_largest && size <= settings.item_size_max  / factor)  {        /* make sure items  are always n-byte aligned */        if  (size % chunk_align_bytes)                               //  Memory 8-byte alignment             size += chunk_align_ bytes -  (Size % chuNk_align_bytes);        slabclass[i].size = size;         slabclass[i].perslab = settings.item_size_max /  slabclass[i].size;        size *= factor;         if  (settings.verbose > 1)  {             fprintf (stderr,  "slab class %3d: chunk size  %9u perslab %7u\n ",                     i, slabclass[i].size, slabclass[i].perslab);         }    }    //  The trunk size of the last slabclass_t in Slabclass is set to the maximum item size     power_largest = i;     slabclass[power_largest].size = settings.item_size_max;    slabclass[power_largest].perslab =  1;    if  (settings.verbose > 1)  {         fprintf (stderr,  "slab class %3d: chunk size %9u  perslab %7u\n ",                 i, slabclass[i].size, slabclass[i].perslab);    }     ....//  omitted}


The Slabclass in the above code is an array of type slabclass_t structures, defined as follows:



typedef struct {    unsigned int size;       /* sizes of items */    unsigned int perslab;    /* how many items per slab */    void ** slots;           /* list of item  ptrs */    unsigned int sl_total;  /* size of  previous array */    unsigned int sl_curr;   /*  first free slot */    void *end_page_ptr;          /* pointer to next free item at end of  page, or 0 */    unsigned int end_page_free; /*  Number of items remaining at end of last alloced page */    unsigned  int slabs;     /* how many slabs were allocated  for this class */    void **slab_list;        /* array of slab pointers */    unsigned int  list_size; /* size of prev array */    unsigned  Int killing;  /* index+1 of dying slab, or zero if none  */    size_t requested; /* the number of requested  bytes */} slabclass_t;




The source code of the segmentation algorithm shows that the slab algorithm divides slab pages according to different sizes of chunk, while chunk with different sizes multiplies by factor (default is 1.25).

Use the MEMCACHE-VV command to view memory allocations (8-byte alignment):

650) this.width=650; "src=" Http://hi.csdn.net/attachment/201111/30/0_13226438938sQ8.gif "style=" border:none; Height:auto; "/>


This article from "Li Shilong" blog, declined reprint!

I take you to understand the principle of Memcache in-depth understanding

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.