Memcached of pseudo-distributed memory cache

Why does the title call Memcached a pseudo-distributed memory cache?

1. The Memcached server does not have a clustering solution, so there is a risk of a single point of failure.

2. The Memcached server has no persistence mechanism, the data is stored in memory, and the data is lost when the process exits.

 

1. Memcached installation and use

 Official website: http://memcached.org/

 Download address: http://www.memcached.org/files/memcached-1.4.34.tar.gz

 

 Memcached depends on the libevent library:

apt-get install libevent-dev

 

yum install libevent-devel

   

   Other system installation scripts can search by themselves...   

 

 

Unzip the memcached-1.xxtar.gz installation package:

wget https://memcached.org/latest
[you might need to rename the file]
tar -zxf memcached-1.x.x.tar.gz
cd memcached-1.x.x
./configure --prefix=/usr/local/memcached
make && make test && sudo make install

  

 

2. Memcached memory management mechanism


 Description: The memcached server process starts and  allocates a memory area of ​​the specified size (default: 64M)     through the -m  parameter.

The memory division mechanism of memcached is shown in the figure. First, understand several concepts:

1. Slab Class: Slabs with the same memory size belong to a Slab Class, and  the memory block growth of each Slab is distinguished by the -f  parameter

2. Slab: 包含多个Chunk,每个Slab(默认:1M)按照最小内存大小进行切分

3. Chunk: 存储 [K-V] 数据的最小内存单元,通过 -n 参数指定最小内存分配空间

 

启动参数参考(部分命令未用过,注意是否支持):

-p                             指定端口号(默认: 11211  

-U <num>                UDP监听端口 (默认: 11211, 0 时关闭) 

-s <file>                   用于监听的UNIX套接字路径(禁用网络支持)

-a <mask>               UNIX套接字访问掩码,八进制数字(默认:0700)

-m                             指定最大使用内存大小(默认:64MB)

-t                               线程数(默认:4)

-l <ip_addr>             绑定地址 (默认:允许所有地址绑定,存在安全隐患) 

-d start                      启动memcached服务   

-d restart                   重起memcached服务   

-d stop|shutdown     关闭正在运行的memcached服务   

-u <username>         绑定使用指定用于运行进程 <username>  (只有root用户可以使用这个参数)

-P <file>                    将PID写入文件<file>,这样可以使得后边进行快速进程终止, 需要与 -d 一起使用

-M                               内存耗尽时返回错误,而不是删除项

-c                                 最大同时连接数(默认:1024)

-f                                 块大小增长因子(默认:1.25)

-n <bytes>                 最小分配空间,key+value+flags默认是48

-k                                锁定所有内存页。注意你可以锁定的内存上限。试图分配更多内存会失败的,所以留意启动守护                                                  进程时所用的用户可分配的内存上限。(不是前面的 -u <username> 参数;在sh下,使用命                                                令"ulimit -S -l NUM_KB"来设置)

-v                                提示信息(在事件循环中打印错误/警告信息)

-vv                              详细信息(还打印客户端命令/响应)

-vvv                            超详细信息(还打印内部状态的变化)

-h                                打印这个帮助信息并退出。

-i                                 打印memcached和libevent的许可。

-L                                尝试使用大内存页(如果可用的话)。提高内存页尺寸可以减少"页表缓冲(TLB)"丢失次数,提                                                 高运行效率。为了从操作系统获得大内存页,memcached会把全部数据项分配到一个大区块

-D <char>                  使用 <char> 作为前缀和ID的分隔符。这个用于按前缀获得状态报告。默认是":"(冒 号)。如果                                               指定了这个参数,则状态收集会自动开启;如果没指定,则需要用命令"stats detail on"来开启

-R                                每个连接可处理的最大请求数

-C                                禁用CAS

-b                                设置后台日志队列的长度(默认:1024)

-B                                绑定协议 - 可能值:ascii,binary,auto(默认)

-I                                 重写每个数据页尺寸。调整数据项最大尺寸

 

客户端写入命令,服务端的处理流程大致如下图:


 

 

3. Memcached如何解决单点故障?

    方案一:采用客户端分片,例如:一致性哈希

    方案二:引入中间层代理,例如:Twemproxy

    

4. Memcached如何支持高可用方案?

    1. 主备复制:http://repcached.lab.klab.org/

    After the introduction of the master-standby replication scheme, in practice, LVS can be used as a front-end load balancing and Keepalived node heartbeat check. There are many architecture cases on the Internet LVS+Keepalived+Twemproxy. In short, specific solutions are designed in combination with specific business scenarios.

 

5. Simple Enterprise Architecture Design


 The figure only shows the role of Memcached in the enterprise architecture. The reason why it is called a simple enterprise architecture is because the architecture diagram described above still has many detailed problems in practice, and these problems are relatively serious and even affect The stability of system operation. In actual development, appropriate adjustments or redesigns should be made according to business characteristics.

 

6. Performance test report

 [To be added]

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326743412&siteId=291194637