Advanced Applications for Redis

Source: Internet
Author: User
Tags configuration settings

Advanced applications for RedisExperiment Introduction

Before learning the basics and basic commands for Redis, go on to the advanced applications of Redis, including: Security settings, master-slave replication, transaction processing, persistence mechanism, virtual memory usage.

First, security

Set the password to be specified on the client connection (because the Redis speed is quite fast, a second can be 150K of password attempts, so you need to set a password strong passwords).

There are two ways to set a password:

(1) The Requirepass parameter of the config SET command is used, the format is config set requirepass "password". (2) Set the Requirepass property in configuration redis.conf, followed by the password.

There are two ways to enter authentication:

(1) You can redis-cli-a password when you log in

(2) Use auth after login password

(1) Set password

The first method of password setting has been mentioned in the previous experiment (in the example of Config set command), here we look at the second way to set the password.

You first need to go to the Redis installation directory and then modify the configuration file redis.conf. Based on the results of the grep command, use the VI editor to modify "# Requirepass foobared" to "Requirepass test123" and then save the exit.

$ grep-n requirepass/etc/redis/redis.conf

$ sudo vim/etc/redis/redis.conf

(2) Restart Redis-server and Redis-cli

Restart Redis Server

Ervice redis-server Restart

Enter into the REDIS-CLI interface for verification

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/70/14/wKiom1Wwx9bjEzFOAADmWejra0k218.jpg "title=" 24.png "alt=" wkiom1wwx9bjezfoaadmwejra0k218.jpg "/>

The result indicates that the first info command failed, and the info command returned normally after Auth authentication. Finally exit Redis-cli.

Another type of password authentication method:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/70/11/wKioL1Wwyciwc15OAAOlRcfXtp4212.jpg "title=" 25.png "alt=" wkiol1wwyciwc15oaaolrcfxtp4212.jpg "/>

second, master-slave replication

Due to the environmental reasons, here the author roughly explain the master-slave replication workflow, do not do experiments.

The master-slave replication configuration and use of Redis is relatively straightforward, and master-slave replication allows multiple slave servers to have the same database copy as master server.

The server is read-only and cannot be written.

Redis master-slave replication features:

1, master can have multiple slave.

2, more than one slave can connect to the same master, you can also connect to other slave. (When Master is down, the connected slave turns to master)

3, master-slave replication does not block master, and then synchronize the data, the primary can continue to process client requests.

4, improve the scalability of the system.

Redis master-slave replication process:

1, slave and master to establish a connection, send Sync Sync command.

2. Master starts a background process, saves the database snapshot to a file, and the master master process starts collecting new write commands and caches.

3. After the background is finished saving, send this file to slave.

4. Slave save this file to disk.

third, transaction processing

The transaction processing of Redis is relatively straightforward. Only the commands in the client-initiated transaction can be guaranteed to execute consecutively, and no other client commands are inserted, and when a client issues the multi command in the connection, the connection enters the context of a transaction, and the subsequent command of the connection is not executed but is stored in a queue. When the EXEC command is executed, Redis executes all the commands in the queue sequentially. If an error occurs in the execution, the correct execution is not rolled back, unlike the relational database transaction.

> Multi

> Set Name A

> Set name B

> Exec

> Get Name

Iv. Persistence mechanism

Redis is an in-memory database that supports persistence, and Redis needs to frequently synchronize the in-memory data to disk to ensure persistence.

Redis supports two persistence modes:

1, snapshotting (snapshot), the data stored in the file, the default way.

is to write a snapshot of the in-memory data into a binary file, the default file Dump.rdb, which can be automatically persisted by configuration settings. Redis can be configured to automatically save snapshots in n seconds if more than M key is modified.

Save 1 #900秒内如果超过1个key被修改 to initiate a snapshot save

Save #300秒内如果超过10个key被修改, the snapshot is saved

2, Append-only file (abbreviated as AOF), the read and write operations are stored in the file.

Because the snapshot is done at a certain interval, if Redis is accidentally down, all modifications after the last snapshot are lost.

AOF is more persistent than snapshot, because Redis writes each received write command through the Write function to a file, and when Redis starts, it re-establishes the contents of the entire database in memory by re-executing the Write command saved in the file.

Because the OS caches write modifications in the kernel, it may not be written to disk immediately, so the persistence of aof mode may also result in the loss of some data. You can tell Redis through the configuration file that we want to force the OS to write to disk through the Fsync function.

Configurable parameters in the configuration file:

AppendOnly Yes//enable AOF persistence mode

#appendfsync always//Receive write commands to write to disk immediately, the slowest, but ensure the complete persistence of the data

Appendfsync everysec//writes to disk once per second, making a good compromise in performance and persistence

#appendfsync no//full reliance on OS, best performance, no guarantee of persistence

In the REDIS-CLI command, the Save command is to write data to disk.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/70/11/wKioL1WwyryAz8_GAAD8m4cI-sw885.jpg "title=" 3.png " alt= "Wkiol1wwyryaz8_gaad8m4ci-sw885.jpg"/>650) this.width=650; src= http://s3.51cto.com/wyfs02/M02/70/14/ Wkiom1wwx-2qbdd5aahxdo_hlqo511.jpg "title=" 26.png "alt=" Wkiom1wwx-2qbdd5aahxdo_hlqo511.jpg "/>


V. Use of virtual memory

Virtual memory management was canceled at 2.6 and above, and in the installation experiment, 2.8.9 Redis, no configuration options for the virtual memory management feature are available in the configuration files in the experiment. This is the only explanation

Redis's virtual memory is temporarily swapping infrequently accessed data from memory to disk, freeing up memory space for other access data, especially for memory databases such as Redis, where memory is not always sufficient. In addition to separating multiple Redis servers, the way to increase the capacity of a database is to use virtual memory to exchange infrequently accessed data to disk.

By configuring the VM-related redis.config configuration:

vm-enable Yes #开启vm功能

Vm-swap-file/tmp/redis.swap #交换出来的value保存的文件路径vm-max-memory 10000000 #redis使用的最大内存上线 vm-page-size 32 #每个页面的大小32字节

Vm-pages 123217729 #最多使用多小个页面

Vm-max-threads 4 #用于执行value对象换入的工作线程数量

Reference Documents

http://m.blog.csdn.net/blog/fengshizty/42936073#


Advanced Applications for 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.