1. 程式人生 > >深入理解Memcached原理

深入理解Memcached原理

1.為什麼要使用memcache

 由於網站的高併發讀寫需求,傳統的關係型資料庫開始出現瓶頸,例如:

1)對資料庫的高併發讀寫:

關係型資料庫本身就是個龐然大物,處理過程非常耗時(如解析SQL語句,事務處理等)。如果對關係型資料庫進行高併發讀寫(每秒上萬次的訪問),那麼它是無法承受的。

2)對海量資料的處理:

對於大型的SNS網站,每天有上千萬次的資料產生(如twitter, 新浪微博)。對於關係型資料庫,如果在一個有上億條資料的資料表種查詢某條記錄,效率將非常低。

使用memcache能很好的解決以上問題。

在實際使用中,通常把資料庫查詢的結果儲存到Memcache中,下次訪問時直接從memcache中讀取,而不再進行資料庫查詢操作,這樣就在很大程度上減少了資料庫的負擔。

儲存在memcache中的物件實際放置在記憶體中,這也是memcache如此高效的原因。


2.memcache的安裝和使用

這個網上有太多教程了,不做贅言。

3.基於libevent的事件處理


libevent是個程式庫,它將Linux的epoll、BSD類作業系統的kqueue等事件處理功能 封裝成統一的介面。即使對伺服器的連線數增加,也能發揮O(1)的效能。

 memcached使用這個libevent庫,因此能在Linux、BSD、Solaris等作業系統上發揮其高效能。 

參考:

4.memcache使用例項:

[php] view plaincopyprint?在CODE上檢視程式碼片派生到我的程式碼片
  1. <?php  
  2. $mc = new Memcache();  
  3. $mc->connect('127.0.0.1', 11211);  
  4. $uid = (int)$_GET['uid'];  
  5. $sql = "select * from users where uid='uid' ";  
  6. $key = md5($sql);  
  7. if(!($data = $mc->get($key))) {  
  8.     $conn = mysql_connect('localhost''test''test');  
  9.     mysql_select_db('test');  
  10.     $result = mysql_fetch_object(
    $result);  
  11.     while($row = mysql_fetch_object($result)) {  
  12.           $data[] = $row;  
  13.     }  
  14.     $mc->add($key$datas);  
  15. }  
  16. var_dump($datas);  
  17. ?>  
<?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如何支援高併發(此處還需深入研究)

memcache使用多路複用I/O模型,如(epoll, select等),傳統I/O中,系統可能會因為某個使用者連線還沒做好I/O準備而一直等待,知道這個連線做好I/O準備。這時如果有其他使用者連線到伺服器,很可能會因為系統阻塞而得不到響應。

而多路複用I/O是一種訊息通知模式,使用者連線做好I/O準備後,系統會通知我們這個連線可以進行I/O操作,這樣就不會阻塞在某個使用者連線。因此,memcache才能支援高併發。

此外,memcache使用了多執行緒機制。可以同時處理多個請求。執行緒數一般設定為CPU核數,這研報告效率最高。

6.使用Slab分配演算法儲存資料

slab分配演算法的原理是:把固定大小(1MB)的記憶體分為n小塊,如下圖所示:


slab分配演算法把每1MB大小的記憶體稱為一個slab頁,每次向系統申請一個slab頁,然後再通過分隔演算法把這個slab頁分割成若干個小塊的chunk(如上圖所示),然後把這些chunk分配給使用者使用,分割演算法如下(在slabs.c檔案中):

