Redis Learning Notes

Source: Internet
Author: User
Tags failover install redis

Redis is an open source API that is written in ANSI C, supports the network, can be persisted in memory, key-value databases, and provides multiple languages.

Redis is a key-value storage system. Similar to memcached, it supports storing more value types, including string (string), list (linked list), set (set

Zset (sorted set--ordered set) and hash (hash type) and memcached, in order to ensure efficiency, the data is cached in memory. The difference is

Redis periodically writes the updated data to the disk or writes the modified operation to the Chase

and the Master-slave (master-Slave) synchronization is implemented on this basis.

Redis supports master-slave synchronization. Data can be synchronized from the primary server to any number of slave servers, from the server to the primary server that is associated with other slave servers

Redis and memcached do not have a performance bottleneck for processing concurrent requests, and performance bottlenecks may be on the NIC

Official Bench-mark data: Result: Read speed is 110,000 times/s, write speed is 81,000 times/s

Installation: yum-y install epel-release yum-y Install redis-3.0.7-2.el6.remi.x86_64.rpm

Configuration file: etc/redis.conf vim/etc/redis.conf

If it is based on local communication, modify the open Unixsocket/tmp/redis.sock file and its permissions Unixsocketperm 700 to improve performance

Several parameters are described:

Databases: Specifies how many databases are supported, using only library No. 0 in the default cluster (distributed scenario), and not supporting the use of multiple libraries

Tcp-keepalive 0 Turn off TCP persistent connection feature

Save 900 1 900 seconds a data modification in which the modified data is synchronized to the disk's data file (persistent RDB)
Save 300 10
Save 60 10000

This feature can be commented out

Requirepass after you set up a client connection to make any other specified password before you log in, use auth password to complete the authentication

Login command Redis-cli-h HostIP For example: redis-cli-h 127.0.0.1 or REDIS-CLI

Start Services: Service Redis start

Redis commands Get help

The persistence of Redis:

RDB persistence to generate a point-in-time snapshot of a dataset within a specified time interval (point-in-time snapshot)

AOF persists all write commands that are performed by the server and restores the dataset when the server starts by re-executing these commands

Advantages of the RDB: The only thing the parent process has to do when saving the Rdb file is to fork out a child process, and the child process will process all the subsequent save work, the parent

Process There is no need to perform any disk I/O operations. The RDB recovers large datasets faster than AOF, and can be restored to a different database based on the backup strategy .

Point in time

The disadvantage of the RDB is that Redis has to fork () out a subprocess and perform the actual persistence work by child processes. When datasets are large, fork () may be non-

Time-consuming, causing the server to stop processing the client in such milliseconds, and if the data set is very large and CPU time is very tight, then this stop can even

Will last a whole second. Although the AOF rewrite also requires fork (), no matter how long the execution interval of the AOF rewrite, the durability of the data will not have any loss, once the

Downtime may lose data for several minutes

Advantages of AOF: The default policy for AOF is Fsync once per second, in this configuration, Redis can still maintain good performance, and even if there is a failure to stop,

A maximum of one second of data is lost, and the AOF file is an append-only log file (append only log), so the write to the AOF file does not need to be

To seek, even if the log contains incomplete commands for some reason (e.g., write-on-disk full, write-down, etc.), redis-check-aof

can also easily fix this problem, Redis can automatically rewrite the AOF in the background when the AOF file size becomes too large: the rewritten new AOF package

Contains the minimum set of commands required to restore the current data set. The entire rewrite operation is absolutely safe because Redis continues to append commands to the new AOF file as it is created

To an existing AOF file, the existing AOF file will not be lost even if there is an outage during the rewrite process. And once the new AOF file is created, Redis will start from the old

The AOF file switches to the new AOF file and begins the append operation to the new AOF file. The AOF file preserves all write operations to the database in an orderly manner, and these write

is saved in the format of the Redis protocol, so the contents of the AOF file are easy to read and parsing (parse) is easy. Exporting (export)

The AOF file is also very simple: for example, if you accidentally executed the Flushall command, but as long as the AOF file is not rewritten, just stop the server and move

In addition to the Flushall command at the end of the AOF file, and restart Redis, you can restore the dataset to the state it was in before the Flushall was executed.

AOF disadvantage: For the same data set, the size of the AOF file is usually larger than the size of the RDB file. Depending on the Fsync policy used, the speed of the AOF may be

Slower than RDB. In general, Fsync performance per second is still very high, while closing fsync can make AOF as fast as an RDB, even under high load

The same is true. However, the RDB can provide a more guaranteed maximum delay time (latency) when handling large write loads. AOF has been such a bug in the past.

: Because of an individual command, the AOF file cannot be restored as it was when the data set was re-loaded. (for example, a blocking command Brpoplpush

has caused such a bug. Tests are added to this scenario in the test suite: They automatically generate random, complex datasets, and by re-loading these numbers

To make sure everything is OK. Although this bug is not common in AOF files, it is almost impossible for an RDB to have this bug in comparison.

RDB and AOF, which one should I use?

If the data does not have security requirements. It is recommended to turn off the persistence feature, if very high recommendations are turned on, the tradeoff is to turn on aof

Persistent reference link for Redis above: http://my.oschina.net/davehe/blog/174662

Note: Persistence itself cannot replace backups; You should also develop a backup strategy that periodically backs up the Redis database

RDB and AOF are enabled at the same time

(1) Bgsave and bgrewriteaof do not execute simultaneously

(2) when the Redis server starts to recover data, it takes precedence over aof

To change the appendonly no parameter to Yes in the configuration file, you can turn aof persistence on, appendfilename specify AOF file name

No-appendfsync-on-rewrite If the new write operation is temporarily in memory when overriding the AoF file, the default is no

Auto-aof-rewrite-percentage the current aof file size is twice times the size of the last rewrite AoF file is automatically overridden for the current aof file

Auto-aof-rewrite-min-size setting the size of the current aof file to be overridden at least to a specified size

Copy:

1). The same master can synchronize multiple slaves

2). Slave can also accept other slaves connection and synchronization requests, which can effectively load the sync pressure of master

