Redis QuickStart: A first-knowledge redis

Source: Internet
Author: User
Tags value store

"IT168 Zhuangao" in the previous article described "Redis QuickStart: Choose Key-value Store", today to introduce you to the basics of Redis. Redis is an open source API that is written in ANSI C, supports the web, can be persisted in memory, key-value databases, and provides multiple languages. From March 15, 2010 onwards, the development work of Redis is hosted by VMware.

1. Data type

As a key-value database, Redis also provides a mapping of key (key) and key value (value). However, in addition to the regular numeric value or string, the Redis key value can also be one of the following forms:

Lists (list)

Sets (collection)

Sorted sets (Ordered collection)

Hashes (hash table)

The data type of the key value determines the operation that the key value supports. Redis supports advanced atomic operations such as the intersection of lists, sets, or ordered sets, set, and set, and, if the type of the key value is a normal number, Redis provides self-increment atomic operations.

2. Persistence

Typically, Redis stores data in memory or is configured to use virtual memory. Data persistence can be achieved in two ways: by writing the data in memory to disk continuously, or by logging each update with a log method similar to MySQL. The former performance is high, but may cause some degree of data loss; the latter is the opposite.

3. Master-Slave synchronization

Redis supports synchronizing data to multiple slave libraries, a feature that is useful for improving read performance.

4. Performance

Memory-based features undoubtedly provide Redis with excellent performance compared to the need to rely on disk records for each updated database. There is a significant performance difference between read and write operations.

5, the language of providing the API

C

C + +

C #

Clojure

Common Lisp

Erlang

Haskell

Java

Javascript

Lua

Objective-c

Perl

PHP

Python

Ruby

Scala

Go

Tcl

6. Applicable occasions

Without a doubt, Redis pioneered a new approach to data storage, and using Redis, we don't have to focus on the problem of how to put an elephant into a refrigerator in the face of a monotonous database, but instead use Redis's flexible data structure and data manipulation to build different refrigerators for different elephants. I hope you like this metaphor.

Here are some of the scenarios that Redis applies to:

(1), take the latest n data operation

For example, the most recent article on your website, in the following way, we can put the latest 5,000 reviews of the ID in the Redis list collection, and will be out of the collection part from the database.

Use the Lpush latest.comments command to insert data into the list collection

Insert complete and then use the LTrim latest.comments 0 5000 command to keep the last 5,000 IDs forever

Then we can use the following logic when retrieving a page comment from the client

FUNCTION get_latest_comments (Start,num_items):
Id_list = Redis.lrange ("latest.comments", start,start+num_items-1)
IF Id_list.length < Num_items
Id_list = sql_db ("Select ... ORDER by Time LIMIT ... ")
END
RETURN id_list
END

If you have a different filter dimension, such as the newest n for a category, you can build a list that is categorized by this category, and Redis is very efficient if you save the ID.

(2), leaderboard application, take top n operation

This requirement differs from the above requirements in that the preceding operation takes the time as the weight, this is the weight of a certain condition, such as the number of times by the top, then we need our sorted set to go, set the value you want to sort into the score of sorted set, Set the specific data to the corresponding value, each time only need to execute a zadd command.

(3), the need to accurately set the expiration time of the application

For example, you can set the score value of the sorted set to the timestamp of the expiration time, then you can simply sort through the expiration time, and periodically purge out-of-date data, not only to clear the expired data in Redis, You can think of this expiration time in Redis as an index to the data in the database, use Redis to find out what data needs to be deleted, and then delete the corresponding records from the database exactly.

(4), counter application

Redis commands are atomic, and you can easily use the INCR,DECR command to build a counter system.

(5), uniq operation, to obtain a period of time all data row weight value

This is the most appropriate set data structure to use Redis, just to constantly throw it into set, set means set, so it automatically takes weight.

(6), real-time system, anti-spam system

With the Set function mentioned above, you can know whether an end user has performed an operation, can find a combination of its operations to analyze statistical comparisons, and so on. There is nothing to be done, only unexpected.

(7), pub/sub build real-time message system

Redis's pub/sub system can build real-time messaging systems, such as many examples of real-time chat systems built with Pub/sub.

(8), build the queue system

Using list, you can build a queue system, and you can even build a prioritized queue system using sorted set.

(9), cache

This needless to say, performance is better than memcached, data structure is more diversified.

Redis QuickStart: A first-knowledge redis

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.