What is a Redis database?

Source: Internet
Author: User
Tags key string memcached

The first project of the new company was made with Redis. Have not contacted before, so from the Internet to find some articles, learn to understand the original link: http://baike.so.com/doc/5063975-5291322.html

What is Redis?

Redis is an open source API that is written in ANSI C, supports the web, can be persisted in memory, key-value databases , and provides multiple languages. From March 15, 2010 onwards, the development work of Redis is hosted by VMware .

Redis Company

Redis is a key-value storage System .

Similar to Memcached , it supports storing more value types , including string (String), list (linked list), set (set), and Zset (ordered collection).

These data types support Push/pop, Add/remove, and intersection-set and difference sets, and richer operations, and these operations are atomic . Based on this, Redis supports sorting in a variety of different ways .

As with memcached, data is cached in memory to ensure efficiency . The difference is that Redis periodically writes the updated data to disk or writes the modified operation to the appended record file , and master-slave (Master-Slave) synchronization is implemented on this basis.

Redis is a high-performance Key-value database . The emergence of Redis, to a large extent, compensates for the lack of memcached such keyvalue storage, in some cases can be a good complement to the relational database. It provides a python,ruby,erlang,php client, which is easy to use

REDIS Data Structures

Redis currently offers four types of data: String,list,set and Zset (sorted set).

String (String)

String is the simplest type, you can understand the same type as memcached, a key corresponds to a value, and the operation supported on it is similar to the operation of Memcached. But it's more versatile.

Redis uses structure SDSHDR and SDS to encapsulate strings , and string-related operations are implemented in the source file sds.h/sds.c . The SDSHDR data structure is defined as follows:

  typedef char  *sds; Tepedef char* SDS;    3    Sdshdr {  4   5  long   Len;    7  long  free     9  char   buf[];  10  11 }; 

List (doubly linked list)

List is a linked list structure, the main function is push, pop, get all the values of a range and so on. Key in operation is understood as the name of the linked list .

The definition and implementation of list in the source file adlist.h/adlist.c, the relevant data structure is defined as follows:

 1  //  2  3  typedef struct   Listiter { 4  5  listnode *next;//point to next node  6  Span style= "color: #008080;"  >7  int   direction; Direction  8  9 } listiter; 

 

// list Data Structure  struct**tail; void * (*dup) (void *ptr); void (*free) (void *ptr); int (*match) (voidvoid *int  len;listiter iter;} list;

Dict (hash table)

Set is a set, and we are similar to the concept of the set in mathematics, the operation of the set has added delete elements, there are multiple sets of orthogonal and poor operation . In Operation Key is understood as the name of the collection .

The Hashtable operation is implemented in the source file dict.h/dict.c, and the data structure is defined as follows:

// element items in the Dict  struct  dictentry {void *key; void *val; struct dictentry *next;} dictentry;

//dict Related configuration functionstypedefstructDicttype {unsignedint(*hashfunction)(Const void*key);void*(*Keydup)(void*privdata,Const void*key);void*(*Valdup)(void*privdata,Const void*obj);int(*Keycompare)(void*privdata,Const void*key1,
Const void*key2);void(*Keydestructor)(void*privdata,void*key);void(*Valdestructor)(void*privdata,void*obj);} Dicttype;

 //  Dict definition  typedef  struct   dict {dictentry  * *table;dicttype  *long   size;unsigned  long   sizemask;unsigned  long   used;  void  *privdata;} dict;  

 

// dict iterators  struct*ht; int  *entry, *nextentry;} dictiterator;

The table in Dict is an array of dictentry pointers , and each member in the array is a one-way list with the same element as the hash value . Set is implemented on the basis of dict, specifying that the comparison function of key is Dictencobjkeycompare, and if key is equal, it is no longer inserted.

Zset (sort set)

Zset is an upgraded version of Set , and he adds a sequential attribute on the set, which can be specified when adding a modified element, and Zset automatically re-adjusts the order of the new values after each assignment. It can be understood that there are two columns of MySQL table, one column of value, and one in the order of storage . Key in operation is understood as the name of Zset.

typedef struct Zskiplistnode {

struct Zskiplistnode **forward;

struct Zskiplistnode *backward;

Double score;

RobJ *obj;

} Zskiplistnode;

typedef struct zskiplist {

struct Zskiplistnode *header, *tail;

unsigned long length;

int level;

} zskiplist;

typedef struct Zset {

Dict *dict;

Zskiplist *ZSL;

} Zset;

Zset uses dict to maintain the mapping of key--value, and to save the ordered relationship of value with ZSL (zskiplist) . ZSL is actually an unstable multi-fork tree, and the elements from the root node to the leaf nodes in each chain remain in ascending order.

The Redis command

Redis is simply using memory storage , the key to persistence is these three instructions: SAVE BGSAVE lastsave ...

When the Save command is received, Redis dumps the data into a file .

What is worth saying is its exclusive function: storing lists and collections , which is a more competitive place than MC.

does not introduce the MC inside already has the east, only lists the special:

Type key -Used to get the types of a key

keys pattern -matches all key patterns, such as keys, to list all keys, of course, the complexity O (n)

Randomkey -Returns a random key

RENAME oldkey newkey -key can also be renamed

List operation, Essence

Rpush Key String -Adds a value to the head of a key list

Lpush Key String -Adds a value to the end of a key list

llen Key -list length

lrange key Start end -Returns the value of a range in the list, which is equivalent to a paging query inside MySQL

LTRIM key Start end -preserves only a range of values in the list

LINDEX Key index-Gets the value of a specific index number in the list, be aware of the O (n) complexity

LSET Key Index value-sets the values for a position in the list

Lpop Key

rpop Key -like the above Lpop, is a stack or a queue of the kind of head-tail instructions, can be used as Message Queuing to use the

Collection operations

Sadd Key member -add Element

Srem Key member -delete element

scard Key -Returns the collection size

sismember Key member -Determines whether a value is in the collection

SINTER key1 key2 ... keyN -Gets the intersection element of multiple collections

smembers Key -Lists all elements of the collection

There are also multiple DB commands that can be replaced with DB, data can be isolated, and the default is stored in DB 0.

File description for Redis

Redis uses two file formats: full- volume data and incremental requests . The full data format is to write the in-memory data to disk , so that the next time to read the file to load; The incremental request file is to serialize the in-memory data to the operation request, to read the file for replay to get the data , the serialized operation includes set, Rpush, Sadd, Zadd.

The Redis storage is divided into memory storage, disk storage, and log files , which are configured with three parameters in the configuration file .

The Save seconds Updates,save configuration, indicating how many times the update operation is synchronized to the data file. This can be a combination of multiple conditions, such as the default configuration file settings, set three conditions.

AppendOnly yes/no, appendonly configuration, indicates whether logging occurs after each update operation and, if not turned on, may result in data loss over a period of time when power is lost . Because the Redis itself synchronizes data files in sync with the save conditions above, some data will only exist in memory for a period of time.

Appendfsync no/always/everysec, Appendfsync configuration, no indicates that the data cache of the operating system is synchronized to disk , always indicates that Fsync () is manually invoked after each update operation Writes data to disk , everysec indicates synchronization once per second .

What is a Redis database?

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.