Application scenarios of several Redis data structures and redis data structure scenarios

Source: Internet
Author: User

Application scenarios of several Redis data structures and redis data structure scenarios
String[Html]View plaincopy

  1. 1. String
  2. Common commands:
  3. In addition to get, set, incr, decr mget, and other operations, Redis also provides the following operations:
  4. Returns the string length.
  5. Append content to string
  6. Set and obtain a part of a string
  7. Set and obtain a bit of a string)
  8. Batch set the content of a series of strings
  9. Application scenarios:
  10. String is the most common data type. Common key/value storage can be classified as this type. value is not only String,
  11. It can also be a number: for example, you want to know when to block an IP address (more than a few times ). The INCRBY Command makes it easy to maintain the count by increasing the number of atoms.
  12. Implementation Method:
  13. M, decr, and other operations are converted to numeric values for calculation. In this case, the encoding field of redisObject is int.

Hash [Html]View plaincopy
  1. Common commands:
  2. Hget, hset, hgetall, etc.
  3. Application scenarios:
  4. Let's take an example to describe the Hash Application Scenario. For example, we want to store a user information object data that contains the following information:
  5. User ID, which is the search key,
  6. The stored value user object contains information such as name, age, and birthday,
  7. If the common key/value structure is used for storage, there are two main storage methods:
  8. The first method is to use the user ID as the search key, and encapsulate other information into an object for storage in serialized mode,
  9. For example: set u001 "Li San, 18,20010101"
  10. The disadvantage of this method is that it increases the serialization/deserialization overhead, and the entire object needs to be retrieved when one of the information needs to be modified, and the modification operation needs to protect concurrency, introduce CAS and other complex problems.
  11. The second method is to store the key-value pair as many members of the user information object, and use the user ID + name of the corresponding attribute as a unique identifier to obtain the value of the corresponding attribute,
  12. For example, mset user: 001: name "Li San" user: 001: age18 user: 001: birthday "20010101"
  13. Although serialization overhead and concurrency problems are saved, the user ID is retained. If there is a large amount of such data, the memory waste is still considerable.
  14. The Hash provided by Redis can solve this problem very well. Redis's Hash is actually a HashMap of internally stored values,
  15. And provides an interface to directly access this Map member,
  16. For example: hmset user: 001 name "Li San" age 18 birthday "20010101"
  17. That is to say, the Key is still the user ID, the value is a Map, the key of the Map is the property name of the member, and the value is the property value,
  18. In this way, you can directly modify and access data through the internal Map Key (the internal Map key in Redis is called field), that is, through
  19. Key (User ID) + field (attribute tag) operations correspond to the attribute data, which neither need to store data repeatedly nor bring about serialization and concurrent modification control problems. Solved the problem.
  20. At the same time, it should be noted that Redis provides an interface (hgetall) to directly retrieve all attribute data. However, if there are many internal Map members, it involves traversing the entire internal Map operation, because of the Redis single-threaded model, this traversal operation may be time-consuming, and requests from other clients do not respond at all. This requires special attention.
  21. Implementation Method:
  22. As mentioned above, the Value corresponding to Redis Hash is actually a HashMap. There are two different implementations here, when there are few Members of this Hash, Redis will adopt a compact storage like a one-dimensional array to save memory, instead of a real HashMap structure. The corresponding value redisObject's encoding is zipmap, when the number of members increases, it is automatically converted to a real HashMap. In this case, encoding is ht.

List [Html]View plaincopy
  1. Common commands:
  2. Lpush, rpush, lpop, rpop, lrange, and BLPOP.
  3. Application scenarios:
  4. Redis list has many application scenarios and is also one of the most important data structures of Redis.
  5. We can easily implement the latest message ranking and other functions.
  6. Another application of Lists is message queue. You can use the PUSH operation of Lists to store the task in Lists, and then use the POP operation to retrieve and execute the task.
  7. Implementation Method:
  8. The implementation of Redis list is a two-way linked list, that is, it supports reverse lookup and traversal to facilitate operations. However, it brings some additional memory overhead and many internal implementations of Redis, this data structure is also used, including the sending Buffer Queue.
  9. RPOPLPUSH source destination
  10. The command RPOPLPUSH performs the following two actions within an atomic time:
  11. Bring up the last element (tail element) in the list source and return it to the client.
  12. Insert the elements popped up by source to the destination list as the Header element of the destination list.
  13. If source and destination are the same, the end element of the table in the list is moved to the header and the element is returned. In this case, the rotation operation of the list is considered.
  14. A typical example is the Monitoring Program of the server: they need to check a group of websites in parallel for as short as possible to ensure their accessibility.
  15. Redis. lpush "downstream_ips", "192.168.0.10"
  16. Redis. lpush "downstream_ips", "192.168.0.11"
  17. Redis. lpush "downstream_ips", "192.168.0.12"
  18. Redis. lpush "downstream_ips", "192.168.0.13"
  19. Then:
  20. Next_ip = redis. rpoplpush "downstream_ips", "downstream_ips"
  21. BLPOP
  22. Assume that there are three lists: job, command, and request, where job does not exist. Both command and request hold a non-empty list. Consider the following command:
  23. BLPOP job command request 30 # blocking for 30 seconds. If the value is 0, it is blocked for an indefinite period. If the job list is empty, it is skipped, and the first element in the command list is popped up.
  24. 1) "command" # A list of elements is displayed.
  25. 2) "update system..." # the value of the pop-up element
  26. Why is pop blocked? It is mainly used to avoid polling. A simple example is to use list to implement a work queue. The thread that executes the task can call the pop of the blocked version to obtain the task. This avoids polling to check whether a task exists. When a task comes, the worker thread can return immediately or avoid the delay caused by polling.

