Home > database >  Redis things, persistence, replication
Redis things, persistence, replication

Time:10-17

Redis transactions can guarantee will not be interrupted, can't guarantee the rollback,

Multi

Op

Op

Op

The exec

Multi and exec main effect is to remove competition conditions, and can reduce the number of communications between the client and server, improve performance,

$setkey value

$expire key 100

$TTL key

Redis provides two ways of data persistence, snapshot RDB and AOF additional files, only the two can selectively use persistent way,

The save 60 1000

Rdbcompression yes

Dbfilename dump.

RDB

Appendonly no

Appendfsync everysec

No - appendfsync - on - rewrite no

Auto - aof - rewrite - percentage with 100

Auto - aof - rewrite - min - the size of 64 MB

Dir.

Above the persistent configuration is simple, persistence for snapshot files, may be lost after a recent create snapshots to write all of the data, the snapshot files can be triggered by the following way,

1 the client send Redis BGSAVE - (background save) command to create the snapshot,

2 the client sends a SAVE the Redis command to create a snapshot, Redis server in execution,

3 if the configuration save 60 10000 said 60 seconds 10000 this write will trigger BGSAVE,

4 when Redis closed by a SHUTDOWN command receiving server request, or receives the TERM, will perform a SAVE command,

5 when a Redis server connection, another Redis server and send each other the SYNC command to start a copy operation, if the primary server did not perform BGSAVE operations, or the primary server is not just finished performing BGSAVE operation, then the primary server will perform BGSAVE operations,

Use snapshot may lose data, need to be careful to use, best determine how long it will take to lost data, to set up the Save operation, the snapshot cannot too frequent, also cannot too few and far between, such as when we processing log files, 1000 line can't deal with the progress of the deposited in the Redis, even if the program crashes, we can continue to perform before the unfinished work, we can also use the transaction line to progress and the processing result in the record to a file at the same time,

When Redis stored data are only a few GB, there is no question of using snapshot to SAVE the data, may be more than we read this sentence of time is short, because BGSAVE opens a child process to a snapshot of writing, so you need memory remaining, so when Redis after tens of gigabytes of data storage, the remaining memory is not enough cases, BGSAVE long pause, may cause the system can also cause a lot of the use of virtual memory, leads to lower performance Redis to the point of not be able to use, the cause of the pause and Redis is directly proportional to how much data is stored in the memory, in a 68 GB of Xen virtual machine, to a take up 50 GB of memory Redis server performs GBSAVE command, create the child alone will take more than 15 seconds, and generate a snapshot files need 15-20 minutes more, then we can consider to use the SAVE command, take 3-5 minutes to complete snapshot generated work, for the snapshot number less frequent program, we can in the case of not busy executive script to invoke the SAVE command, then backup generated snapshot, and then continue to continue to perform the operation, for the client

Again, if you allow the loss of data, then using the snapshot is a good choice, otherwise can use AOF to will be stored in memory data saved to the hard disk, as soon as possible

File is written to=write buffer + notice operating system written to the hard disk + written to disk, as soon as possible

Use Redis proposal set to synchronize every second, and the performance of this and do not use the persistence, this will ensure that even if the program crashes have only lost a second data, but AOF the file size is a problem, first of all, may explode disk, very slow, also makes the fault reset at this time we can send Redis BGREWRITEAOF to remove redundancy in AOF file command, it will also open a background process to execute the rewritten, we also can set auto - AOF - rewrite - percentage and auto - AOF - rewrite - min - size to automatically perform BGREWRITEAOF,

Persistence is necessary, but we must make a backup, the persistent file generated best backup to a number of different places, so as to avoid data loss, it is best to snapshot files and aof file backup to a different server,

In addition to the persistent, copy is also a major feature of Redis, and relational database, the Redis is also the main server to send updates from the server, and use the read from the server to process all requests, though, Redis outstanding performance, but also can meet can't rapid processing condition, especially at the time of collection and an ordered set of operations that involve elements may have tens of thousands or even millions, in this case, the time it takes to perform operation may need in seconds to calculate, in general, a normal 2.4 GHZ 2 processor on the server for a collection of two 10000 elements SUNIONSTORE takes 7-8 milliseconds,

