Twemproxy(nutcracker)是一个memcache、redis协议的轻量级代理。它的主要作用是减少后端缓存服务器的连接数;它支持作为多个后端服务器的代理,并对数据进行sharding分片存储到多个服务器;能够暂时移除后端不可用的服务器节点。
(一)安装
1、安装libtool
yum -y install libtool (解决运行autoconf 缺少 aclocal的问题)
2、安装2.6.4或以上版本 autoconf
如果autoconf为2.6.4以下版本运行时会出现如下错误信息:
安装高版本的autoconf
下载地址: http://ftp.gnu.org/gnu/autoconf/
验证安装版本号:
3、安装twemproxy
a)获取twemproxy源码的方式,还可以直接下载源码包
wget ‘https://github.com/twitter/twemproxy/archive/v0.4.0.tar.gz’
b)configure时可以指定debug的log输出级别 “full、log、yes、no”,默认是no。
根据Twemproxy的使用建议,在生产环境下启动debug log并设置log级别为6是值得的,
可以通过启动参数-v来设置log级别,默认级别是5,可参照下文关于启动参数的说明。
(二)配置
主要配置项说明:
- listen: The listening address and port (name:port or ip:port) for this server pool.
- hash: The name of the hash function. Possible values are:
- one_at_a_time
- md5
- crc16
- crc32 (crc32 implementation compatible with libmemcached)
- crc32a (correct crc32 implementation as per the spec)
- fnv1_64
- fnv1a_64
- fnv1_32
- fnv1a_32
- hsieh
- murmur
- jenkins
- hash_tag: A two character string that specifies the part of the key used for hashing. Eg “{}” or “$$”. Hash tag enable mapping different keys to the same server as long as the part of the key within the tag is the same.
- distribution: The key distribution mode. Possible values are:
- ketama
- modula
- random
- timeout: The timeout value in msec that we wait for to establish a connection to the server or receive a response from a server. By default, we wait indefinitely.
- backlog: The TCP backlog argument. Defaults to 512.
- preconnect: A boolean value that controls if nutcracker should preconnect to all the servers in this pool on process start. Defaults to false.
- redis: A boolean value that controls if a server pool speaks redis or memcached protocol. Defaults to false.
- server_connections: The maximum number of connections that can be opened to each server. By default, we open at most 1 server connection.
- auto_eject_hosts: A boolean value that controls if server should be ejected temporarily when it fails consecutively server_failure_limit times. See liveness recommendations for information. Defaults to false.
- server_retry_timeout: The timeout value in msec to wait for before retrying on a temporarily ejected server, when auto_eject_host is set to true. Defaults to 30000 msec.
- server_failure_limit: The number of consecutive failures on a server that would lead to it being temporarily ejected when auto_eject_host is set to true. Defaults to 2.
- servers: A list of server address, port and weight (name:port:weight or ip:port:weight) for this server pool.
配置举例:
- auto_eject_hosts:true,表明支持自动临时剔除不可用后端服务节点。
- server_retry_timeout: 10000,表明重试被剔除的服务节点是否恢复的时间间隔10秒。不宜设置的太小,否则会不断重试故障节点。
- server_failure_limit: 3, 如果后端服务节点连续访问失败3次就会被自动剔除。
(三)使用
启动参数说明:
常用参数:
- -d 以守护进程的方式启动
- -o 可以log文件的路径
- -c 设置启动使用的配置文件路径
eg.
nutcracker -d -c /opt/twemproxy-0.4.0/conf/nutcracker.yml -o /opt/logs/nutcracker/nutcracker.log -v 6
- -s 设置twemproxy的状态监控端口
默认可以通过telnet或nc命令查看twemproxy的状态,eg.
- -m 设置缓存块的大小,默认16k
在twemproxy中,所有请求和响应使用的内存都是在mbuf中分配的并且使用了零拷贝技术。大块的mbuf能够减少读请求或响应时的系统调用次数,但是当twemproxy处理大量并发连接时,每个连接将会使用一个大块的mbuf,这可能会成为一个问题,所以当处理大量并发客户端连接时,应该尽量把mbuf值设置的小一些,比如512字节。
对Redis命令的支持情况
可参考twemproxy 项目源码中对Redis command support 的具体说明: https://github.com/twitter/twemproxy/blob/master/notes/redis.md
twemproxy对Redis常用数据结构的大部分操作命令都是支持的。根据 Redis 作者 antirez 的测试结果,在大多数情况下,twemproxy 的性能相当不错,与直接操作 Redis 相比,最多只有20%的性能损失,但批量操作如mget的性能比较差,会有50%左右的损失。不过twemproxy的0.4.0 Release版本idning已对mget操作进行了优化,具体细节可查看文章末尾的参考链接。