The principle Redis Introduction

The principle Redis Introduction

Internal structure and introduce the principle of stand-alone servers

Object System

Redis data construction method

Object System

Redis is a key-value pair (key-value pair) database, which each key has a value corresponding thereto, and the keys and values ​​are objects (Object), wherein:

  • The key is always a String object (String Object).
  • And the value may be a string object, the object list (List Object), hash object (Hash Object), any set of objects (Set Object) or an ordered set of objects (Sorted Set Object / Zset Object) one, five kinds of objects corresponding string key operation in our database of Redis (sET, BITOP, PFADD), a list of key-value pairs (RPUSH, LRANGE), hash key pair (HADD, HLEN), a set of key-value pair ( SADD, SMEMBERS) and an ordered collection of key-value pairs (ZADD, ZRANGE).

For example, execute the command SET msg "hello world" make up a database associated with the new key-value pair, where the key is a content "msg" of the object contains a string, and the value is a content containing "hello world" of string object;

On the other hand, Run SADD fruits "apple" "banana" "cherry" is created as a key string object, collection of objects of value pairs.

Object graph

Here Insert Picture Description

Implementation object

A method to achieve different types of objects Redis

Implementation object

Each object has Redis the underlying data structure corresponding thereto, and in order to allow objects at a variety of different application scenarios are preferably
show performance, Redis for each object provides two types of data structure to achieve:

  • The first category is to optimize the performance of a special encoded data structures (encoded Data Structure) , these data structures used primarily "In the CPU mode change memory" to save memory. Mainly in the encoded data structure comprising a number of relatively small value of the object, or the use of relatively small volume value: for example, a string of relatively short character string object includes the set contains only small thirty-five element, or if the list contains only ten when the entries to these situations can be encoded using a data structure.

  • The other is common data structure , that is, we are in the book, see the paper data structure, such as a doubly linked list, dictionary, jump tables and so on. Compared to the coded data structure, the common data structure requires more memory, but can provide more powerful, general structure of the main data contained in the value of the object more, or when using a relatively large volume values.

When you create a new object, Redis will give priority to the use of coded data structure to represent the object, and, if necessary, automatically convert the representation of objects is a common data structure.

data structure

Encoded data structure:

  • Packing List (zip list)
  • Set of integers (int set)

Common data structure:

  • Simple dynamic string (SDS, simple dynamic string)
  • Doubly linked list - dictionary
  • Jump table

Also note that the data to save memory is not encoded specific structure - a common data structure corresponding memory optimization mode, there are two or more such as SDS representation, allows different representations of the same object at different also stay productive as possible usage scenarios.

The relationship between the object data structure

Here Insert Picture Description

Introduction underlying data structure

Packing List, integers, SDS, two-way lists, dictionaries, jump table

Packing List

Compression list has the following characteristics:

  • Compression list contains items are ordered, the list ends respectively headers and footers.
  • Each entry may store a string, integer or floating point.
  • May start from the header or footer from traversing the entire compressed list, complexity is O (N).
  • Positioning the compressed items on the list of the specified index, complexity is O (N).
  • Use compression to store a list of values ​​consume more memory than using a doubly linked list to store the value of consuming less memory.
    Here Insert Picture Description
Integers

Integers has the following characteristics:

  • 集合元素只能是整数(最大 为 64 位),并且集合中不会出 现重复的元素。
  • 集合的底层使用有序的整数数组来表示。
  • 数组的类型会随着新添加元素的 类型而改变:举个例子,如果集合中位长度最大的元素可以使用16 位整数来保存,那么数 组的类型就是
    int16_t ,而如果集合中位长度最大的元素可以使用 32 位整数来保存的话,那么数组的类型就是 int32_t ,诸如此类。
  • 数组的类型只会自动增大,但不会减小。
    Here Insert Picture Description
SDS

Redis 使用 SDS (simple dynamic string)而不是 C 语言的字符串格式(以空字符 为结尾的字符数组)来作为 Redis 的默认字符串表示,SDS 具有以下特点:

  • 可以储存位数组(实现 BITOP 和 HyperLogLog)、字符串、整数和浮点数,其中超 过 64 位的整数和超过 IEEE 754 标准的浮点数使用字符串来表示。
  • 具有 int 、 embstr 和 raw 三种表示形式可选,其中 int 表示用于储存小于等于 64 位的整数,embstr 用来储存比较短的位数组和字符串,而其他格式的 值则由 raw 格式储存。
  • 比起 C 语言的字符串格式, SDS 具有以下四个优点: 1)常数复杂度获取长度值; 2)不会引起缓冲区溢出; 3)通过预分配和惰性释放两种策略来减少内存重分配的 执行次数; 4)可以储存二进制位。
    Here Insert Picture Description
双向链表

Redis 的双向链表实现具有以下特性:

  • 双向、无环、带有表头和表尾指针。
  • 一个链表包含多个项,每个项都是一个字符串对象,换句话来说,一个列表对象可以包含多个字符串对象。
  • 可以从表头或者表尾遍历整个链表,复杂度为 O(N) 。
  • 定位特定索引上的项,复杂度为 O(N) 。
  • 链表带有长度记录属性,获取链表的当前长度的复杂度为 O(1) 。
    Here Insert Picture Description
字典