We can use SLAVEOF host port for slave to accept the primary data update, connections from the server after the primary server sends the SYNC command, the main server performs BGSAVE and to write all write into the buffer, send BGSAVE, then write command send buffer, the buffer to write to send, from now on, each performing a write command, will be the same write command sent from the server, need buffer memory, so it is recommended that the master server USES only 50% 65% of memory, the memory used to perform BGSAVE and create records write command buffer,

Using SLAVEOF host port or using SALVEOF commands are available with master-slave relationship, if the former, then Redis will be loaded at boot time any snapshot or AOF files are available, and then execute the replication process, Redis does not support the main master replicate, when from the server and from the server, the sending BGSAVE file, the causes from the server from the server disconnected reconnection,

When read requests are significantly higher than the importance of writing, we can create a tree structure of master-slave chain, to realize the replication, through the use of AOF and copy, we can in the case of lost at least, the data synchronization to multiple servers,

Whether we need to check whether the copy content has already written to the hard disk, first we need to use master_link_status whether it is up to judge whether the connection, in addition to judge whether the data sync from the server, generally we are written to the real data to the master server and then write a the only fictitious value, see this fictitious value can be reached from the server, how to determine whether the data is written to disk, this is a bit difficult, use the INFO command to see if aof_pending_bio_fsync attribute to 0, if is 0, that means that the server has known all data written into the hard disk,

View the INFO in a server-side comprehensive state can use Redis command to complete,

Data security & amp; Performance guarantee

If we want to Redis as the only means of data storage, we are going to make sure the Redis does not lose any data, Redis not like a traditional relational database with ACID to ensure reliable data transaction, Redis is just a piece of software, even if the hardware and software design of perfect, also may appear blackout emergency, such as

When a system failure, we should accept check and dumpfile aof file directory, and through the redis - check - aof behind plus - fix parameters, the program will scan for aof file, and found the wrong command will delete the command after all commands, but a snapshot files cannot be repaired, so we'd better more than snapshot files to backup,

Have a backup, we are encountering the primary server when we need to be replaced, for SAVE command from the service B first, generate a new snapshot files, then send the snapshot file to the machine C, and start on the machine C Redis, finally make B C from the server, another method is to upgrade from the server is given priority to the server, and then to create a new master server from the server, of course, these operations are man-made, can go wrong, we can use the Redis Sentinel surveillance designated Redis master server and staff from the server, and in the main server offline automatic failover,

In order to guarantee the correctness of the data, we need to use Redis transactions, Redis transactions and traditional relational database is not the same, the traditional relational database support rolled back, but Redis only guarantee multiple serial execution is not interrupted, read and write operations under below we learn by a deal to Redis transactions, assume we have a demand, the demand is to allow players to sell their props, players will be their own game props in the market, if someone bought, then the player's account funding increases will respond, buy the player will reduce capital,

User: 8888 - & gt; {id: 8888, name: Jerry, as: 43,}

The inventory

: 8888 - & gt;

The item

: 1001,

The item

: 1002,

The item

: 1003

]

Market

: - & gt;

The item

: | 58, 1004-8888

]

In the above data structure, the product id and player id are pieced together, and according to the selling price will be in the market for the sorting, very suitable for paging and sorting, in Redis transactions, we often use the following command MULTI/EXEC, WATCH, UNWATCH/DISCARD command, can UNWATCH command from the transaction before MULTI queue removed, this can be in MULTI and EXEC command from the transaction between the queue removed, in order to ensure that atomic operation is put in the queue, we need to monitor the player package elements in change, only when the props in the user package, subsequent to remove items from the player's package to the market, otherwise, the item will be moved to the market operation is invalid,

Whilenotexpired

Try

Watch the inventory

If notismember itemid of the inventory

Unwatch

Return

Multi

Market: the add item

The inventory rem item

The exec

Returntrue

The exception

Passreturn false

The same purchase behavior also need to monitor the market: and the personal information of the buyer, if the user's information has not been modified, and the market: the content has not been modified, and the buyer have enough money, so will perform purchase logic,

While notexpired//that method can exit after a certain time, the try

Watch market: buyer

If the item price changed or buyer for the to buy

Unwatch market: buyer

Return

Multi

//logic here
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull