Redis Data Structure Usage scenarios

Source: Internet
Author: User
Tags live chat

I. REDIS data structure usage scenarios

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

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

Let's take a brief look at their respective usage scenarios:

1. string--string

The string data structure is a simple key-value type, and value can be either a string or a number (encoding is an integral type when a number type is represented by a Long, and the rest is 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 the timing persistence of Redis (you can choose RDB mode or AOF mode), operation log and Replication functions. In addition to providing operations like get, set, INCR, DECR, and so on, Redis provides the following Memcached:

    • LEN niushuai:o (1) Gets the string length
    • APPEND Niushuai Redis: APPEND content to strings, and intelligently allocates memory (twice times per time)
    • 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
    • Atomic counter
    • 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 value (usually in JSON format) after the client is serialized, such as the user's nickname, age, gender, and points. When you need to modify one of these items, you usually need to remove the string (JSON), then deserialize, modify the value of an item, and then serialize it back into a string (JSON). 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 Hash structure of Redis allows you to modify only one item property value just as you would Update a property in a database.

3. list--List

The list is plainly a list of links (Redis uses a doubly-linked list), and anyone who learns about data structure knowledge should be able to understand its structure. With the List structure, we can easily implement features such as the latest message ranking (such as the TimeLine of Sina Weibo). Another application of list is the message queue, which can take advantage of the *push operation of list, the task exists in list, and then the worker thread takes out the task with the POP operation to execute. Redis also provides an API to manipulate a section of the list, and you can directly query and delete elements of a section of the list.

4. set--Collection

A set is a set, and the concept of a collection is a combination of a bunch of distinct values. Using the set data structure provided by Redis, you can store some aggregated data. For example, in a microblog application, you can have a collection of all the followers of a user, and a collection of all their fans. Because Redis is very human for the collection to provide the intersection, set, difference sets and other operations, it 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.

    • Common friends, two-degree friends
    • With uniqueness, you can count all the independent IPs that access the site
    • When friends recommend, according to tag intersection, more than a certain threshold can recommend

5. Sorted set--ordered Collection

Compared with sets, Sorted sets is to add a set of elements of a weight parameter score, so that the elements in the set can be ordered by score, such as a storage of the class students Sorted sets, its collection value can be the student's school number, and SCO Re can be the test score, so that when the data is inserted into the collection, it has been sorted in a natural way. 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.

    • Elements with weights, such as a game's user score leaderboard
    • More complex data structures, generally used in the scene is not too much
Second, Redis other feature usage scenarios

1. Subscription-Release system

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.

2. Transaction--transactions

Who says that 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), but this Transac tions also provides the basic command package execution function (in the case of the server does not have a problem, 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.

Redis Data Structure Usage scenarios

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.