Redis Usage Summary-Basic article

Source: Internet
Author: User
Tags configuration settings

At the end of the year, I began to try to use Redis in a reconstructed project, and now the project has been running stably for some time, so here's a summary.

First, Introduction

First of all, what does Redis mean, and the official documentation FAQ gives the answer: it means remote DIctionary server. That is, it is a dictionary server that can be called remotely, as you can see from the name It has to be a k-v server at least:), in fact, it supports a number of more complex operations in addition to the basic operations of GET, set, Del, which is where it outperforms other k-v components such as memcached.

Redis is an advanced k-v server that can be used either as a cache or as a storage server, and it can also be called a data structure . Why do you call it a data structure server? Because it supports several data structures, as the value of Key-value, it can store strings, hashes, lists, sets, sorted sets, bitmaps, and hyperloglogs, and so on, and For different data structures, Redis specializes in special operations that allow us to use operations other than get set, for example, you can intersect two sets and return results.

Second, installation and start-up

Redis installation is very simple, from the official website to download the latest stable version of the client, make can be:

wget http://download.redis.io/releases/redis-2.8.19.tar.gztar xzf redis-2.8. . Tar . gz$ CD Redis-2.8.  +   make

Startup is also very simple, the compiled binaries will be generated in the SRC directory, directly executed, you can use the default configuration to start the Redis service:

$ src/redis-server

So the service starts up, Redis listens to port 6379 by default, and we can use the client (REDIS-CLI) to access and test it:

$ src/redis-Cliredis> set foo barokredis> get foo"bar"

More complex configuration settings we will introduce again in the rear.

Iii. examples of basic use

To quickly understand how Redis is used, it is highly recommended that an article writing a simple Twitter clone with PHP and Redis (Http://redis.io/topics/twitter-clone), If you are not familiar with PHP syntax is not related, PHP's client basically is the Redis command intact encapsulation, the article from the perspective of actual combat to describe the use of several data structures and in the actual use of the process should be how to design their own storage methods. Here we briefly describe how several data structures are used:

3.1 String

String is the most basic data structure, if the simple use of string, then Redis and the normal k-v database is no different, nothing more than a get, set, let's look at the basic operation of string:

> set MyKey somevalueok> get mykey"somevalue"

When the set is executed, if there is no value under the "MyKey" key, the "somevalue" will be stored, otherwise the value will be overwritten.

If you provide only get, set, then Redis is not a redis, for a key, we can also use INCR,DECR to increase or decrease the value of a numeric type, as well as the Mget command, you can get the value of multiple keys at the same time, which is very useful in the actual use of the process, For example, when you need to get multiple values simultaneously, you can drastically reduce network overhead by simply calling Mget at once.

3.2 Hashes

Next we look at the hash, the first reaction, we may think, just now the string is not a hash, from the key mapping to value. In fact, Redis says here hash, equivalent to just a string form of a nested, equivalent to We have a hash object, the name of this object is a key, and its value is a hash, and its value of the key, in the Redis called field, Then we can understand the order of the hash insert data:

> Hset User:  usernameok> Hget User:  username"Antirez "

Hash can also write multiple values at the same time, using the Hmset command:

> Hmset User:19771OK

Similarly, it is possible to use the Hmget command when reading multiple values at the same time, but it is important to note that when using Hmget, you can only read multiple field values under the same key at a time, instead of getting different field values under different keys. If you want to get different field values under different keys, you can use Redis's pipeline method to get them, which reduces overhead.

3.3 Sorted Sets

Sorted set is a high-level Set,redis itself also supports set, but here we speak directly Sorted set, it is more than the normal set, a score item, that is, when inserting data, each value can be given to it a score, So what does score do with it? Sorted set, as the name implies, is can be sorted according to score, when we take out the time can be seen, this list is a good order, this function is very useful, for example, when we want to develop a leaderboard, directly insert the list of items and ranking points can be, Or to develop a micro-blogging system, use it to store timeline, using message publishing time as score, then the timeline nature is orderly, and then use the Redis itself to provide the intersection or the function of the convergence, which makes the business layer of the development of a significant reduction in the volume, Let's take a look at the command example:

> Zadd Hackers1940 "Alan Kay"(integer)1> Zadd Hackers1957 "Sophie Wilson"(integer1)> Zadd Hackers1953 "Richard Stallman"(integer)1> Zadd Hackers1949 "Anita Borg"(integer)1> Zadd Hackers1965 "Yukihiro Matsumoto"(integer)1> Zadd Hackers1914 "Hedy Lamarr"(integer)1> Zadd Hackers1916 "Claude Shannon"(integer)1> Zadd Hackers1969 "Linus Torvalds"(integer)1> Zadd Hackers1912 "Alan Turing"(integer)1

Read by using Zrange or Zrevrange, as the name implies is a positive or reverse order read:

> Zrange hackers 0-1 withscores

1) "Alan Turing"

