Redis Easy to understand

Source: Internet
Author: User

About Redis

REDIS is an acronym for Remote DIctionary Server.

Stores data in a dictionary structure and allows other applications to read and write content from the dictionary through the TCP protocol.

The supported key-value data types are string-type strings, hash-type hashes, List-type lists, set-type set, and ordered collection-type Zset.

Memory Storage and persistence

Redis can set the lifetime (time to Live,ttl) for each health and will be automatically deleted when the lifetime expires. Can be used as a cache system.

In performance, Redis is a single-threaded mode, and memcached supports multithreading, so the latter performance is higher on multicore servers.

Redis was developed using the C language.


Redis Multi-database

A Redis instance provides multiple dictionaries for storing data, and the client can specify which dictionary the data is stored in.

Each dictionary is similar to a separate database. Each dictionary is named after an incrementing number starting from 0, which supports 16 by default and does not support a custom database name.

Data for different applications in general should use different Redis instance stores.


Redis does not differentiate command case

All the commands that Redis provides are atomic operations (atomic operation).

Redis has no mandatory naming of keys, and a good practice is to use "Object type: Object ID: Object property" to name a key, such as using the key user:1:friends to store the friend list of the user with ID 1.

The nesting of data types is not supported for Redis individual data types.

—————————————————————————————————————

Redis Installation

A Redis contract version number (that is, the number after the first decimal point) is an even version of a stable version (such as 2.4, 2.6), and the odd version is a non-stable version (such as 2.5, 2.7).

Download the installation:

wget http://download.redis.io/redis-stable.tar.gz

Tar xzf redis-stable.tar.gz

CD redis-stable

Make

Redis has no other external dependencies.


Redis Run

Start redis:1. Direct Start (command: Redis-server); 2. Start by initializing the script;

Stop Redis:redis-cli SHUTDOWN (the KILL command effect is the same as sending the SHUTDOWN command)


Redis Command-line Client

The REDIS-CLI (Redis command line Interface) is a Redis-based Redis client that comes with its own.


Redis Configuration

Redis provides a configuration template redis.conf, located in the root directory of the source code directory.

—————————————————————————————————————

Data structures for Redis

String type

Summary: You can store any form of string, and a string type key allows the maximum capacity of the stored data to be 512MB.

Command: SET key Value/get Key


Hash type

Summary: Hash types are suitable for storing objects, using object categories and IDs to form key names, using fields to represent objects ' properties, and field values to store property values.

Command: Hset key field Value/hget key field (hmset key field value [Fielld value ...] /Hmget key field [field ...]


List type

Summary: A list type can store an ordered list of strings that are commonly used to add elements to the ends of a list, or to get a fragment of a list. The list type is internally implemented using a doubly linked table, which is faster to add delete elements to both ends, but is slower to access elements through the index.

Command: Lpush key value [value ...]/rpush key value [value ...]/lpop key/rpop key/lrange key Start stop


Collection type

Summary: Each element in the collection is different and has no order. A common operation is to add or remove elements to a collection, determine whether an element exists, and so on, and the collection type is implemented using a hash table with empty values inside the Redis, so these operations are faster. Set-type keys can also be used to perform a set, intersection, and difference operation.

Command: Sadd key member [member ...]/Srem key member [member ...]/sdiff key [key ...]/SINTER key [key ...]/sunion key [ Key ...]


Ordered collection types

Summary: An ordered collection type, based on a collection type, associates a fraction to each element in the collection, sorting each element by that fraction. We do the first n elements that get the highest (or lowest) score, and we get the operations that are equal to fractions of the elements in the specified fractional range. Although each element in the collection is different, their scores can be the same. An ordered collection can adjust an element so far by changing its score.

Command: Zadd key score member [score member ...]/zscore key Member/zrange key start stop [withscores]/zrevrange key start Stop [Withscores]

—————————————————————————————————————

Redis transactions:

A transaction (transaction) in Redis is a set of commands. A transaction, like a command, is the smallest execution unit of Redis, and commands in one transaction are either executed or not executed.

The principle of a transaction is to send a command that belongs to a transaction to Redis, and then let Redis execute the commands sequentially.

Transaction Example:

Redis> MULTI

Ok

Redis> Sadd "User:1:following" 2

QUEUED

Redis> Sadd "User:2:followers" 1

QUEUED

Redis> EXEC

1) (integer) 1

