This article brings you a detailed introduction (example explanation) about Redis compression list. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. help.
This article mainly introduces one of Redis's methods of data storage, compressed list.
This article will introduce
1 and compressed list (ziplist) usage scenarios
2. How to achieve the effect of saving memory?
3. Storage format of compressed list
4. Chain update problem
5 .conf file configuration.
In practice, the main operation is to configure the conf configuration file. There is no exact value, but more of an empirical value. Some projects will directly use the original default values. This article will be helpful to better understand the underlying storage logic of a database. To study and store energy, you need both broad knowledge and profound knowledge. I hope this article will be useful to friends who are also learning Redis. (Recommended tutorial: Redis tutorial)
1. Usage scenarios of compressed list (ziplist):
Redis is used to optimize data storage and save memory , the optimization scheme of using compressed lists is implemented at the bottom of lists, dictionaries (hash keys) and sorted sets.
For example, if the string stored in a hash key is relatively short, Redis will store it in a compressed list format, that is, convert it to a byte array for storage. If the integer value stored internally in a hash key is relatively small, it will also be stored as a node in the compressed list. In the same way, the storage of small data by list keys is similar to the operation of hash keys.
In this way, the compressed list is not a storage data structure in Redis that developers can call directly, but an underlying effort in Redis to optimize data storage. It is still important to understand this.
2. How to achieve the effect of saving memory?
Compressed list is a serialized data structure. The function of this data structure is to store a series of data and its encoding information in a continuous memory area. This memory is physically continuous. . But it is logically divided into components, i.e. nodes. The purpose is to reduce unnecessary memory overhead as much as possible under certain controllable time complexity conditions, so as to achieve the effect of saving memory. You need to understand how to achieve the effect of saving memory, and you also need to understand the storage format of the compressed list.
3. Storage format of compressed list:
Compressed list (ziplist) is Redis list key, hash key and ordered set key One of the underlying implementations, its essence is a serialized data storage structure. Different from ordinary situations, Redis uses double-ended linked lists to represent lists, hash tables to represent hash keys, and hash tables and jump lists to represent ordered sets. When a list or hash dictionary/ordered set contains only a small amount of content, and each list item or hash item/ordered set item is a small integer, or a relatively short string. Then Redis will use the compressed list for the underlying implementation.
The compressed list consists of a series of continuous memory blocks specially encoded by Redis. Each memory block is called a node (entry), and a compressed list can contain many nodes. The data format stored in each node can be a byte array (Chinese strings, etc. will be converted into byte arrays) or integer values.
The length of the byte array can be one of the following:
1. The length is less than or equal to 63 bytes (2 to the 6th power)
2. The length is less than Equal to 16383 bytes (2 to the 14th power)
3. The length is less than or equal to 4294967295 bytes (2 to the 32nd power)
The integer value may be one of the following six types :
1. 4-bit long unsigned integer between 0-12
2. 1-byte long signed integer
3. 3 words Section length signed integer
4. int16_t type integer
5. int32_type integer
6. int64_t type integer
Normal The difference between the storage format and the compressed list storage format:
The list storage structure is typically a double-ended linked list. Each value is represented by a node, and each node will point to the previous Pointers to one node and the following node, and a pointer to the string value the node contains. The string value is stored in three parts. The first part stores the string length, the second part stores the remaining available bytes in the string value, and the third part stores the string data itself. Therefore, a node often needs to store 3 pointers, 2 integers recording string information, the string itself and an extra byte. The overall additional overhead is significant (21 bytes).
Format of compressed list nodes:
Each node consists of three parts: previous_entry_length, encoding, and content. When traversing the compression list, it is traversed from back to front.
1. previous_entry_length records the length of the previous node. Just subtract this value from the current pointer to reach the starting address of the previous node.
2. encoding records the type and length of data stored in the node content attribute
3. content records the value of a node
Obviously compressing the list saves a lot of storage space. But it will also cause the following problems.
4. Issues with chain updates:
Generally speaking, if the overall length of the previous node is less than 254 bytes, the previous_entry_length attribute only needs 1 byte of space to store this length value. When the previous node is larger than 254 bytes, the previous_entry_length attribute uses 5 bytes of space to record the length value.
When a new node is inserted before a node with a length of about 254 bytes, previous_entry_length needs to be added to record the offset of this node to the new node. At this time, the length of this node must be greater than 254 bytes. Therefore, the node after this node cannot only use one byte of previous_entry_length to record the information of this node, but requires 5 bytes to record. If the length of multiple consecutive nodes is about 254 bytes, the insertion and deletion of nodes occurs before/after one of the nodes (the reasoning of deletion is opposite to that of insertion, and the original record of the previous node with 5 bytes may become 1 byte), may trigger chain updates. Obviously, this is very detrimental to the operating efficiency of the system. However, this situation still rarely occurs in practical applications.
The double-ended linked list will be much "easier" in updating, adding and deleting nodes. Because the information stored in each node is relatively independent.
Practical significance:
To estimate how many bytes of storage space a node occupies, appropriately adjust the storage format of the field without making the stored field value occupy less than 254 words of storage space. section (minus the encoding attribute and previous_entry_length attribute) or so. Related commands to view the length of string and hash key values in
Redis:
1. Query the length of the value corresponding to the string key
Command:
Strlen
For example:
127.0.0.1:6379> strlen m_name
(integer) 8
2. Query the length of a certain field in the hash key
Command:
Hstrlen
For example:
127.0.0.1:6379> hstrlen good_list good_list1
(integer) 226
5. Conf file configuration:
By modifying the configuration file , you can control whether to use a compressed list to store the maximum number of elements and the size of the maximum element of the relevant key
Configuration in the Conf file:
1.
[] -max-ziplist-entries: Indicates that the maximum number of elements of the key, that is, the number of nodes under the specified value in a key will be stored in a compressed list
[] -max-ziplist-value: Indicates the maximum size of each node in the compressed list in bytes
In actual use, a certain element of a list key/hash key often stores a relatively large The amount of information will be greater than 64 bytes, so it is likely to be greater than 64 during configuration. At the same time, taking into account the actual storage capacity of data and the size of previous_entry_length mentioned above, [] -max-ziplist-value should be reasonably adjusted Configuration.
Configuration file content:
############## ADVANCED CONFIG ########################## 哈希键 # Hashes are encoded using a memory efficient data structure when they have a # small number of entries, and the biggest entry does not exceed a given # threshold. These thresholds can be configured using the following directives. hash-max-ziplist-entries 512 hash-max-ziplist-value 64 有序集合键 # Similarly to hashes and lists, sorted sets are also specially encoded in # order to save a lot of space. This encoding is only used when the length and # elements of a sorted set are below the following limits: zset-max-ziplist-entries 128 zset-max-ziplist-value 64 列表键,比较特殊,直接使用制定大小kb字节数表示(有些conf文件的列表键与hash键的表达式没太大区别) # Lists are also encoded in a special way to save a lot of space. # The number of entries allowed per internal list node can be specified # as a fixed maximum size or a maximum number of elements. # For a fixed maximum size, use -5 through -1, meaning: # -5: max size: 64 Kb <-- not recommended for normal workloads # -4: max size: 32 Kb <-- not recommended # -3: max size: 16 Kb <-- probably not recommended # -2: max size: 8 Kb <-- good # -1: max size: 4 Kb <-- good # Positive numbers mean store up to _exactly_ that number of elements # per list node. # The highest performing option is usually -2 (8 Kb size) or -1 (4 Kb size), # but if your use case is unique, adjust the settings as necessary. list-max-ziplist-size -2
Case:
Use the default configuration before modifying the configuration:
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
127.0.0.1:6379> hstrlen good_list good_list1
(integer) 226
127.0.0.1:6379> object encoding good_list
"hashtable"
Modify configuration:
hash-max-ziplist-entries 512
hash-max-ziplist-value 254
Note: You need to restart the server after modifying the configuration
127.0.0.1:6379> hstrlen good_list good_list1
(integer) 226
127.0.0.1 :6379> object encoding good_list
"ziplist"
You can see that the storage method has been changed to ziplist
More official pressure Testing and guidance suggestions:
When the number of elements in a compressed list rises to several thousand (actual use may be far less than this value), the performance of the compressed list may decrease because Redis operates this When using this structure, there will be a certain amount of pressure on encoding and decoding.
The length of the compressed list is limited to 500-2000, and the size of each element is limited to 128 bytes or less. The performance of the compressed list will be within a reasonable range.
The above is the detailed content of Detailed introduction to Redis compression list (example explanation). For more information, please follow other related articles on the PHP Chinese website!

Redis是现在最热门的key-value数据库,Redis的最大特点是key-value存储所带来的简单和高性能;相较于MongoDB和Redis,晚一年发布的ES可能知名度要低一些,ES的特点是搜索,ES是围绕搜索设计的。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了关于redis的一些优势和特点,Redis 是一个开源的使用ANSI C语言编写、遵守 BSD 协议、支持网络、可基于内存、分布式存储数据库,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis Cluster集群收缩主从节点的相关问题,包括了Cluster集群收缩概念、将6390主节点从集群中收缩、验证数据迁移过程是否导致数据异常等,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis实现排行榜及相同积分按时间排序,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了关于原子操作中命令原子性的相关问题,包括了处理并发的方案、编程模型、多IO线程以及单命令的相关内容,下面一起看一下,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了bitmap问题,Redis 为我们提供了位图这一数据结构,位图数据结构其实并不是一个全新的玩意,我们可以简单的认为就是个数组,只是里面的内容只能为0或1而已,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis实现排行榜及相同积分按时间排序,本文通过实例代码给大家介绍的非常详细,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了关于实现秒杀的相关内容,包括了秒杀逻辑、存在的链接超时、超卖和库存遗留的问题,下面一起来看一下,希望对大家有帮助。


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

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.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
