Redis RDB File Structure

Source: Internet
Author: User
Tags constant hash redis

The Redis rdb file holds binary data, and the structure consists of 5 parts:

REDIS | db_version | Databases | EOF | Check_sum


The db_version length is 4 bytes, and its value is a string representing the integer that records the version number of the Rdb file.


Databases records the database instance, and the key-value pair data for each DB instance, and if all the db in the Redis-server is empty, the value is also empty, and the length is 0 bytes.


The EOF constant length is 1 bytes, marking the end of the Rdb file body content.


The Check_sum is a 8-byte long unsigned integer that holds a checksum that is calculated by the program through four portions of Redis, db_version, databases, and EOF. When you load an RDB file, Redis checks the load data and compares it to the value to check for errors or corruption in the Rdb file.


1.databases

Each non-empty database in the Rdb file is saved as Selectdb, Db_number, key_value_pairs three parts.

Selectdb | Db_number | Key_value_pairs

The Selectdb constant length is 1 bytes, which identifies the subsequent database number.

Db_number saves the database number, depending on the size of the number, 1 bytes, 2 bytes, or 5 bytes in length. When the program reads into the Db_number section, the server invokes the Select command, which switches the database based on the database number read in.

Key_value_pairs saves all key-value pairs in the database, and if the key-value pair has an expiration time, the expiration time is also saved with the key-value pair.


2.key_value_pairs

Key-value pair structure with no expiration time

TYPE | Key | Value

Key-value pair structure with expiration time

Expiretime_ms | Ms | TYPE | Key | Value

Where the type record is of value and is any of the following constants:

Redis_rdb_type_string

Redis_rdb_type_list

Redis_rdb_type_set

Redis_rdb_type_zset

Redis_rdb_type_hash

Redis_rdb_type_list_ziplist

Redis_rdb_type_set_insert

Redis_rdb_type_zset_ziplist

Redis_rdb_type_hash_ziplist

The key is always a string object, encoded in the same way as the value of redis_rdb_type_string type.

Value differs in structure and length depending on type.

The Expiretime_ms constant length is 1 bytes, and when the program reads into the constant, it indicates that the next read-in will be an expiration in milliseconds.

MS is a 8-byte long signed integer that records a Unix timestamp in milliseconds, which is the expiration time.


3.value encoding

A. String Object

Type is redis_rdb_type_string, the string object encoding can be redis_encoding_int or redis_encoding_raw, corresponding to the saved value type is an integer and a string.

If the string object is encoded as Redis_encoding_raw, and if the string length is greater than 20 bytes, the string will be compressed and saved otherwise.

No compressed string structure

Len | String

Post-compression structure

Redis_rdb_enc_lzf | Compressed_len | Origin_len | Compressed_string

Where the REDIS_RDB_ENC_LZF constant flag string has been compressed by the LZF algorithm, the program during the reading process, when encountering this constant, will be based on the subsequent Compressed_len and Orgin_len and compressed_string Three parts of the string to extract.


B. List objects

The TYPE for Redis_rdb_type_list,value is a redis_encoding_linkedlist encoded list object with the following structure

List_length | item1| item2 | Item3 | ... | Itemn

List_length records the length of the list, the item section represents the list of items, and each item is a string object.


C. Collection objects

The TYPE for Redis_rdb_type_set,value is a collection of REDIS_ENCODING_HT encoded objects, structured as follows

Set_size | Elem1 | elem2 | Elem3 | ... | Elemn

Set_size records the collection size, Elem The beginning part represents the collection of elements, each element is a string object.


D. hash Table Object

The TYPE for Redis_rdb_type_hash,value is a REDIS_ENCODING_HT encoded collection object with the following structure

Hash_size | Key_value_pair 1 | Key_value_pair 2 | Key_value_pair 3 | ... | Key_value_pair N

Hash_size records the size of the hash table. The Key_value_pair section represents a key-value pair, and each key-value pair in the structure is arranged in the same way that the key is pressed next to the value. The structure is as follows

Key1 | value1 | Key2 | value2 | Key3 | Value3 | ....


E. Ordered collection objects

The TYPE for Redis_rdb_type_zset,value holds an ordered collection object that is redis_encoding_skiplist encoded. The structure is as follows

Sorted_set_size | Elem1 | elem2 | Elem3 | ... | Elemn

Sorted_set_size records the ordered collection size, Elem The first part represents the elements in the ordered set, each element is divided into members (member) and the score (score) two parts, the member is a string object, the score is a double type of floating-point number, When you save an RDB file, the program converts the branch to a string object, and then saves the score with the method of saving the string.

Each element in an ordered set is arranged in the same way as a member, with the following structure

Sorted_set_size | Member1 | Score 1 | Member2 | Score2 | ... | Membern | Scoren


F.intset Encoding Collection

TYPE for Redis_rdb_type_set_intset,value is an integer collection object, and the RDB saves this object method by first converting an integer collection to a String object and then saving it to an RDB file. Read is the opposite.


G.ziplist encoded list, hash table, or ordered collection

TYPE is Redis_rdb_type_list_ziplist, redis_rdb_type_hash_ziplist, or redis_rdb_type_zset_ziplist, then value holds a compressed list object, The Rdb file is saved by:

1) Convert the compression list to a string object;

2) Save the converted string object to an Rdb file.

The corresponding type conversion can be done while reading.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.