Introduction to the usage scenarios of 5 data structures in Redis _redis

Source: Internet
Author: User
Tags data structures hash memcached redis

First, REDIS data structure usage scene

Originally read Redisbook This book, the basic functions of redis are already familiar with, starting from last week to see the source of Redis. The current goal is to thoroughly redis the data structure. As we all know, there are 5 kinds of data structures in the Redis, what are the scenarios used for each of these data structures?

string--string
hash--Dictionary
list--List
set--Collection
Sorted set--ordered set

Here's a brief description of their respective usage scenarios:

1. string--string

A string data structure is a simple key-value type, and value can be not only a string, but also a number (when a numeric type is represented by a Long, the encoding is an integral type, and others are stored in SDSHDR as a string). With the Strings type, you can fully implement the current Memcached functionality and be more efficient. You can also enjoy Redis time persistence (you can choose RDB mode or AOF mode), operation log and Replication functions. In addition to providing operations such as GET, set, INCR, DECR, and Memcached, Redis also provides the following actions:

Copy Code code as follows:

1.LEN niushuai:o (1) Get string length
2.APPEND Niushuai Redis: APPEND content to string, and intelligently allocate memory (twice times per time)
3. Set up and get a section of a string
4. Set and get a string of one (bit)
5. Set the contents of a series of strings in batches
6. Atomic counter
7.GETSET command, please set a new value while emptying the old value, with the atomic counter

2. hash--Dictionary

In Memcached, we often package structured information into HashMap, which is stored as a string after the client is serialized (typically in JSON format), such as user nicknames, age, gender, points, and so on. When you need to modify one of these items, you usually need to remove the string (JSON), then deserialize it, modify the value of an item, and then serialize it back into a string (JSON) store. Simply modify a property to do so many things, the consumption must be very large, also does not apply to some possible concurrent operation of the occasion (such as two concurrent operations need to modify the integral). The REDIS Hash structure allows you to modify only one property value just as you would Update an attribute in a database.

Copy Code code as follows:

Store, read, and modify user properties

3. list--List

List plainly is linked list (Redis use the list of two-terminal list), I believe that the knowledge of data structure should be able to understand the structure of the people. With the List structure, we can easily implement features such as the latest news ranking (such as the TimeLine of Sina Weibo). Another application of the list is Message Queuing, which can take advantage of the list's *push operation, place the task in the list, and then use the POP operation to take the task out of execution. Redis also provides an API to manipulate a section of the list, and you can query directly to delete the elements of a section in the list.

Copy Code code as follows:

1. Micro Bo TimeLine
2. Message Queuing

4. set--Collection

A set is a collection, and the concept of a set is a combination of a bunch of distinct values. Using the set data structure provided by Redis, some aggregated data can be stored. For example, in a microblog application, you can have all the followers of a user in a collection, and all their fans in one set. Because Redis is very humane for the collection provides the intersection, and set, the difference set and so on, then can be very convenient to achieve such as common concern, common preferences, two degrees friends and other functions, on the above all the set operation, you can also use different commands to choose the results returned to the client or save set to a new collection.

1. Common friends, two degree friends
2. Using uniqueness, you can count all the independent IP access to the website
3. When friends recommend, according to the tag intersection, greater than a certain threshold can recommend

5. Sorted set--ordered set

Compared with the Sets, Sorted Sets is a set of elements to add a weight parameter score, so that the elements in the collection can be ordered by the score, such as a storage class scores Sorted Sets, the collection value can be the student's school number, and SCO Re can be its test score so that when the data is inserted into the collection, it has been sorted naturally. In addition, you can use Sorted Sets to do with the weight of the queue, such as the ordinary message of the score 1, the important message of score 2, then the worker can choose to press the reverse of the score to get work tasks. Give priority to important tasks.

1. Elements with weights, such as the ranking of user scores for a game
2. The more complex data structure, the general use of the scene is not too much

Second, Redis other function usage scene

1. Subscription-Publishing System

Pub/sub is literally the publication (Publish) and Subscription (Subscribe), in Redis, you can set a key value for the message and message subscriptions, when a key value of the message published, all subscribed to its clients will receive the corresponding message. The most obvious use of this function is to use as a real-time messaging system, such as ordinary instant chat, group chat and other functions.

2. Business--transactions

Who says NoSQL does not support transactions, although Redis's transactions does not provide strict ACID transactions (such as a string of commands executed with EXEC execution, server downtime in execution, a portion of the command executes, the rest is not performed), but the Tran Sactions also provides basic commands for packaging execution (when the server is not in trouble, you can guarantee that a series of commands are executed sequentially, and that there are other client commands in between to execute). Redis also provides a Watch function where you can Watch a key and then perform transactions, in which case the watched will find and refuse to execute if the value of the transactions is modified.

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.