Data security and performance assurance for Redis

Source: Internet
Author: User
Tags redis redis server
Persistence Options

directory where persistent files are stored:

Dir./
snapshot Persistence (snapshotting)

Store all data to disk
Common configurations:

Save
stop-writes-on-bgsave-error No
rdbcompression Yes
dbfilename Dump.rdb

There are several ways to create a snapshot:
1,bgsave command, will fork out a process
The 2,save command, which causes the client to respond before completion,
3. Configure the Save entry
4,redis when closed via shutdown
5, when one Redis is connected to another Redis, the file is usually appended with the master-slave copy (AOF)

Store all executed commands to disk

AppendOnly no
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage
Auto-aof-rewrite-min-size 64MB

Appendfsync Options and Sync frequency

Options Sync Frequency
Always Each Redis command is synchronized to the hard disk. This can seriously affect the speed of Redis
Everysec Perform synchronization once per second
No Let the operating system decide when to synchronize

There is a problem, however, that the aof file is getting bigger, even larger than the snapshot file. To solve this problem, you can use Bgrewriteaof, which, like Bgsave, creates a child process to rewrite the aof file and remove redundant commands. As with snapshot persistence, aof can also set the Auto-aof-rewrite-percent option and auto-aof-rewrite-min-size to automate the execution of bgrewriteaof.

either way, it's not enough to keep your data safe. So if the conditions permit, it is better to back up the data to another server, or the database. redis replication master-slave replication

Experienced engineers know that replication (replication) is a missed lesson or missing. Replication allows other servers to have copies of the data. Redis also has this ability.
Very simple:
You need to set the Dir and Dbfilename options.
Then install two Redis servers, select one as the standby, modify its redis.conf

Slaveof <masterip> <masterport>

, turn on Master Redis, and then turn it on from Redis. This way your data on the host can be backed up to the slave redis. (It is not allowed to write data from Redis, and all data previously owned will be discarded from Reids)
It does not require much configuration to turn on Redis's master-slave replication, but it is interesting and useful to know how the process works.

Steps to connect to the master server from the server

Steps operation of the master server operations from the server
1 Wait for the command to enter Connect (or reconnect) the master server and send the sync command
2 Start execution of Bgsave and use buffer to record all write commands executed after Bgsave Depending on the configuration options, decide whether to continue using the existing data (if any) to process the client request or the client that wants to send the request returns an error
3 Bgsave executes, starts sending snapshot files to the server, and continues all write commands executed with the buffer record during the send Discard old data and start loading snapshot files from the master server
4 The snapshot file is sent and the write commands stored in the cache are sent to the slave server Complete the interpretation of the snapshot file and accept the command request as usual
5 The Buffer Store command is sent, and from now on, without executing a write command, send the same write command to the server. Executes all write commands from the primary server that are stored in the cache, and from now on, receives and executes each write command sent from the primary server.

Warning:
1,redis does not support primary master replication (Master-master replication).
2, all data will be erased from the server.

When multiple slave servers try to connect to the same primary server, the situation becomes different

When
there is a new connection from the server to the master server operation of the master server
Step 3 of the previous table has not been executed All from the server will receive the same snapshot file and the same buffer command
Step 3 of the previous table is executing or has been executed After the master server finishes the 5 steps in the previous table, proceed from step 1 to step 5
master-slave chain

Redis has nothing particularly different from the server and the primary server, so the server can also own it from the server.
Note, however, that the procedure is: if you have slave y from server x, when you perform step 4 o'clock from Server X, he disconnects from Y, causing the need to reconnect and resynchronize from server Y.
When a read request is much more important than a write request, you can create a middle tier composed of master and Slave nodes (master-slave node) to share the primary server's replication effort. Redis Things

We've also introduced things before, using multi and exec to achieve simple things. But there is a problem: users can't make decisions based on the data they read . For example, when placing an order, it is necessary to decide whether or not to place orders based on the amount of inventory you have read.
To solve this problem, we need to know watch, and sometimes we need unwatch and discard commands.

__author__ = ' dubby ' Import redis import Time conn = Redis.
    Redis (host= ' localhost ', port=6379, db=0) def list_item (itemid,sellerid,price): Inventory = "inventory:%s"%sellerid
        item = "%s%s" (itemid,sellerid) end = Time.time () + 5 pip = Conn.pipeline () while Time.time () < end: Try:pip.watch (Inventory) #监视用户包裹发生的变化 if not Pip.sismember (Inventory,itemi

            d): #检查用户是否坚持购买此商品 Pip.unwatch () #如果用户取消购买, cancel monitoring and exit return None Pip.multi () #开始购买 Pip.zadd ("Market:", Item,price) Pip.srem (INV Entory,itemid) Pip.execute () return True except Redis.exception.EatchError: #如果在购 During the purchase, the user package is found to change, retry pass return False 

What is Discard:unwatch can after watch execution, multi executes before the connection is reset, discard can reset the connection before exec executes after multi execution. non-Thing-type assembly line

Pipeline (pipeline) In addition to things, you can also cache commands, reduce the number of interactions with the Redis server, but sometimes do not need things, just need to cache commands, then you can

Pip = Conn.pipline (False)

So we can get a pipeline without things.

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.