How-to Dump Keys from Memcache--reference

<div class="meta">
<div class="submitted">Submitted by Lars Windolf on 19. October 2012 - 21:53


<div class="submitted">http://lzone.de/dump%20memcache%20keys

You spent already 50GB on the memcache cluster,but you still see many evictions and the cache hit ratio doesn't look good since a few days. The developers swear that they didn't change the caching recently,they checked the code twice and have found no problem.

What now? How to get some insight into the black box of memcached? One way would be to add logging to the application to see and count what is being read and written and then to guess from this about the cache efficiency. For to debug what's happening we need to set how the cache keys are used by the application.

An Easier Way

Memcache itself provides a means to peek into its content. The memcache protocol provides to peek into the data that is organized by slabs (categories of data of a given size range). There are some significant limitations though:

  1. You can only dump keys per slab class (keys with roughly the same content size)
  2. You can only dump one page per slab class (1MB of data)
  3. This is an unofficial feature that

The second limitation is propably the hardest because 1MB of several gigabytes is almost nothing. Still it can be useful to watch how you use a subset of your keys. But this might depend on your use case.

If you don't care about the technical details just skip to the to learn about what tools allow you to easily dump everything. Alternatively follow the following guide and try the commands against your memcached setup.

How it Works

First you need to know how memcache organizes its memory. If you start memcache with option "-vv" you see the slab classes it creates. For example

$ memcached -vv
slab class   1: chunk size        96 perslab   10922
slab class   2: chunk size       120 perslab    8738
slab class   3: chunk size       152 perslab    6898
slab class   4: chunk size       192 perslab    5461
[...]

In the configuration printed above memcache will keep fit 6898 pieces of data between 121 and 152 byte in a single slab of 1MB size (6898*152). All slabs are sized as 1MB per default. Use the following command to print all currently existing slabs:

stats slabs

If you've added a single key to an empty memcached 1.4.13 with

set mykey 0 60 1
1
STORED

you'll now see the following result for the "stats slabs" command:

stats slabs
STAT 1:chunk_size 96
STAT 1:chunks_per_page 10922
STAT 1:total_pages 1
STAT 1:total_chunks 10922
STAT 1:used_chunks 1
STAT 1:free_chunks 0
STAT 1:free_chunks_end 10921
STAT 1:mem_requested 71
STAT 1:get_hits 0
STAT 1:cmd_set 2
STAT 1:delete_hits 0
STAT 1:incr_hits 0
STAT 1:decr_hits 0
STAT 1:cas_hits 0
STAT 1:cas_badval 0
STAT 1:touch_hits 0
STAT active_slabs 1
STAT total_malloced 1048512
END

The example shows that we have only one active slab type #1. Our key being just one byte large fits into this as the smallest possible chunk size. The slab statistics show that currently on one page of the slab class exists and that only one chunk is used.

Most importantly it shows a counter for each write operation (set,incr,decr,cas,touch) and one for gets. Using those you can determine a hit ratio!

You can also fetch another set of infos using "stats items" with interesting counters concerning evictions and out of memory counters.

stats items
STAT items:1:number 1
STAT items:1:age 4
STAT items:1:evicted 0
STAT items:1:evicted_nonzero 0
STAT items:1:evicted_time 0
STAT items:1:outofmemory 0
STAT items:1:tailrepairs 0
STAT items:1:reclaimed 0
STAT items:1:expired_unfetched 0
STAT items:1:evicted_unfetched 0
END

What We Can Guess Already...

Given the statistics infos per slabs class we can already guess a lot of thing about the application behaviour:

  1. How is the cache ratio for different content sizes?
    • How good is the caching of large HTML chunks?
  2. How much memory do we spend on different content sizes?
    • How much do we spend on simple numeric counters?
    • How much do we spend on our session data?
    • How much do we spend on large HTML chunks?
  3. How many large objects can we cache at all?

Of course to answer the questions you need to know about the cache objects of your application.

Now: How to Dump Keys?

Keys can be dumped per slabs class using the "stats cachedump" command.

stats cachedump  

To dump our single key in class #1 run

stats cachedump 1 1000
ITEM mykey [1 b; 1350677968 s]
END

The "cachedump" returns one item per line. The first number in the braces gives the size in bytes,the second the timestamp of the creation. Given the key name you can now also dump its value using

get mykey
VALUE mykey 0 1
1
END

This is it: iterate over all slabs classes you want,extract the key names and if need dump there contents.

Dumping Tools

There are different dumping tools sometimes just scripts out there that help you with printing memcache keys:

Does freeze your memcached process!!!

Be careful when using this in production. But can thereby workaround the 1MB limitation and really dump all keys.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