[cpp] view plaincopyprint?在CODE上檢視程式碼片派生到我的程式碼片
  1. /** 
  2.  * Determines the chunk sizes and initializes the slab class descriptors 
  3.  * accordingly. 
  4.  */
  5. void slabs_init(constsize_t limit, constdouble factor, constbool prealloc) {  
  6.     int i = POWER_SMALLEST - 1;  
  7.     unsigned int size = sizeof(item) + settings.chunk_size;  
  8.     mem_limit = limit;  
  9.     if (prealloc) {  
  10.         /* Allocate everything in a big chunk with malloc 通過malloc的方式申請記憶體*/
  11.         mem_base = malloc(mem_limit);  
  12.         if (mem_base != NULL) {  
  13.             mem_current = mem_base;  
  14.             mem_avail = mem_limit;  
  15.         } else {  
  16.             fprintf(stderr, "Warning: Failed to allocate requested memory in"
  17.                     " one large chunk.\nWill allocate in smaller chunks\n");  
  18.         }  
  19.     }  
  20.     memset(slabclass, 0, sizeof(slabclass));  
  21.     while (++i < POWER_LARGEST && size <= settings.item_size_max / factor) {  
  22.         /* Make sure items are always n-byte aligned  注意這裡的位元組對齊*/
  23.         if (size % CHUNK_ALIGN_BYTES)  
  24.             size += CHUNK_ALIGN_BYTES - (size % CHUNK_ALIGN_BYTES);  
  25.         slabclass[i].size = size;  
  26.         slabclass[i].perslab = settings.item_size_max / slabclass[i].size;  
  27.         size *= factor;//以1.25為倍數增大chunk
  28.         if (settings.verbose > 1) {  
  29.             fprintf(stderr, "slab class %3d: chunk size %9u perslab %7u\n",  
  30.                     i, slabclass[i].size, slabclass[i].perslab);  
  31.         }  
  32.     }  
  33.     power_largest = i;  
  34.     slabclass[power_largest].size = settings.item_size_max;  
  35.     slabclass[power_largest].perslab = 1;  
  36.     if (settings.verbose > 1) {  
  37.         fprintf(stderr, "slab class %3d: chunk size %9u perslab %7u\n",  
  38.                 i, slabclass[i].size, slabclass[i].perslab);  
  39.     }  
  40.     /* for the test suite:  faking of how much we've already malloc'd */
  41.     {  
  42.         char *t_initial_malloc = getenv("T_MEMD_INITIAL_MALLOC");  
  43.         if (t_initial_malloc) {  
  44.             mem_malloced = (size_t)atol(t_initial_malloc);  
  45.         }  
  46.     }  
  47.     if (prealloc) {  
  48.         slabs_preallocate(power_largest);  
  49.     }  
  50. }  
/**
 * Determines the chunk sizes and initializes the slab class descriptors
 * accordingly.
 */
void slabs_init(const size_t limit, const double factor, const bool prealloc) {
    int i = POWER_SMALLEST - 1;
    unsigned int size = sizeof(item) + settings.chunk_size;

    mem_limit = limit;

    if (prealloc) {
        /* Allocate everything in a big chunk with malloc 通過malloc的方式申請記憶體*/
        mem_base = malloc(mem_limit);
        if (mem_base != NULL) {
            mem_current = mem_base;
            mem_avail = mem_limit;
        } else {
            fprintf(stderr, "Warning: Failed to allocate requested memory in"
                    " one large chunk.\nWill allocate in smaller chunks\n");
        }
    }

    memset(slabclass, 0, sizeof(slabclass));

    while (++i < POWER_LARGEST && size <= settings.item_size_max / factor) {
        /* Make sure items are always n-byte aligned  注意這裡的位元組對齊*/
        if (size % CHUNK_ALIGN_BYTES)
            size += CHUNK_ALIGN_BYTES - (size % CHUNK_ALIGN_BYTES);

        slabclass[i].size = size;
        slabclass[i].perslab = settings.item_size_max / slabclass[i].size;
        size *= factor;//以1.25為倍數增大chunk
        if (settings.verbose > 1) {
            fprintf(stderr, "slab class %3d: chunk size %9u perslab %7u\n",
                    i, slabclass[i].size, slabclass[i].perslab);
        }
    }

    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);
    }

    /* for the test suite:  faking of how much we've already malloc'd */
    {
        char *t_initial_malloc = getenv("T_MEMD_INITIAL_MALLOC");
        if (t_initial_malloc) {
            mem_malloced = (size_t)atol(t_initial_malloc);
        }

    }

    if (prealloc) {
        slabs_preallocate(power_largest);
    }
}

