1. Redis is best suited for all data in-momory scenarios, although Redis also provides persistence, but actually more of a disk-backed function, compared with the traditional meaning of persistence there is a big difference, then you may have questions, It seems that Redis is more like an enhanced version of memcached, so when to use memcached, when to use Redis?
If you simply compare the differences between Redis and memcached, most of them will get the following ideas:
1, Redis not only supports simple k/v type of data, but also provides the storage of data structures such as List,set,zset,hash.
2, Redis support data backup, that is, Master-slave mode of data backup.
3, Redis support data persistence, you can keep the in-memory data on the disk, restart can be loaded again for use
2. Redis Common data types
The most commonly used data types for Redis are the following:
String
Hash
List
Set
Sorted Set
Pub/sub
Transactions
Before describing these types of data, let's look at a diagram of how these different data types are described in Redis internal memory management:
650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M00/84/5A/wKioL1eN3ZqRQ_XJAABX0wZRsO8034.jpg-wh_500x0-wm_3 -wmp_4-s_1481890713.jpg "title=" 51550047_1.jpg "alt=" Wkiol1en3zqrq_xjaabx0wzrso8034.jpg-wh_50 "/>
First, Redis internally uses a Redisobject object to represent all key and value,redisobject information, as shown in the following:
Type represents what data type a value object is,
Encoding is how different data types are stored inside the Redis,
For example: Type=string means that value is stored as a normal string, then the corresponding encoding can be raw or int, and if it is an int the actual redis is stored and represented by a numeric class, Of course, the premise is that the string itself can be expressed as a numeric value, such as: "123" "456" such a string.
Here you need to specify the VM field, only the virtual memory feature of Redis is turned on, this field will actually allocate memory, which is turned off by default, which is described later in this function. We can find that Redis uses Redisobject to indicate that all key/value data is a waste of memory, and of course, the cost of memory management is mainly to provide a unified management interface for different data types of Redis. The actual author also offers several ways to help us save memory as much as possible, which we'll discuss in detail later.
3. Various data type application and implementation methods
Let's start with the analysis of the use of these 7 types of data and how to implement them internally:
strings data structure is a simple key-value type, and value is not only a string, it can also be a number.
common commands: SET,GET,DECR, Incr,mget and so on.
scenario: string is the most common type of data, and normal Key/value storage can be categorized as such. can fully implement the current Memcached, and is more efficient. You can also enjoy Redis's timed persistence, operation logs, and replication functions. In addition to providing operations like get, set, INCR, DECR, and so on Memcached, Redis provides the following actions:
-
Get string length
Append content to a string
Set and get a section of a string
Set and get one of the strings (bit)
Bulk set the contents of a series of strings
Implementation method: String in the Redis internal storage By default is a string, referenced by Redisobject, when encountered INCR,DECR and other operations will be converted to a numeric type for calculation, at this time Redisobject encoding field is an int.
Common commands:Hget,hset,hgetall and so on.
Application Scenarios: In memcached, we often package structured information into HashMap, which is stored as a string value after the client is serialized, such as the user's nickname, age, gender, integral, and so on, when it is necessary to modify one of these items, it is usually necessary to remove all values after deserialization, Modify the value of an item, and then serialize the store back. This not only increases the overhead, but also does not apply to some scenarios where concurrent operations are possible (for example, two concurrent operations need to modify the integral). The hash structure of Redis allows you to modify only one item property value just as you would update a property in a database.
Let's simply cite an example to describe the application scenario for a hash, such as storing a user information object data that contains the following information:
The user ID is the key to find, the stored value user object contains the name, age, birthday and other information, if the ordinary key/value structure to store, mainly has the following 2 kinds of storage methods:
650) this.width=650; "style=" WIDTH:315PX;HEIGHT:197PX; "src=" http://s4.51cto.com/wyfs02/M00/84/5A/ Wkiom1en4bojnuitaaamzkdzfzm826.jpg-wh_500x0-wm_3-wmp_4-s_4063938342.jpg "title=" 51550047_2.jpg "border=" 0 "Height = "197" hspace= "0" vspace= "0" width= "315" alt= "wkiom1en4bojnuitaaamzkdzfzm826.jpg-wh_50"/>
The disadvantage of using the user ID as a lookup key to encapsulate other information as a serialized object is to increase the cost of serialization/deserialization and to retrieve the entire object when one of the information needs to be modified, and the modification operation requires concurrency protection. Introduce complex problems such as CAs.
650) this.width=650; "src=" Http://image83.360doc.com/DownloadImg/2015/03/2416/51550047_3 "style=" margin:0px; padding:0px;border:0px; "/>
The second method is how many members of this user information object will be saved into the number of key-value, with the user id+ the name of the corresponding property as a unique identifier to obtain the value of the corresponding property, although the cost of serialization and concurrency is omitted, but the user ID is repeated storage, if there is a large number of such data, The memory waste is still very considerable.
So the hash provided by Redis is a good solution to this problem, and the Redis hash is actually the internal stored value as a hashmap, and provides a direct access to the map member's interface, such as:
650) this.width=650; "src=" Http://image83.360doc.com/DownloadImg/2015/03/2416/51550047_4 "style=" margin:0px; padding:0px;border:0px; "/>
That is, the key is still the user ID, value is a map, the map key is a member of the property name, value is the property value, so that the data can be modified and accessed directly through its internal map key (Redis called internal map key field), This means that the corresponding attribute data can be manipulated by key (user ID) + field (attribute tag), without the need to store the data repeatedly and without the problem of serialization and concurrency modification control. A good solution to the problem.
It is also important to note that Redis provides an interface (Hgetall) that can fetch all of the property data directly, but if the internal map has a large number of members, it involves traversing the entire internal map, which can be time-consuming due to the Redis single-threaded model. The other client requests are not responding at all, which requires extra attention.
Implementation method:
The above has been said that the Redis hash corresponds to value inside the actual is a hashmap, actually there will be 2 different implementations, this hash of the members of the relatively small redis in order to save memory will be similar to a one-dimensional array to compact storage, without the use of a real HASHMAP structure , the encoding of the corresponding value Redisobject is Zipmap, and when the number of members increases, it automatically turns into a true hashmap, at which time encoding is HT.
Common commands:lpush,rpush,lpop,rpop,lrange and so on.
Application Scenarios:
Redis list has a lot of applications and is one of the most important data structures of redis, such as Twitter watchlist, fan list, etc. can be implemented using Redis's list structure.
Lists are linked lists, and people who believe that they have a knowledge of data structures should be able to understand their structure. With the lists structure, we can easily achieve the latest message ranking and other functions. Another application of lists is Message Queuing,
The lists push operation can be used to present the task in lists, and then the worker thread then takes the task out of execution with the pop operation. Redis also provides an API for manipulating a section of lists, where you can directly query and delete elements from a section of lists.
Implementation method:
The implementation of Redis list is a doubly linked list, which can support reverse lookup and traversal, but it is more convenient to operate, but it brings some additional memory overhead, and many implementations within Redis, including sending buffer queues, are also used in this data structure.
Common commands:
Sadd,spop,smembers,sunion and so on.
Application Scenarios:
The functionality provided by Redis set externally is a list-like feature, except that set is automatically weight-saving, and set is a good choice when you need to store a list of data and you don't want duplicate data. and set provides an important interface to determine whether a member is within a set set, which is not available in list.
The concept of a sets collection is a combination of a bunch of distinct values. Using the sets data structure provided by Redis, you can store some aggregated information, such as in a microblog application, where you can have a collection of all the followers of a user and a collection of all their fans. Redis also provides for the collection of intersection, set, difference sets and other operations, can be very convenient to achieve such as common concern, common preferences, two-degree friends and other functions, to all of the above collection operations, you can also use different commands to choose whether to return the results to the client or save set into a new collection.
Implementation method:
The internal implementation of set is a value that is always null hashmap, which is actually calculated by hashing the way to fast weight, which is also set to provide a judge whether a member is within the cause of the collection.
Common commands:
Zadd,zrange,zrem,zcard, etc.
Usage scenarios:
The usage scenario for Redis sorted set is similar to set, except that the set is not automatically ordered, and the sorted set can be ordered by the user with an additional priority (score) parameter, and is inserted in an orderly, automatic sort. When you need an ordered and non-repeating collection list, you can choose sorted set data structures, such as the public Timeline of Twitter, which can be stored as score in the publication time, which is automatically sorted by time.
Also can use sorted sets to do with the weight of the queue, such as the normal message score is 1, the important message of the score is 2, and then the worker can choose to press score reverse order to get work tasks. Let important tasks take precedence.
Implementation method:
Redis sorted set internal use HashMap and jump Table (skiplist) to ensure the storage and ordering of data, HashMap in the member to score mapping, and the jumping table is all the members, sorted by HashMap in the score , the use of the structure of the jumping table can obtain a relatively high efficiency of finding, and it is relatively simple to implement.
Pub/sub literally is the release (Publish) and Subscription (Subscribe), in Redis, you can set a key value for message publishing and message subscription, when a key value on a message published, all subscribed to its client will receive the corresponding message. The most obvious use of this function is to use it as a real-time messaging system, such as regular live chat, group chat, and other functions.
Who says NoSQL does not support transactions, although Redis's transactions provides not strictly acid transactions (such as a string of commands executed with Exec execution, in the execution of the server down, then there will be a part of the command execution, the rest is not executed), However, this transactions provides the basic command package execution function (in case the server does not have a problem, you can ensure that a series of commands are executed together in sequence, there will be other client commands inserted to execute). Redis also provides a watch function, you can watch a key, and then execute transactions, in the process, if the value of this watched is modified, then this transactions will find and refuse to execute.
Go to: http://www.360doc.com/content/16/0719/16/35210735_576795164.shtml
This article is from "Yun Weibang" blog, please be sure to keep this source http://aklaus.blog.51cto.com/9724632/1827801
Five types of Redis introduction