通信协议
Redis单线程对外提供服务,单节点在跑满一个CPU核心的情况下,可以达到10w/s的超高QPS。
RESP(Redis Serialization Protocol)
Redis协议将传输的结构数据分为5种最小单元类型,单元结束时统一加上回车换行符号\r\n。
- 单行字符串,
+
开头; - 多行字符串,
$
开头; - 整数值,
:
开头; - 错误消息,
-
开头; - 数组,
*
开头,后接数组的长度。
单行字串'hello world'
+hello world\r\n
多行字串
$\r\nhello world\r\n
整数1024
:1024\r\n
错误
-WRONGTYPE Operation against a key holding the wrong kind of value\r\n
数组[1,2,3]
*3\r\n:1\r\n:2\r\n3\r\n
NULL,多行字串表示,长度-1
$-1\r\n
空行,多行字串表示,长度0
$0\r\n\r\n
小结
虽然协议中有大量冗余的回车换行符,但其显示比较友好。