An article to talk about the communication protocol in Redis-RESP
本篇文章带大家了解一下Redis中的通信协议,介绍一下RESP协议、数据结构的5种单元类型,希望对大家有所帮助!
RESP
RESP(Redis Serialization Protocol)是Redis序列化协议的简写,该协议是纯文本协议,实现过程简单,解析性能较好。【相关推荐:Redis视频教程】
5种单元类型
Redis协议将传输的数据结构分为5种最小的单元类型,单元结束时统一加上回车换行符号\r\n。
1. 单行字符串以 + 符号开头
例:+hello world\r\n
2. 多行字符串以 $ 符号开头,后跟字符串长度
例:$11\r\nhello world\r\n
多行字符串也可以用于表示单行字符串
3. 整数值以 : 符号开头,后跟整数的字符串形式
例::1024\r\n
4. 错误信息以 - 符号开头
参数类型错误
例: -WRONGTYPE Operation against a key holding the wrong kind of value\r\n
5. 数组以 * 号开头,后跟数组的长度
例:*3\r\n:l\r\n:2\r\n:3\r\n
两种特殊类型
1. NULL
NULL使用多行字符串表示,长度为-1
例:$-1\r\n
2. 空字符串
空串用多行字符串表示,长度填0
例:$0\r\n\r\n
空字符串有两个\r\n,因为两个\r\n之间的就是空串
客户端请求服务端
客户端向服务器发送的指令只有一种格式,就是多行字符串数组。
例如一个简单的set指令 set x x 会被序列化成下面的字符串
*3\r\n$3\r\nset\r\n$1\r\nx\r\n$1\r\nx\r\n
控制台展示如下
*3 $3 set $1 x $1 x
服务端响应客户端
服务端响应客户端信息时,将会使用多种数据结构,比客户端发送到服务端时复杂很多,不过即便很复杂,也是上面提到的5种基本类型的组合。
单行字符串响应
127 . 0 .0.1: 6379> set x x OK
上面的OK就是单行字符串响应(没有双引号),即 +OK
错误响应
127 . 0 . 0.1:6379> incr x (error} ERR value is not an integer or out of range
对一个字符串进行自增,服务器抛出错误提醒
-ERR value is not an integer or out of range
整数响应
127.0.0.1:6379> incr books (integer} 1
1就是整数响应 :1
多行字符串响应
127.0.0.1:6379> get x "x"
上面用括号引起来的x就是多行字符串响应,即:
$1 x
数组响应
127.0.0.1:6379> hset info name bibabo (integer) 1 127.0.0.1:6379> hset info age 18 (integer) 1 127.0.0.1:6379> hset info sex male (integer) 1 127.0.0.1:6379> hgetall info 1) "name" 2) "bibabo" 3) "age" 4) "18" 5) "sex" 6) "male"
上面的hgetall命令返回的就是一个数组,第0、2、4的字符串是hash表的key,1、3、6则是value,客户端负责将数组组装成字典返回。
*6 $4 name $6 bibabo $3 age $2 18 $3 sex $4 male
嵌套
127.0.0.1:6379> scan 0 1 )"0" 2) 1 ) "info" 2 )"books" 3 )"author"
scan命令用来扫描服务器包含的所有key列表,通过游标的形式一次获取一部分,该命令返回的是一个嵌套数组,
数组第一个值表示游标的值,如果这个值为0,说明已经遍历完毕,如果不为0,使用这个值作为下一次scan时的参数,
数组的第二个值又是一个数组,这个数组就是key的列表。
*2 $1 0 *3 $4 info $5 books $6 author
结
Redis作为文本协议中含有大量的回车换行符,这会占用网络流量,不过依然有很多项目使用RESP作为通讯协议,因为性能并不是评分的全部,简单性、易理解性、易实现性都需要进行权衡,
一般数据库的瓶颈很少在协议上,而是内部的逻辑处理,Redis使用一个单线程对外提供服务,在CPU跑满的情况下,可以达到10w/s的QPS。
更多编程相关知识,请访问:编程视频!!
The above is the detailed content of An article to talk about the communication protocol in Redis-RESP. For more information, please follow other related articles on the PHP Chinese website!

Redis is an open source memory data structure storage used as a database, cache and message broker, suitable for scenarios where fast response and high concurrency are required. 1.Redis uses memory to store data and provides microsecond read and write speed. 2. It supports a variety of data structures, such as strings, lists, collections, etc. 3. Redis realizes data persistence through RDB and AOF mechanisms. 4. Use single-threaded model and multiplexing technology to handle requests efficiently. 5. Performance optimization strategies include LRU algorithm and cluster mode.

Redis's functions mainly include cache, session management and other functions: 1) The cache function stores data through memory to improve reading speed, and is suitable for high-frequency access scenarios such as e-commerce websites; 2) The session management function shares session data in a distributed system and automatically cleans it through an expiration time mechanism; 3) Other functions such as publish-subscribe mode, distributed locks and counters, suitable for real-time message push and multi-threaded systems and other scenarios.

Redis's core functions include memory storage and persistence mechanisms. 1) Memory storage provides extremely fast read and write speeds, suitable for high-performance applications. 2) Persistence ensures that data is not lost through RDB and AOF, and the choice is based on application needs.

Redis'sServer-SideOperationsofferFunctionsandTriggersforexecutingcomplexoperationsontheserver.1)FunctionsallowcustomoperationsinLua,JavaScript,orRedis'sscriptinglanguage,enhancingscalabilityandmaintenance.2)Triggersenableautomaticfunctionexecutionone

Redisisbothadatabaseandaserver.1)Asadatabase,itusesin-memorystorageforfastaccess,idealforreal-timeapplicationsandcaching.2)Asaserver,itsupportspub/submessagingandLuascriptingforreal-timecommunicationandserver-sideoperations.

Redis is a NoSQL database that provides high performance and flexibility. 1) Store data through key-value pairs, suitable for processing large-scale data and high concurrency. 2) Memory storage and single-threaded models ensure fast read and write and atomicity. 3) Use RDB and AOF mechanisms to persist data, supporting high availability and scale-out.

Redis is a memory data structure storage system, mainly used as a database, cache and message broker. Its core features include single-threaded model, I/O multiplexing, persistence mechanism, replication and clustering functions. Redis is commonly used in practical applications for caching, session storage, and message queues. It can significantly improve its performance by selecting the right data structure, using pipelines and transactions, and monitoring and tuning.

The main difference between Redis and SQL databases is that Redis is an in-memory database, suitable for high performance and flexibility requirements; SQL database is a relational database, suitable for complex queries and data consistency requirements. Specifically, 1) Redis provides high-speed data access and caching services, supports multiple data types, suitable for caching and real-time data processing; 2) SQL database manages data through a table structure, supports complex queries and transaction processing, and is suitable for scenarios such as e-commerce and financial systems that require data consistency.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Notepad++7.3.1
Easy-to-use and free code editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6
Visual web development tools