Set [Html]View plaincopy
  1. 4. Set
  2. Common commands:
  3. Sadd, srem, spop, sdiff, smembers, and sunion.
  4. Application scenarios:
  5. The functions provided by Redis set are similar to those provided by list. The special feature is that set can automatically record duplicates. When you need to store a list of data, if you do not want duplicate data, set is a good choice, and set provides an important interface to determine whether a member is in a set. This is also not provided by list.
  6. For example, in a Weibo application, each person's friends exist in a set. In this way, you may only need to use the intersection command to seek mutual friends.
  7. Redis also provides operations such as intersection, union, and difference set for the set, which can be very convenient to implement
  8. Implementation Method:
  9. The internal implementation of set is a HashMap whose value is always null. Actually, it is to calculate the hash method to quickly remove duplicates, this is also why set can determine whether a member is in the set.


Sort Set [Html]View plaincopy
  1. 5. Sorted set
  2. Common commands:
  3. Zadd, zrange, zrem, zcard, etc.
  4. Use Cases:
  5. Take a condition as the weight, such as sorting by the number of top times.
  6. The ZREVRANGE command can be used to obtain the top 100 users by score. ZRANK can be used to obtain the User ranking, which is very direct and easy to operate.
  7. The usage scenario of Redis sorted set is similar to set. The difference is that set is not automatically ordered, while sorted set can sort members by providing an additional priority (score) parameter, it is insert-ordered, that is, automatic sorting.
  8. For example, the public timeline of twitter can be stored as the score based on the posting time, which is automatically sorted by time.
  9. For example, the SortedSets score of the class can be the student ID, and the score can be the score of the test. In this way, the data inserted into the set has been naturally sorted.
  10. In addition, Sorted Sets can be used for weighted queues. For example, the score of a common message is 1 and that of an important message is 2, then, the worker thread can obtain the task in descending order of score. Give priority to important tasks.
  11. Applications requiring precise expiration time setting
  12. For example, you can set the score value of the sorted set as the timestamp of the expiration time, so you can simply sort the expiration time and regularly clear the expired data, in addition to clearing expired data in Redis, You can regard the expiration time in Redis as an index of the data in the database, and use Redis to find out which data needs to be deleted after expiration, then, the corresponding records are precisely deleted from the database.
  13. Implementation Method:
  14. In Redis sorted set, HashMap and SkipList are used internally to ensure data storage and order. In HashMap, Members are mapped to scores, the hop table stores all the members, and the sorting is based on the score saved in HashMap. using the structure of the hop table, you can get a high search efficiency and the implementation is relatively simple.


Message subscription [Html]View plaincopy
  1. 6. Pub/Sub
  2. Pub/Sub is literally Publish and Subscribe. In Redis, you can set a key value for message publishing and message subscription,
  3. After a message is published on a key value, all clients that subscribe to it will receive the corresponding message. The most obvious usage of this function is to use it as a real-time messaging system, such as Common Instant chat and group chat functions.
  4. Client 1: subscribe rain
  5. Client 2: PUBLISH rain "my love !!! "
  6. (Integer) 2 indicates that several clients have subscribed to the message.

Transaction [Html]View plaincopy
  1. 7. Transactions
  2. Who said NoSQL does not support Transactions, although Redis's Transactions does not provide strict ACID Transactions (for example, if a string of commands submitted and executed using EXEC, the server goes down during execution, some commands are executed, but the rest are not executed. However, Transactions provides the basic command packaging and execution function (when the server is normal, it can be ensured that a series of commands are executed in sequence, and other client commands are inserted in the middle for execution ).
  3. Redis also provides a Watch function. You can Watch a key and then execute Transactions. During this process, if the value of Watched is modified, then this Transactions will find and refuse to execute.
  4. Session 1
  5. (1) Step 2
  6. Redis 127.0.0.1: 6379> get age
  7. "10"
  8. Redis 127.0.0.1: 6379> watch age
  9. OK
  10. Redis 127.0.0.1: 6379> multi
  11. OK
  12. Redis 127.0.0.1: 6379>
  13. Session 2
  14. (2) Step 2
  15. Redis 127.0.0.1: 6379> set age 30
  16. OK
  17. Redis 127.0.0.1: 6379> get age
  18. "30"
  19. Redis 127.0.0.1: 6379>
  20. Session 1
  21. (3) Step 2
  22. Redis 127.0.0.1: 6379> set age 20
  23. QUEUED
  24. Redis 127.0.0.1: 6379> exec
  25. (Nil)
  26. Redis 127.0.0.1: 6379> get age
  27. "30"
  28. Redis 127.0.0.1: 6379>
  29. Step 1: Session 1 has not had time to modify the age value.
  30. Step 2: Session 2 has set the age value to 30.
  31. Step 3: Session 1 wants to set the age value to 20, but the result 1 is returned as nil, which indicates that the execution fails. Then, let's take the value of age as 30, this is caused by the optimistic lock on age in Session 1.


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.