Actual algorithm (a): Analysis Redis common data corresponding to the type of data structure

Actual algorithm (a): Analysis Redis common data corresponding to the type of data structure

Common data types in the database Redis, what are the underlying data structures to achieve?

Redis database Introduction

Redis is a key (Key-Value) database, non-relational database, with respect to the structure of a relational database such as MySQL, MySQL tables is more complex, it contains many fields, SQL statements can achieve the very complexity of the query requirements, Redis It contains only "key", "value" in two parts, and only to look up values ​​by key, so reading and writing efficiency is very high Redis

Redis is mainly used as an in-memory database that the data is in memory, also supports the stored data is stored in the hard disk

Redis, the data type of the key is a string, in order to enrich the data storage mode, a lot of data type values, strings, lists, dictionaries, set, ordered set

List list

Storing a list of supported data set, corresponding to the list of two methods, one is compressed list ziplist, the other is a two-way circular linked list

When a small amount of data stored in the list when the list is by way of compression, but need to meet two conditions: a single data list stored less than 64 bytes, the number list data is less than 512. Compression is a data storage structure list Redis own design, similar to the array, data is stored by a contiguous memory space, allowing different sizes of data storage. Packing List save memory, and support different types of data storage, but also because the data is stored in a continuous memory space to get through key high value list type of data efficiency

When a large amount of data stored in the list, the need is achieved by way circular linked list, a list structure is additionally defined to organize the first list, the tail pointer, and the length information

typedef struct listnode{
	struct listNode *prev;
	struct listNode *next;
	void *value;
}listNode;

typedef struct list{
	listNode *head;
	listNode *tail;
	unsigned long len;
	//……
}list;

Dictionary (hash)

A dictionary for storing a set of data pairs, each data pair comprising a key and two, there are two types of dictionaries implementations, one second is compressed list hash table.

When a small amount of data stored when compression Redis used only to implement a dictionary listing, also need to meet two conditions: a dictionary storage key value and one size smaller than 64 bytes, the second is the key for the dictionary the number is less than 512, or we need to use a hash table to implement a dictionary, Redis hash algorithm using MurmurHash2 as a hash function for hash conflict, Redis using the list method to solve, but also supports dynamic expansion of the hash table , volume reduction

When the dynamic data increases, the load factor of the hash table increases, when the load factor is greater than 1, the Redis trigger expansion, expand the hash table is 2 times the original size, after the reduction of the data, when the load factor is less than 0.1, Redis trigger volume reduction, reduced to 2 times the size of the number of dictionary data. Use progressive expansion of volume reduction strategy, will move data in batches to avoid a one-time move large amounts of data services stop due

Collection (set)

For storing a set of unique data, data type has two methods, one is based on an ordered array, the second is based on a hash table, when the data to be stored in both of the following conditions when, on the Redis an orderly arrays to implement a set of data types, or Redis data on the use of hash table to store the collection

  • The stored data are integers
  • The number of data elements stored in no more than 512

Ordered set sortedset

For storing a set of ordered set of data, and each data is provided with a score, the score by the size of the data organized into a jump table, to support rapid accordance scores, the score data acquisition interval

When a small amount of data when, Redis will use compression to achieve an ordered set list, use the list to achieve compression premise has ordered collection

  • All the data size is less than 64 bytes
  • The number of elements is less than 128

Persistent data structures

Redis is treated as in-memory database, but also supports data off the disk, is about memory data storage to the hard disk, if the power failure when the data will not be lost after the restart, Redis just then stored in the hard disk data re-read you can continue to take the work memory, the Redis data format from the "key" and "value" of two parts, and the value support many data types such as strings, lists, sets, ordered set. Like the type of dictionaries, collections, etc., used in the underlying hash table, hash table the concept of a pointer that points to a memory address in memory, Redis how to store a memory address associated with the specific data structures to disk?

Persistent problem known data structure or object persistent problems, persistent storage to disk =

How the data structures persistent disk?

First, remove the existing storage structure, only the data stored to disk, when we need to restore data from disk when memory is re-organizing the data into the original data structure, Redis is used in this idea, but the data restore the hard disk to memory from the process, consume more time

Second, it retains the original storage format, according to the data stored in the original format on the disk. We can hash size of the list, information for each data to be hashed slot number, etc., are stored on disk, with these information to avoid re-compute the hash value

Published 75 original articles · won praise 9 · views 9163

Guess you like

Origin blog.csdn.net/ywangjiyl/article/details/104893053