前言设计一个缓存系统,不得不要考虑的问题就是:缓存穿透、缓存击穿与失效时的雪崩效应。缓存穿透缓存穿透是指查询一个一定不存在的数据,由于缓存是不命中时被动写的,并且出于容错考虑,如果从存储层查不到数据则不写入缓存,这将导致这个不存在的数据每次请求都要到存储层去查询,失
在192.168.80.100要联网关闭防火墙及SElinuxsystemctlstopfirewalld//关闭防火墙setenforce0//关闭监控memcached服务器:上传源码包和插件包:yuminstalllrz*-y把memcached-1.5.6.tar.gzlibevent-2.1.8-stable.tar.gz软件包拉入
Redis与Memcache对比:1.Memcache是一个分布式的内存对象缓存系统而redis是可以实现持久存储2.Memcache是一个LRU的缓存redis支持更多的数据类型3.Memcache是多线程的redis是单线程的4.二者性能几乎不相上下,实际上redis会受到硬盘持久化的影响,但是性能仍然保持在与Memcache不相上下,
安装Mencache:关闭防火墙及SElinuxsystemctl  stop  firewalldsetenforce  0memcached服务器:上传源码包和插件包:yum install  lrz*  -ymemcached-1.5.9.tar.gzlibevent-2.1.8-stable.tar.gz解压tarxfmemcached-1.5.6.tar.gz-C/optarxflibevent-2.1.8-stabl
#安装php的yaf模块,参考https://www.cnblogs.com/shifu204/p/6743578.htmlhttps://www.cnblogs.com/jiqing9006/p/9646872.htmlCentOS用phpize安装PHP扩展出现Can'tfindPHPheadersin/usr/include/php原因:https://wangzq-phper.iteye.com/blog/2297792#php的memcache的
win10下安装配置apache、php、mysql、redis、memcache资源官网下载亲测可用。名称版本下载地址网盘提取码apache2.4.38-x64下载地址33h4mysql5.5.62下载地址zicrphp7.1.26-ts-x64下载地址6ar6redis4.0.2.3-x64下载地址bnpkmemcache1.4.
使用理由:就是为了频繁查询数据库。并且数据有多!那么存到memcached内存就能够避免数据库的不断被訪问,从而提速一、安装memcachememcached在windows7上的安装问题 如今安装包:http://download.csdn.net/detail/qq1355541448/7587663或者 http://www.jb51.net/softs/448
https://www.cnblogs.com/xrq730/p/4948707.htmlMemCache是什么理解memcached的内存存储机制SlabAllocator内存分配机制Memcached默认情况下采用了名为SlabAllocator的机制分配、管理内存。0.之前内存分配的弊端在该机制出现以前,内存的分配是通过对所有记录简单地进行malloc和
 转载请注明原文地址:https://www.cnblogs.com/ygj0930/p/10923221.html 一:Memcache是什么,为什么要用它 二:Memcache指令有哪些 三:Memcache使用实例 四:Memcache深入解读
memcache讲解和在.net中初使用2017年10月17日22:51:36 等待临界 阅读数:503  前言传统数据库面临的问题数据库死锁磁盘IO正文了解memcache原理基本命令memcache与memcachedmemcache的适用范围memcache的客户端和服务端memcache与redis异同
前言设计一个缓存系统,不得不要考虑的问题就是:缓存穿透、缓存击穿与失效时的雪崩效应。缓存穿透缓存穿透是指查询一个一定不存在的数据,由于缓存是不命中时被动写的,并且出于容错考虑,如果从存储层查不到数据则不写入缓存,这将导致这个不存在的数据每次请求都要到存储层去查询,失
一、概念讲解1.memcache是一个高性能的分布式的内存对象缓存系统,用于动态web应用以减轻数据库负担2.memcache通过在内存里维护一个统一的巨大的hash表,来存储经常被读写的一些数组与文件,从而极大的提高网站的运行效率。3.memcache是一种内存缓存技术,是一种缓存手段,要看情况
前戏Memcached是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载。它通过在内存中缓存数据和对象减少读取数据库的次数,从而减小数据库的压力,提高动态,数据库网站的速度。Memcached基于一个存储键/值对的hashmap。其守护进程是用C编写的,但是客户端可以用任
一.配置环境1.安装jdk和tomcat并将其放至/usr/local目录下[root@server1~]#ls[root@server1~]#tarzxfjdk-7u79-linux-x64.tar.gz-C/usr/local[root@server1~]#tarzxfapache-tomcat-7.0.37.tar.gz-C/usr/local2.为了升级的时候只升级软链接,所以对jdk和tom
一、    缓存系统静态web页面:1、在静态Web程序中,客户端使用Web浏览器(IE、FireFox等)经过网络(Network)连接到服务器上,使用HTTP协议发起一个请求(Request),告诉服务器我现在需要得到哪个页面,所有的请求交给Web服务器,之后WEB服务器根据用户的需要,从文件系统(存放了所有静态页面
session//php文件中ini_set("session.save_handler","memcache");ini_set("session.save_path","tcp://127.0.0.1:11211");session_start();$_SESSION['TEST']='AreyouOK?';echosession_id();不建议在php.
废话少说,直接上代码<?php//实例化memcache类$mem=newmemcache();//连接memcache$mem->connect('localhost','11211');/**判断缓存中是否有数据如果有,就读取,没有,在查询时候,就把查询的数据存入memcache中*///获取缓存数据$
一、概念1、什么是memcacheMemcached是一个自由开源的,高性能,分布式内存对象缓存系统Memcached是以LiveJournal旗下DangaInteractive公司的BradFitzpatric为首开发的一款软件。现在已成为mixi、hatena、Facebook、Vox、LiveJournal等众多服务中提高Web应用扩展性的重要
关于Redis和Memcache在应用中,都可以实现缓存的功能,但是,具体使用情况需要根据具体业务场景,尤其是对缓存数据有特性要求时,需要选择对应的缓存机制。共同点:都是基于内存的数据库,可用作缓存机制区别:1、关于数据类型:Memcache只支持简单的Key/Value数据结构;Redis支持的数据结构相
ThedefaultcachegroupisloadedbasedontheCache::$defaultsetting.Itissettothefiledriverasstandard,howeverthiscanbechangedwithinthe/application/boostrap.phpfile//ChangethedefaultcachedrivertomemcacheCache::$default='me