In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
1. Redis is most suitable for all data in-momory scenarios. Although Redis also provides persistence, it is actually more of a disk-backed function, which is quite different from the traditional persistence, so you may have doubts. It seems that Redis is more like an enhanced version of Memcached, so when to use Memcached and when to use Redis?
If you simply compare the difference between Redis and Memcached, most will get the following point of view:
1. Redis not only supports simple KBH data, but also provides storage of data structures such as list,set,zset,hash.
2. Redis supports data backup, that is, data backup in master-slave mode.
3. Redis supports data persistence. You can keep the data in memory on disk, and you can load it again when you restart it.
2. Redis common data types
The most commonly used data types in Redis are as follows:
String
Hash
List
Set
Sorted set
Pub/sub
Transactions
Before describing these data types in detail, let's take a look at how these different data types are described in Redis internal memory management:
First of all, a redisObject object is used inside Redis to represent the most important information of all key and value,redisObject, as shown in the figure above:
Type represents a value object. What data type is it?
Encoding is how different data types are stored within redis.
For example, type=string represents value to store an ordinary string, then the corresponding encoding can be raw or int, and if int represents the actual redis, the string is stored and represented as a numeric class internally, of course, provided that the string itself can be represented by a numeric value, such as a string like "123,456".
The vm field needs to be specified here. Only when the virtual memory feature of Redis is enabled, this field will actually allocate memory. This function is disabled by default, which will be described later. Through the figure above, we can find that it is a waste of memory for Redis to use redisObject to represent all key/value data. Of course, these memory management costs are mainly to provide a unified management interface for different data types of Redis. The actual author also provides a variety of methods to help us save memory as much as possible, which we will discuss in detail later.
3. Application and implementation of various data types
Let's first analyze the use and internal implementation of these seven data types one by one:
String:
Strings data structure is a simple key-value type, value is not only a String, but also a number.
Common commands: set,get,decr,incr,mget, etc.
Application scenario: String is the most commonly used data type, ordinary key/ value storage can be classified into this category. That is, it can fully realize the functions of the current Memcached, and it is more efficient. You can also enjoy the regular persistence of Redis, operation log and Replication and other functions. In addition to providing the same get, set, incr, decr, and so on operations as Memcached, Redis also provides the following operations:
Get string length
To the string append content
Set and get a segment of a string
Set and get a bit of a string (bit)
Set the contents of a series of strings in batch
Implementation: String is stored in redis as a string by default, which is referenced by redisObject. When it encounters operations such as incr,decr, it will be converted to numeric calculation. In this case, the encoding field of redisObject is int.
Hash
Common commands: hget,hset,hgetall, etc.
Application scenario: in Memcached, we often package some structured information into HashMap, which is stored as a string value after serialization on the client, such as the user's nickname, age, gender, score, and so on. When you need to modify one of these items, you usually need to deserialize all the values, modify the value of a certain item, and then serialize and store it back. This not only increases the overhead, but also does not apply to situations 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 attribute value as if you were Update an attribute in the database.
Let's give a simple example to describe the application scenario of Hash. For example, we want to store a user information object data that contains the following information:
The user ID is the found key, and the stored value user object contains name, age, birthday and other information. If you use the normal key/value structure to store it, there are two main storage methods:
The first method takes the user ID as a lookup key and encapsulates other information into an object to store in a serialized way. the disadvantage of this method is that it increases the cost of serialization / deserialization, and when one of the information needs to be modified, the whole object needs to be retrieved, and the modification operation needs to protect concurrency and introduce complex problems such as CAS.
The second method is to save as many key-value pairs as the number of members of the user information object, and use the name of the attribute corresponding to the user ID+ as the unique identification to get the value of the corresponding attribute. Although the serialization overhead and concurrency problems are eliminated, the user ID is stored repeatedly. If there is a large amount of such data, the memory waste is still considerable.
Then the Hash provided by Redis solves this problem very well. The Hash of Redis is actually an internally stored Value as a HashMap, and provides an interface to directly access this Map member, as shown below:
In other words, Key is still the user ID, value is a Map, the key of this Map is the attribute name of the member, and value is the attribute value, so the data can be modified and accessed directly through the Key of the internal Map (the key of the internal Map is called field in the Redis), that is, the corresponding attribute data can be manipulated through key (user ID) + field (attribute tag). There is no need to store the data repeatedly. Nor does it cause problems with serialization and concurrency modification control. Solved the problem very well.
It should also be noted that Redis provides hgetall to fetch all attribute data directly, but if there are many members of the internal Map, then it involves traversing the entire internal Map operation. Due to the Redis single-threaded model, this traversal operation may be time-consuming, while other client requests are completely unresponsive, which requires special attention.
Implementation method:
As mentioned above, the Redis Hash corresponds to the internal Value is actually a HashMap. In fact, there are two different implementations. When the members of this Hash are relatively small, the Redis will use a compact storage method similar to an one-dimensional array in order to save memory, but will not use the real HashMap structure. The encoding of the corresponding value redisObject is zipmap. When the number of members increases, it will be automatically converted to the real HashMap, and the encoding will be ht.
List
Common commands: lpush,rpush,lpop,rpop,lrange, etc.
Application scenarios:
There are many application scenarios of Redis list, and it is also one of the most important data structures of Redis. For example, twitter's watch list, fan list and so on can be implemented by Redis's list structure.
Lists is a linked list, and anyone with a little knowledge of data structures should be able to understand its structure. Using the Lists structure, we can easily achieve functions such as ranking the latest messages. Another application of Lists is message queuing
You can take advantage of the PUSH operation of Lists to store the task in Lists, and then the worker thread uses the POP operation to fetch the task for execution. Redis also provides api for manipulating a certain segment of Lists. You can directly query and delete the elements of a certain segment of Lists.
Implementation method:
The implementation of Redis list is a two-way linked list, that is, it can support reverse search and traversal, which is more convenient to operate, but it brings some additional memory overhead. Many implementations within Redis, including send buffer queues, also use this data structure.
Set
Common commands:
Sadd,spop,smembers,sunion et al.
Application scenarios:
Redis set external function is similar to list is a list function, the special feature is that set can automatically arrange weight, when you need to store a list of data, but do not want to duplicate data, set is a good choice, and set provides an important interface to determine whether a member is in a set collection, which list can not provide.
The concept of a Sets set is a combination of unrepeated values. Using the Sets data structure provided by Redis, you can store some collective data. For example, in Weibo applications, you can store all the followers of a user in a collection and all their fans in a collection. Redis also provides intersection, union, difference and other operations for the collection, which is very convenient to achieve, such as common concern, common preferences, second-degree friends and other functions. For all the above collection operations, you can also use different commands to choose whether to return the results to the client or save to a new collection.
Implementation method:
The internal implementation of set is a HashMap in which value is always null. In fact, the weight is arranged quickly by calculating hash, which is why set can determine whether a member is in the collection or not.
Sorted Set
Common commands:
Zadd,zrange,zrem,zcard et al.
Use the scene:
The usage scenario of Redis sorted set is similar to that of set, except that set is not automatically ordered, while sorted set can sort members by providing an additional parameter of score, and it is inserted in order, that is, automatic sorting. When you need an ordered and non-repeating list of collections, you can choose the sorted set data structure. For example, twitter's public timeline can be stored as a score with the publication time, so that the acquisition is automatically sorted according to time.
In addition, Sorted Sets can be used to make weighted queues, such as the score of ordinary messages is 1 and the score of important messages is 2, and then the worker thread can choose to get work tasks in reverse order of score. Give priority to important tasks.
Implementation method:
Redis sorted set internal use HashMap and jump table (SkipList) to ensure data storage and order, HashMap is put in the member to score mapping, while the jump table is stored in all the members, sorting according to the score stored in HashMap, the use of jump table structure can achieve higher search efficiency, and relatively simple in implementation.
Pub/Sub
Pub/Sub literally means Publish and Subscribe. In Redis, you can set a key value to publish and subscribe to a message. When a key value publishes a message, all clients that subscribe to it will receive the corresponding message. The most obvious use of this function is as a real-time messaging system, such as ordinary real-time chat, group chat and other functions.
Transactions
Who said that NoSQL does not support transactions, although Redis's Transactions does not provide strict ACID transactions (such as a string of commands committed for execution with EXEC, and the server goes down during execution, then some of the commands will be executed and the rest will not be executed), but this Transactions still provides basic command packaging and execution (if there is no problem with the server). You can ensure that a series of commands are executed sequentially, with other client commands plugged in). Redis also provides a Watch function, you can Watch a key, and then execute Transactions, in the process, if the value of the Watched is modified, then the Transactions will discover and refuse to hold the line.
Go to: http://www.360doc.com/content/16/0719/16/35210735_576795164.shtml
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.