3). Master Server provides services for slaves in a non-blocking manner. So during Master-slave synchronization, the client can still submit queries or modify requests

4). Slave Server also completes data synchronization in a non-blocking manner. During synchronization, if a client submits a query request, REDIS returns the data before synchronization

How replication works: After slave is started and connected to master, it will actively send a sync command. Master will then start the background disk process and collect the

There are received commands to modify the dataset, and master will transfer the entire database file to slave to complete a full synchronization once the background process finishes executing. and slave

After receiving the database file data, the server disks it and loads it into memory. After that, Master continues to add all the modified commands that have been collected, and the new modify commands in turn

transfer to Slaves,slave will execute these data modification commands at this time, thus achieving final data synchronization if the link between master and slave appears to be disconnected,

Slave can automatically reconnect master, but once the connection is successful, a full synchronization will be performed automatically.

  

A parameter will be set from the library's configuration file: slaveof 192.168.238.224 6379 Specifies the IP address of the main library, the port to listen on

Use the Info replication command to see if the master-slave synchronization is successful after logging in

From library configuration parameters: Slave-serve-stale-data If you are not connected to the main library from the library, whether to send your own data when there is a read request, the default is Yes

Slave-read-only is read-only from the library, yes is the default

Note: If Master uses Requirepass to turn on the authentication feature, the server will use Masterauth <PASSWORD> to connect to the service request using this password

Certification

Sentinel: for monitoring and managing multiple Redis services implement HA, notifications, automatic failover, require Sentinel to be the number of cardinality, at least three to master whether to do online

The right decision to vote, while also avoiding sentinel single-point failure

Program to start Sentinel redis-sentinel/etc/redis-sentinel.conf

/etc/redis-sentinel.conf Dedicated profile for Sentinel

Launching the Sentinel program will implement the following procedure

(1) Initialization of the server itself, running Redis-server Technical secondary code for Sentinel function

(2) Initialize Sentinel State, initialize the monitored master server list based on the given configuration file

(3) Create a connection to master

Parameters that must be configured in the configuration file:

Sentinel Monitor <master-name> <ip> <redis-port> <quorum>

For example: Sentinel Monitor MyMaster 127.0.0.1 6379 1 quorum is specified with several Sentinel

Sentinel Down-after-milliseconds <master-name> <milliseconds>

For example: Sentinel Down-after-milliseconds MyMaster 30000 30 seconds after the main library is not connected, the main library is determined to be down

Sentinel Parallel-syncs MyMaster 1

How much of the library can be synchronized at the same time when promoted from the library to the primary library

Sentinel Failover-timeout MyMaster 180000

Be sure to configure the above three parameters and then open Sentinel, or it will be troublesome, unable to automatically fail over

Failover Timeout time

Subjective downline: A Sentinel instance that determines the downline of a node

Objective offline: Multiple sentinel node after negotiation to determine a node offline

  

Dedicated commands:

SENTINEL Masters

SENTINEL Slaves <master name>

SENTINEL get-master-addr-by-name <master name>

    

Add: When the main library goes down, Sentinel notifies the client of the new main library IP address (requiring Redis's dedicated client program), and the original main library will be returned to the new master library from

Vault, Sentinel works in a non-daemon process

Redis Learning Notes

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.