上面程式碼中的slabclass是一個型別為slabclass_t結構的陣列,其定義如下:

[cpp] view plaincopyprint?在CODE上檢視程式碼片派生到我的程式碼片
  1. typedefstruct {  
  2.     unsigned int size;      /* sizes of items */
  3.     unsigned int perslab;   /* how many items per slab */
  4.     void **slots;           /* list of item ptrs */
  5.     unsigned int sl_total;  /* size of previous array */
  6.     unsigned int sl_curr;   /* first free slot */
  7.     void *end_page_ptr;         /* pointer to next free item at end of page, or 0 */
  8.     unsigned int end_page_free; /* number of items remaining at end of last alloced page */
  9.     unsigned int slabs;     /* how many slabs were allocated for this class */
  10.     void **slab_list;       /* array of slab pointers */
  11.     unsigned int list_size; /* size of prev array */
  12.     unsigned int killing;  /* index+1 of dying slab, or zero if none */
  13.     size_t requested; /* The number of requested bytes */
  14. } slabclass_t;  
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;

借用別人的一張圖說明slabclass_t結構:


由分割演算法的原始碼可知,slab演算法按照不同大小的chunk分割slab頁,而不同大小的chunk以factor(預設是1.25)倍增大。

使用memcache -u root -vv 命令檢視記憶體分配情況(8位元組對齊):


找到大小最合適的chunk分配給請求快取的資料:

[cpp] view plaincopyprint?在CODE上檢視程式碼片派生到我的程式碼片
  1. /* 
  2.  * Figures out which slab class (chunk size) is required to store an item of 
  3.  * a given size. 
  4.  * 
  5.  * Given object size, return id to use when allocating/freeing memory for object 
  6.  * 0 means error: can't store such a large object 
  7.  */
  8. unsigned int slabs_clsid(constsize_t size) {  
  9.     int res = POWER_SMALLEST;// 初始化為最小的chunk
  10.     if (size == 0)  
  11.         return 0;  
  12.     while (size > slabclass[res].size) //逐漸增大chunk size,直到找到第一個比申請的size大的chunk
  13.         if (res++ == power_largest)     /* won't fit in the biggest slab */
  14.             return 0;  
  15.     return res;  
  16. }  
/*
 * Figures out which slab class (chunk size) is required to store an item of
 * a given size.
 *
 * Given object size, return id to use when allocating/freeing memory for object
 * 0 means error: can't store such a large object
 */

unsigned int slabs_clsid(const size_t size) {
    int res = POWER_SMALLEST;// 初始化為最小的chunk

    if (size == 0)
        return 0;
    while (size > slabclass[res].size) //逐漸增大chunk size,直到找到第一個比申請的size大的chunk
        if (res++ == power_largest)     /* won't fit in the biggest slab */
            return 0;
    return res;
}


記憶體分配:

[cpp] view plaincopyprint?在CODE上檢視程式碼片派生到我的程式碼片
  1. staticvoid *do_slabs_alloc(constsize_t size, unsigned int id) {  
  2.     slabclass_t *p;  
  3.     void *ret = NULL;  
  4.     item *it = NULL;  
  5.     if (id < POWER_SMALLEST || id > power_largest) {//判斷id是否會導致slabclass[]陣列越界
  6.         MEMCACHED_SLABS_ALLOCATE_FAILED(size, 0);  
  7.         return NULL;  
  8.     }  
  9.     p = &slabclass[id];//獲取slabclass[id]的引用
  10.     assert(p->sl_curr == 0 || ((item *)p->slots)->slabs_clsid == 0);//判斷slabclass[id]是否有剩餘的chunk
  11.     if (! (p->sl_curr != 0 || do_slabs_newslab(id) != 0)) {//如果slabclass[id]中已經沒有空餘chunk並且試圖向系統申請一個“頁”(slab)的chunk失敗,則返回NULL
  12.     /* We don't have more memory available */
  13.         ret = NULL;  
  14.     } elseif (p->sl_curr != 0) {//slabclass[id]的空閒連結串列中還有chunk,則直接將其分配出去
  15.         it = (item *)p->slots;//獲取空閒連結串列的頭指標
  16.         p->slots = it->next;//將頭結點指向下一個結點(取下頭結點)
  17.         if (it->next) it->next->prev = 0;//將新頭結點的prev指標置空
  18.         p->sl_curr--;//減少slabclass[id]空閒連結串列中的chunk計數
  19.         ret = (void *)it;//將頭結點賦給ret指標
  20.     }  
  21.     if (ret) {//請求成功
  22.         p->requested += size;//更新slabclass[id]所分配的記憶體總數
  23.         MEMCACHED_SLABS_ALLOCATE(size, id, p->size, ret);  
  24.     } else {  
  25.         MEMCACHED_SLABS_ALLOCATE_FAILED(size, id);  
  26.     }  
  27.     return ret;  
  28. }  