2) (integer) 1

The code above demonstrates how transactions are used.

First the Multi command tells Redis that the following command belongs to a transaction, the cache is not executed;

Redis answer OK;

Send two Sadd command, Redis does not execute command, but put into waiting queue queued;

After the command to execute is sent, the EXEC command is sent to tell Redis that it can execute;

Redis waits for commands to execute in the order in which they are sent, and then returns the execution results.


Fault handling:

If the client is disconnected before the EXEC command is sent, Redis clears the transaction queue and all commands are not executed, but if the client is disconnected after the EXEC command has been sent, Redis executes all of the commands.

Redis transactions can also ensure that commands within a transaction are executed sequentially without being inserted by other commands.

What does Redis do if one of the commands in a transaction performs an error?

When an error occurs before the command executes, such as a syntax error, REDIS returns an error directly and all commands are not executed;

When an error occurs in the execution of a command, such as a run-time error when executing a command, Redis receives and executes, and all commands execute, because it cannot be discovered before execution.

The Redis transaction has no relation to the rollback (rollback) functionality provided by the database transaction.


Watch command

We already know that the return value of each result can only be obtained when all commands are executed sequentially in one transaction, but in some cases it is necessary to obtain the return value of a command before executing the next command based on that value.

The above scenario produces a race condition when the transaction is implemented. Another member of the Transaction Family watch, the command can monitor one or more keys, once one of the keys is modified (or deleted), then the transaction will not be executed. Monitoring continues to the EXEC command (the commands in the transaction are executed after exec, so you can modify the watch Monitor's key values after the multi command).

Because the Watch command works only when the monitored key value is modified to block the execution of a transaction, and does not guarantee that the other client does not modify the key value, we need to re-execute the entire function after the exec fails.


Survival time

Sometimes effective data, such as time-limited offers, caches, or verification codes, can be deleted after a certain period.

Redis can use the expire command to set the lifetime of a key, and Redis will automatically delete it when the time comes. By default, the time is not set for a permanent existence.

Command: EXPIRE key seconds//The above command identifies that the key key will be deleted after seconds seconds.


Limit the frequency of access: for example, to limit the maximum number of accesses per user to 100 times a minute.

Idea: Each user stores the "rate.limiting: User IP" String type key, each time the user accesses the increment, the first access to set the lifetime of the key is 1 minutes. The key value is read every time the user accesses, and the limit is determined by more than 100. The key is automatically deleted every minute and the user can recalculate the limit value next minute.


Implementing caching: To increase the load capacity of your Web site, you often need to cache some of the results of high-frequency operations that are expensive for CPU or IO resources, and you want these caches to expire automatically over time.

You can do this by giving the key a way to set the time to live. Each access first queries the cache, then returns the cached value, without recalculating and dropping the cache and setting the cache lifetime at the same time.

But the memory of the server is limited, and the memory that Redis can use is limited. Too long a cache key's time-to-live setting causes Redis to fill up memory, and setting too short causes the cache hit rate to be too low. Actual development will find it difficult to set a reasonable time-to-live for the cache key, which limits the maximum amount of memory that Redis can use, and allows Redis to eliminate unwanted cache keys according to certain rules, which is useful when using Redis as a caching system only.

—————————————————————————————————————

The sort of Redis

Sort command:

The sort command sorts the list type, collection type, and ordered collection type keys, and can accomplish tasks similar to connection queries in the relational database.

In addition to arranging numbers, the sort command allows you to arrange non-numeric elements in dictionary order by using the Alpha parameter.

The sort command is arranged by default in order from small to large, and if you want to order from large to small, you need to use the desc parameter.

The sort command implements paging: Returns the result of a specified range by supporting the limit parameter. In the same way as SQL statements, the LIMIT offset count indicates that the previous offset element is skipped and the count element after it is obtained.


By parameter:

The data is typically stored with the ID of the object identified, but more often we want to sort by an attribute of the object corresponding to the ID.

The By parameter syntax is "by reference key". A reference key can be a key of a string type or a field of a hash type key (denoted by a field name, such as a key name).

If the by parameter is provided, the sort command will no longer be sorted by the value of the element itself, instead the first "*" in the reference key is substituted for each element using the value of the element and the value is obtained, and the elements are sorted based on that value.

Example: SORT tag:ruby:posts by Post:*->time DESC


Get Parameters:

The get parameter does not affect sorting, it does so that the return result of the sort command is no longer the value of the element itself, but the key value specified in the Get parameter.

Example: SORT tag:ruby:posts by Post:*->time DESC GET Post:*->title


Store Parameters:

The sort command returns the sorting results directly, and you can use the store parameter if you want to keep the sort results. The store parameter is commonly used to combine the expire command cache sorting results. Example pseudo-code:

#判断是否存在之前排序结果的缓存

$isCacheExists = EXISTS Cache.sort

If $isCacheExists is 1

#如果存在则直接返回

Return Lrange cache.sort, 0,-1

Else

#如果不存在, the sort command is used to order and the result is stored in the Cache.sort key as the cache

$sortResult = SORT some.list STORE cache.sort

#设置缓存的生存时间为10分钟

EXPIRE Cache.sort, 600

#返回排序结果

Return $sortResult

—————————————————————————————————————

Message notification


Task queue: A queue of delivery tasks that can be used to implement the notification process with a task queue.

The benefits of task queuing: loose coupling, producers and consumers need not know each other's implementation details, easy to expand the consumer, can have multiple and can be distributed across different servers.

Redis implements the task queue: A list type that uses Redis, and the concept of its lpush and Rpop commands to implement the queue. Brpop and Blpop also implement a blocking mechanism without elements.


Priority queue: Priority consumption of urgent messages.

Can be achieved by using the Brpop command, BRPOP can receive multiple keys at the same time, such as: Brpop key [key ...]

The meaning is to detect multiple keys at the same time, if none of the keys are blocked, and if one of the keys has an element, the element is ejected from the key. If multiple keys have elements, an element in the first key is taken from left to right.

As a result, the key of the message to be prioritized is placed in front of the consumption order, so that no matter what the element is, how much is squeezed, as long as the previous element is prioritized.


Publish/Subscribe mode: Two roles, publisher and Subscriber, respectively. Subscribers can subscribe to one or more channels (channel), and publishers can send messages to the specified channel, and all subscribers to the channel receive this message.

Command: PUBLISH channel Message/subscribe Channel [channel ...]

Subscribe by rules: Use the Psubscribe command to subscribe to the specified rules. Rules support GLOB style wildcard format. such as: Psubscribe Channel.? *

—————————————————————————————————————

The Redis pipeline

Clients and Redis use TCP protocol connections. Whether the client sends a command to Redis or the result of a Redis return command to the client, it needs to be transmitted over the network, and the total time spent in these two parts is called round trip latency. When you execute multiple commands, each command waits for the last command to execute, even if the command does not require the result of the previous command.

The underlying communication protocol for Redis provides support for pipelines (pipelining). A pipeline can send multiple commands at once and return the results one time after execution, and the set of commands can be sent together through a pipeline when each command in a set of commands does not depend on the result of the previous command's execution. The pipeline reduces the number of communication between the client and Redis to achieve the goal of reducing the total round-trip delay.

—————————————————————————————————————

Optimize your redis storage space

Redis is a memory-based database, and all of the data is stored in memory, so it is important to optimize storage and reduce memory footprint for cost control.

1. Compact key names and key values;

2. Internal code optimization: Redis provides two internal encodings for each data type, and Redis automatically adjusts to the actual situation.

Data type internal encoding method OBJECT encoding command result

String type redis_encoding_rawRAW

Redis_encoding_intINT

Hash type redis_encoding_htHashtable

Redis_encoding_ziplistziplist

List type redis_encoding_linkedlistLinkedList

Redis_encoding_ziplistziplist

Collection type redis_encoding_htHashtable

Redis_encoding_intsetIntset

Ordered collection type redis_encoding_skiplistskiplist

Redis_encoding_ziplistziplist

Shared objects: Redis starts with 10,000 redisobject type variables that store numbers from 0 to 9999, respectively, as shared objects, if the string key value you want to set is within these 10,000 digits (such as set Key1 123) You can directly reference the shared object without having to establish the redisobject. Thus, using the string type key to store the object ID is a very small number that saves storage space, and Redis simply stores the key name and a reference to the shared object.

The Redis_encoding_ziplist encoding type is a compact encoding format that sacrifices partial read performance in exchange for extremely high space utilization and is suitable for use with fewer elements.

—————————————————————————————————————

Script

Redis launched the scripting feature in version 2.6, allowing developers to use LUA voice scripting to execute in Redis, where most of the Redis commands can be called. The benefits of using scripts are as follows:

1. Reduce network overhead; Multiple commands can be placed in a script to send a request, reducing network round-trip latency.

2. Atomic operation; The entire script will be executed as a whole, and the middle will not be inserted by other commands.

3. Reuse; scripts are permanently stored in Redis and can be reused by other clients.

Lua is an efficient, lightweight scripting language. Lua is the "moon" in Portuguese language and its logo resembles a satellite, implying that Lua is a "satellite voice" that can be easily embedded in other languages.

—————————————————————————————————————

Persistence of : With the persistence feature, Redis guarantees that there will be no loss (or small loss) of data even if the server is restarted.

RDB mode;

AoF mode (Append only File);

—————————————————————————————————————

Copy:

If the data is stored on a single server, it can also cause data loss when the hard disk fails.

To avoid a single point of failure, Redis provides the ability for replication to automatically synchronize, replicating multiple copies of the database to be deployed on different servers.

Replication enables read-write separation to increase the load capacity of the server.


The Redis database is divided into two categories: the primary database and the database;

The primary database can read and write operations, and when the write operation occurs, the data is automatically synchronized to the database;

From the database is generally read-only, and receives the data that the primary database synchronizes.


Using replication in Redis is simple, simply by adding "slaveof Primary database IP Primary database port" from the configuration file of the database, without any configuration of the primary database.

In addition to using the configuration file to set the slaveof parameter, you can also modify it using the slaveof command at run time.


How Redis is replicated:

When one starts from the database, the Sync command is sent to the primary database, and the master database receives the sync command to start saving the snapshot in the background (that is, the RDB persistence process) and caches the commands that are received indirectly during the retention period. When the snapshot is complete, Redis sends the snapshot files and all cached commands to the slave database. When received from the database, the snapshot file is loaded and the cached command is executed. When the master-slave database is disconnected, the above operation is re-executed, and the breakpoint is not supported for continuous transmission.


From the database can not only receive the synchronization data of the main database, but also can exist as the primary database.

For example: A has B and C two from, B has D and e two from; When writing data to B, it can only be synchronized to D and E, and cannot be synchronized to a or C.


Persistence is time-consuming, and in order to improve performance, you can create one (or several) from the database through replication, and enable persistence from the database while disabling persistence in the primary database.

Restarting the base database when it crashes from the database automatically synchronizes the data so there is no need to worry about data loss.

When the primary database crashes, you need to use the slaveof NO one command from the database to promote the service from the database to the primary database, and then use the slaveof command to set it to the new primary database from the database after the original primary database is started, and then synchronize the data back.

—————————————————————————————————————

Time-consuming command log:

When a command execution time exceeds the limit, Redis adds information such as the execution time of the command to a time-consuming command log (slow log) for development viewing.

This limit can be set by the Slowlog-log-slower-than parameter of the configuration file, which is subtle (1000000 microseconds = 1 seconds) and the default value is 10000.

The time-consuming command log is stored in memory, and the number of records can be limited by the Slowlog-max-len parameter of the configuration file.


Redis Easy to understand

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.