Redis 的字典实现具有以下特性:

  • 查找、添加、删除键值对的复杂度为 O(1) ,键和值都是字符串对象。
  • 使用散列表(hash table)为底层实现,使用链地址法(separate chaining)来解决键冲突。
  • Redis 会在不同的地方使用不同的散列算法,其中最常用的是 MurmurHash2 算法。
  • 在键值对数量大增或者大减的时候会对散列表进行重新散列(rehash),并且这个 rehash 是渐进式、分多次进行的,不会在短时间内耗费大量 CPU 时间,㐀成服务器阻塞。
    Here Insert Picture Description
跳跃表

Redis 的跳跃表实现具有以下特点:

  • 支持平均 O(log N) 最坏 O(N) 复杂度的节点查找操作,并且可以通过执行范围性(range)操作来批量地获取有序的节点。
  • 跳跃表节点除了实现跳跃表所需的层(level)之外,还具有 score 属性和 obj 属性:前者是一个浮点数,用于记录成员的分值;而后者则是一个字符串对象,用来记录成员本身。
  • 和字典一起构成 ZSET 结构,用于实现 Redis 的有序集合结构:其中字典用于快㏿ 获取元素的分值(比如实现 ZSCORE 命令),以及判断元素是否存在;而跳跃表则用于执行范围操作(比如实现 ZRANGE 命令)。
    Here Insert Picture Description

数据库实现

Redis 储存键值对的方式

Redis 数据库的实现方法

在 Redis 里面,每个数据库都是一个字典,该字典的键和值都是我们之前提到的对象,其中:

  • 字典的键总是一个字符串对象,它储存了用户为键设置的键名。
  • 字典的值则可以是字符串对象、列表对象、散列对象、集合对象或者有序集合对象的其中一个。

因为数据库就是字典,所以针对数据库的操作都是基于字典操作来 实现的:

  • 比如说,使用 DEL 命令删除一个数据库键,就是删除数据库对应的字典的键值对。
  • 又比如说,使用 FLUSHDB 清空数据库,就是清空数据库对应的字典。
  • 诸如此类。

数据库示例

Here Insert Picture Description

记录过期时间

为了记录数据库键的过期时间,Redis 为每个数据库创建了另一个字典,专门使用这个字典来记录键的 过期时间,其中:

  • 字典的键指向数据库键对象,也即是带有过期时间的那个键(数据库字典和储存过期时间的字典通过指针使用同一个键对象,不会㐀成任何资源浪费)。
  • 键的值则是一个毫秒格式的 UNIX 时间戳,记录了键到期的时间。
带有过期时间的数据库示例

Here Insert Picture Description

持久化实现

RDB 持久化和 AOF 持久化的实现原理

RDB 持久化实现原理

Redis 会遍历服务器中的所有数据库,访问数据库中的所有键值对,并根据键值对的类型,将这些键值对以及它们的过期时间写入到 RDB 文件里面。
Here Insert Picture Description

AOF 持久化实现原理

AOF 持久化功能在每次执行命令之后就将协议格式的命令写入到 AOF 缓冲区,然后服务器再定期将缓冲区的内容写入到 AOF 文件,还原数据时只要重新执行 AOF 文件里面的命令即可。
Here Insert Picture Description

AOF 文件重写的实现原理

无须对现有的 AOF 文件进行处理,直接根据数据库目前的状态来生成新的 AOF 文件。
Here Insert Picture Description
SELECT
RPUSH alphabet …
HMSET book …
SET message …

命令处理模型

Redis 处理命令请求的方法与模式

命令处理模型

Redis 服务器使用 Reactor 模式来连接多个客户端并处理命令请求,其中:

  • 客户端发送的命令请求会被放到一个有序的 队列里面。
  • 服务器使用单线程方式来执行命令 —— 服务器每次从队列里面取出一个请求并处理它,只有在当前的命令请求处理完毕之后,服务器才会去处理下一个命令请求。
  • 单线程的命令处理方式使得针对服务器以及数据库的操作都不需要加锁,好处是极大地方便了功能的实现,减少了代码出错的可能性;而坏处则是不能最大化地使用硬件的多 线程能力。
    Here Insert Picture Description

复习

本节重点

Redis 数据库中的键和值都是对象,其中键总是一个字符串对象,而值则可以是多种类型对象的其中一个。

Redis 为每种对象都设置了两种或以上的表示方式,使得 对象可以在不同的应用场景中都有最好的性能表现。

Redis database is a dictionary, a dictionary database operations are operations to achieve; in addition, Redis also uses another dictionary to record the expiration time special key-value pairs.

RDB persistence be achieved by traversing a file is written to the database and the key, and the AOF is performed by persistent record command requests the server to achieve.

Reactor pattern Redis server uses to process client command requests.

Learn more about the underlying implementation of knowledge Redis

Redis because the underlying implementation itself is a huge topic, so this lesson simply introduced Redis server implementation knowledge of stand-alone databases.

As for the other features provided by Redis server (such as transaction, publish and subscribe, Lua script) and introduced multi-machine function (such as copying, Sentinel and clusters) Redis provided is not involved.

If you are interested to continue to understand this knowledge, you can refer to what I wrote, "Redis design and implementation of" one book, which details the Redis almost all of the features of the realization of the principle, should be interested in learning about the students in this area of ​​little help.

Book Home: RedisBook.com
Here Insert Picture Description

Published 252 original articles · won praise 151 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_39885372/article/details/104290080