Redis Primer Notes (2)
The previous article describes the basics of Redis and the supported data types, and this article describes Redis persistence, master-slave replication, simple transactional support, and publishing subscription capabilities.
Persistence of
Redis is an in-memory database that supports persistence, which means that Redis often needs to synchronize in-memory data to disk to ensure persistence, which is a big advantage relative to memcache. Redis supports two persistence modes, one is snapshotting (snapshot) is the default, and the other is the way Append-only file (abbreviated AOF). snapshotting
Snapshots are the default persistence mode. This way, the in-memory data is written to the binary in a snapshot, and the default file name is Dump.rdb. You can configure how automatic snapshot persistence is done. We can configure Redis to take snapshots automatically if more than M key is modified in n seconds, the following is the default snapshot save configuration
Save 1 #900秒内如果超过1个key被修改 to initiate a snapshot save
Save #300秒内容如超过10个key被修改, the snapshot save is initiated
Save 60 10000
append-only File
AoF is more persistent than snapshot mode, because Redis appends each received write command to a file by using the Write function (default is appendonly.aof) when aof persistence is used. When Redis restarts, the contents of the entire database are rebuilt in memory by re-executing the write commands saved in the file. Of course, because the OS caches write modifications in the kernel, it may not be written to disk immediately. The persistence of this aof method is also likely to lose some of the modifications. But we can tell Redis through the configuration file that we want to force the OS to write to disk through the Fsync function. There are three ways to do this (the default is: Fsync once per second)
appendonly Yes//enable AOF persistence mode
# Appendfsync always//write command is immediately forced to write to disk, the slowest, but guaranteed full persistence, not recommended
Appendfsync everysec//force write disk once per second, a good compromise in performance and persistence, recommended
# Appendfsync no//completely dependent on OS, best performance, no guarantee of persistence
Master-slave replication
• Master-slave replication allows multiple slave servers to have the same database copy as master server. Here are some of the features of Redis master-slave replication–1.master can have multiple slave–2. In addition to multiple slave connected to the same master, slave can also connect other slave to form a graphic structure–3. Master-slave replication does not block primary. This means that when one or more slave and master synchronize data for the first time, master can continue to process requests from the client. Instead, slave is blocked when the data is first synchronized and cannot process client requests. –4. Master-slave replication can be used to improve the scalability of the system (we can use multiple slave dedicated to client read requests, such as the sort operation can be handled using slave), or can be used to do simple data redundancy.
-5. You can disable data persistence in master, just comment out all the save configurations in the master configuration file and configure data persistence on slave only.
Things
Redis support for transactions is now relatively straightforward. Redis can only guarantee that commands in one client-initiated transaction can be executed consecutively, but not in the middle of the other client's commands. –multi things start –exec Executing a transaction –discard give up things –watch monitoring key –unwatch to discard all key monitoring The Watch command monitors the given key, and the entire transaction fails if the monitored key has changed since the call to watch. Note that watch's key is valid for the entire connection, and as with transactions, monitoring and transactions are automatically purged if the connection is broken. Publish a Subscription (Pub/sub ) • Publish Subscription (pub/sub) is a message communication pattern. Subscribers can subscribe to Redis server with the subscribe and Psubscribe commands for the type of message they are interested in, and Redis calls the message type channel. When a publisher sends a specific type of message to Redis server through the Publish command. All clients subscribing to this message type receive this message. The delivery of the message here is many-to-many. A client can subscribe to multiple channel, or it can send messages to multiple channel.
–subscribe–unsubscribe–psubscribe–punsubscribe–publish Pipe (Pipeline) Redis is a cs-mode TCP server that uses a similar request-response protocol to HTTP. A client can initiate multiple request commands from a single socket connection. After each request command is issued, the client usually blocks and waits for the Redis service to process, and the result is returned to the client via a response message after Redis finishes processing the request. The basic communication process is as follows
CLIENT:INCR X
Server:1
CLIENT:INCR X
Server:2
CLIENT:INCR X
Server:3
CLIENT:INCR X
Server:4
• Basically four commands require 8 TCP messages to complete. Due to the network latency of the communication, the packet transfer time between the client and server takes 0.125 seconds. Then the above four commands 8 messages will take at least 1 seconds to complete. the pipeline is used to package multiple commands from the client and does not need to wait for the response of a single command to return, and the Redis server will package the results of multiple commands together and return them to the client after processing multiple commands. The communication process is as follows
CLIENT:INCR X
CLIENT:INCR X
CLIENT:INCR X
CLIENT:INCR X
server:1
Server:2
Server:3
Server:4 virtual Memory (VM)The VM's author has abandoned the featureRedis does not use the virtual memory mechanism provided by the OS but implements its own virtual memory mechanism, but the idea and purpose are the same. is to temporarily swap infrequently accessed data from memory to disk, freeing up memory space for other data that needs to be accessed. Especially for memory databases such as Redis, memory is never enough. In addition to splitting the data out to multiple Redis servers. Another way to increase the capacity of a database is to use VMS to swap data that is infrequently accessed on the disk. If our stored data is always accessed with a small portion of the data, most of the data is rarely accessed, and it is true that only a small number of users are often active on the site. When a small amount of data is accessed frequently, the use of VMs not only increases the capacity of a single Redis server database, but also does not affect performance too much. –vm-enabled Yes #开启vm功能–vm-swap-file/tmp/redis.swap #交换的value保存的文件路径/tmp/redis.swap–vm-max-memory 1000000 #最大内存上限, after which the value to disk file has been exchanged–vm-page-size #每个页面的大小32个字节–vm-pages 134217728 #最多使用在文件中使用多少页面vm-max-threads 4 #用于执行value对象换入换出的工作线程数量, 0 means no worker threads are used
Redis Primer Notes (2)