static void *do_slabs_alloc(const size_t size, unsigned int id) {
    slabclass_t *p;
    void *ret = NULL;
    item *it = NULL;
 
    if (id < POWER_SMALLEST || id > power_largest) {//判斷id是否會導致slabclass[]陣列越界
        MEMCACHED_SLABS_ALLOCATE_FAILED(size, 0);
        return NULL;
    }
 
    p = &slabclass[id];//獲取slabclass[id]的引用
    assert(p->sl_curr == 0 || ((item *)p->slots)->slabs_clsid == 0);//判斷slabclass[id]是否有剩餘的chunk
 
    if (! (p->sl_curr != 0 || do_slabs_newslab(id) != 0)) {//如果slabclass[id]中已經沒有空餘chunk並且試圖向系統申請一個“頁”(slab)的chunk失敗,則返回NULL
    /* We don't have more memory available */
        ret = NULL;
    } else if (p->sl_curr != 0) {//slabclass[id]的空閒連結串列中還有chunk,則直接將其分配出去
        it = (item *)p->slots;//獲取空閒連結串列的頭指標
        p->slots = it->next;//將頭結點指向下一個結點(取下頭結點)
        if (it->next) it->next->prev = 0;//將新頭結點的prev指標置空
        p->sl_curr--;//減少slabclass[id]空閒連結串列中的chunk計數
        ret = (void *)it;//將頭結點賦給ret指標
    }
 
    if (ret) {//請求成功
        p->requested += size;//更新slabclass[id]所分配的記憶體總數
        MEMCACHED_SLABS_ALLOCATE(size, id, p->size, ret);
    } else {
        MEMCACHED_SLABS_ALLOCATE_FAILED(size, id);
    }
 
    return ret;
}

do_slabs_allc()函式首先嚐試從slot列表(被回收的chunk)中獲取可用的chunk,如果有可用的就返回,否則從空閒的chunk列表中獲取可用的chunk並返回。

刪除過期item:

延遲刪除過期item到查詢時進行,可以提高memcache的效率,因為不必每時每刻檢查過期item,從而提高CPU工作效率

使用LRU(last recently used)演算法淘汰資料:

[cpp] view plaincopyprint?在CODE上檢視程式碼片派生到我的程式碼片
  1. /* 
  2.  * try to get one off the right LRU 
  3.  * don't necessariuly unlink the tail because it may be locked: refcount>0 
  4.  * search up from tail an item with refcount==0 and unlink it; give up after 50 
  5.  * tries 
  6.  */
  7. if (tails[id] == 0) {  
  8.     itemstats[id].outofmemory++;  
  9.     return NULL;  
  10. }  
  11. for (search = tails[id]; tries > 0 && search != NULL; tries--, search=search->prev) {  
  12.     if (search->refcount == 0) { //refount==0的情況,釋放掉
  13.         if (search->exptime == 0 || search->exptime > current_time) {  
  14.             itemstats[id].evicted++;  
  15.             itemstats[id].evicted_time = current_time - search->time;  
  16.             STATS_LOCK();  
  17.             stats.evictions++;  
  18.             STATS_UNLOCK();  
  19.         }  
  20.         do_item_unlink(search);  
  21.         break;  
  22.     }  
  23. }  
  24. it = slabs_alloc(ntotal, id);  
  25. if (it == 0) {  
  26.     itemstats[id].outofmemory++;  
  27.     /* Last ditch effort. There is a very rare bug which causes 
  28.      * refcount leaks. We've fixed most of them, but it still happens, 
  29.      * and it may happen in the future. 
  30.      * We can reasonably assume no item can stay locked for more than 
  31.      * three hours, so if we find one in the tail which is that old, 
  32.      * free it anyway. 
  33.      */
  34.     tries = 50;  
  35.     for (search = tails[id]; tries > 0 && search != NULL; tries--, search=search->prev) {  
  36.         if (search->refcount != 0 && search->time + 10800 < current_time) { //最近3小時沒有被訪問到的情況,釋放掉
  37.             itemstats[id].tailrepairs++;  
  38.             search->refcount = 0;  
  39.             do_item_unlink(search);  
  40.             break;  
  41.         }  
  42.     }  
  43.     it = slabs_alloc(ntotal, id);  
  44.     if (it == 0) {  
  45.         return NULL;  
  46.     }  
  47. }  
/*
 * try to get one off the right LRU
 * don't necessariuly unlink the tail because it may be locked: refcount>0
 * search up from tail an item with refcount==0 and unlink it; give up after 50
 * tries
 */

if (tails[id] == 0) {
	itemstats[id].outofmemory++;
	return NULL;
}

for (search = tails[id]; tries > 0 && search != NULL; tries--, search=search->prev) {
	if (search->refcount == 0) { //refount==0的情況,釋放掉
		if (search->exptime == 0 || search->exptime > current_time) {
			itemstats[id].evicted++;
			itemstats[id].evicted_time = current_time - search->time;
			STATS_LOCK();
			stats.evictions++;
			STATS_UNLOCK();
		}
		do_item_unlink(search);
		break;
	}
}
it = slabs_alloc(ntotal, id);
if (it == 0) {
	itemstats[id].outofmemory++;
	/* Last ditch effort. There is a very rare bug which causes
	 * refcount leaks. We've fixed most of them, but it still happens,
	 * and it may happen in the future.
	 * We can reasonably assume no item can stay locked for more than
	 * three hours, so if we find one in the tail which is that old,
	 * free it anyway.
	 */
	tries = 50;
	for (search = tails[id]; tries > 0 && search != NULL; tries--, search=search->prev) {
		if (search->refcount != 0 && search->time + 10800 < current_time) { //最近3小時沒有被訪問到的情況,釋放掉
			itemstats[id].tailrepairs++;
			search->refcount = 0;
			do_item_unlink(search);
			break;
		}
	}
	it = slabs_alloc(ntotal, id);
	if (it == 0) {
		return NULL;
	}
}

從item列表的尾部開始遍歷,找到refcount==0的chunk,呼叫do_item_unlink()函式釋放掉,另外,search->time+10800<current_time(即最近3小時沒有被訪問過的item),也釋放掉--這就是LRU演算法的原理。

附:阿里2014筆試題一道:

某快取系統採用LRU淘汰演算法,假定快取容量為4,並且初始為空,那麼在順序訪問一下資料項的時候:1,5,1,3,5,2,4,1,2出現快取直接命中的次數是?,最後快取中即將準備淘汰的資料項是? 答案:3, 5 解答:
  1. 1調入記憶體 1
  2. 5調入記憶體 1 5
  3. 1調入記憶體 5 1(命中 1,更新次序)
  4. 3調入記憶體 5 1 3
  5. 5調入記憶體 1 3 5 (命中5)
  6. 2調入記憶體 1 3 5 2
  7. 4調入記憶體(1最久未使用,淘汰1) 3 5 2 4
  8. 1調入記憶體(3最久未使用,淘汰3) 5 2 4 1
  9. 2調入記憶體 5 4 1 2(命中2)
因此,直接命中次數是3,最後快取即將準備淘汰的資料項是5