2) "1912"

3) "Hedy Lamarr"

4) "1914"

5) "Claude Shannon"

6) "1916"

7) "Alan Kay"

8) "1940"

9) "Anita Borg"

10) "1949"

One) "Richard Stallman"

12) "1953"

"Sophie Wilson"

14) "1957"

() "Yukihiro Matsumoto"

16) "1965"

) "Linus Torvalds"

18) "1969"

Where Withscores is to tell Redis whether to return the score item, it is very convenient to return only the list of people without this parameter.

3.4 Summary

Redis provides a very rich set of commands to use, making it easy to do some advanced operations on storing data, rather than just treating it as a k-v storage data, which can reduce our business complexity to some extent.

Redis commands can be queried on the official website (http://redis.io/commands), not listed here.

Iv. Configuration and Deployment

4.1 Persistence

Redis provides two ways to persist data to disk, avoid service crashes, or data loss after a machine restart, which are aof and RDB, respectively.

AOF is the equivalent of the binlog commonly used in our services, and Redis writes this request to the AoF file whenever a write request comes in, and when Redis crashes, it can read the AoF file to reconstruct the data.

The RDB is also called snapshoting, which is the snapshot, which is the equivalent of the data dump from shared memory to disk that we often use in our service.

Both of these options can be configured in the configuration file, Redis provides a profile template, a redis.conf file under the Redis folder, and when you start Redis with a profile, the parameters of a profile are added behind Redis:

SRC/REDIS-CLI redis.conf

To open the AOF feature, you can modify the AppendOnly entry in the configuration file to Yes, and specify Appendfilename as the aof file name, note that this is only the configuration file name, the file path needs to modify the Dir configuration item, this configuration item also applies to the Rdb file.

The RDB feature is open by default and is affected by the save entry in the configuration file:

 the 1   10000

Here to explain the meaning of these options: SAVE N m represents at least m changes per N seconds, before the RDB operation, then the top three options mean, when there are 10,000 changes in 60 seconds, write the disk, and when there are 10 changes in 300 seconds, also write disk, if the write is not frequent , then within 900 seconds, if there is a change, write it once.

In addition to automating the way, we can also manually invoke the Bgsave command, forcing data backup, but this command literally looks like a background, but in fact, it will also cause service blocking!

It should be noted that the AOF function can turn on the compression mechanism, when the aof file is too large, can be compressed, but this action will block the process, so use it cautiously! The Rdb file is also the same, when the write RDB operation, the process is also blocked, these two places is the Redis is a more troublesome place, and currently did not find a perfect solution.

4.2 Primary and Standby synchronization

The settings for the primary and standby synchronization are relatively simple, and the SLAVOF option is configured in the configuration file:

Slaveof <masterip> <masterport>

Redis initiates a connection to the host, and the host synchronizes the data, but it is important to note that the process is also blocked and the service is inaccessible during this time.

4.3 Summary

Redis provides a simple way to persist, but the way it blocks is often caused by a certain amount of time in the service is not available, so in the selection of the time to combine their own business situation to decide how to use the configuration.

Redis Usage Summary-Basic article

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.