Redis Learning (6)-redis Advanced Practical Features

Source: Internet
Author: User
Tags configuration settings strong password redis server

Redis Advanced Utility Features:

1. Security
2. master-slave replication
3. Transaction processing
4. Persistence mechanism
5. Publish a subscription message
6. Use of virtual memory

Security:

Set the password warning to be used before any other specified client connection is made: Because Redis is very fast, the next external user of a better server can make a 150k password attempt in a second, which means you need to specify a very strong password to prevent brute force.
Configuration method:
Requirepass Beijing (Configure password in config file)
Auth Beijing (Authorization mode 1)
REDIS-CLI-A Beijing (Authorization mode 2)

Master-slave replication:

Redis Master-slave replication is simple to configure and use, and master-slave replication allows multiple Redis servers to have the same database copy as master server

Redis master-slave replication features:
1.Master can have multiple slave
2. Multiple slave can connect to the same master, and can also connect to other slave
3. Master-slave replication does not block primary, while synchronizing data, Master can continue to process client requests
4. Improve the scalability of the system

Redis master-slave replication process:
1.Slave Connect with master, send sync Sync command
2.Master initiates 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 has finished saving, send this file to slave
4.slave saving files to your hard disk

Configure the master-slave server:
Configuring the slave server is simple, just add the configuration in the slave configuration file:
Slaveof 192.168.1.1 6379 #指定master的IP和端口
Masterauth Lamp #这是主机的密码

Transaction processing:

redis's support for things is now relatively simple, Redis Smart guarantees that a command in a client-initiated transaction can be executed consecutively without inserting a command from the client, and when a client issues a multi command in a connection, the connection goes into a transaction context and subsequent commands are not immediately executed. Instead, put it in a queue first. When you execute the EXEC command, Redis executes all the commands in the queue sequentially,

Add a transaction
Command:
Multi
Ok
INCR A
QUEUED
INCR b QUEUED
Exec
1) 1
2) 1

How to cancel a transaction (rollback)
Dicard
If one is unsuccessful, it does not affect the action above
INCR Age
INCR name #不成功
Exec

Optimistic locking complex transaction control
Optimistic lock: Most are implemented based on the data version of the record mechanism, that is, to add a version of the identity of the data, in a version based on the database table solution, typically by adding a "version" field for the database table to read out the data, the version number is read together, When updated, this version number is added 1, at which point the version number of the submitted data is compared with the current version number of the corresponding record of the data table, if the submitted version number is greater than the current version number of the database, it is updated, otherwise it is considered to be outdated data. The watch command monitors the given key, and when exec does, if the monitored key has changed since the call to watch, the entire transaction fails, and watch monitors multiple keys multiple times, allowing the specified key to be optimistic. Note that watch's key is valid for the entire connection, and the transaction is the same, if the connection is broken, monitoring and transactions are automatically cleared, of course the Exec,discard,unwatch command will know all the monitoring in the connection

Persistence mechanism:

redis is a persistent memory database, This means that Redis often needs to synchronize the in-memory data to the hard disk to ensure that persistent
Redis supports two persistence modes:
1.snapshotting (snapshot) is the default way
2.append-only file (abbreviated AOF)

snapshotting Way The
snapshot is the default persistence method, in which the in-memory data is written to the binary in a snapshot, the default file name Dump.rdb can be automatically snapshot persisted by configuration settings, and we can configure Redis to automatically snapshot if more than M key modifications are made in n seconds

How to configure:
Save 1 #900秒内如果超过1个key被修改 to initiate a snapshot save
Save #300秒内容如果超过10个key被修改, the snapshot save is initiated
Save 60 10000

AoF Way
Because the snapshot is done at a certain interval, if Redis accidentally falls down, all modifications after the last snapshot are lost. AOF is more persistent than snapshot, because Redis appends each received write command to a file by using the Write function, and when Redis restarts, it rebuilds the contents of the database in memory by re-executing the Write command saved in the file. Of course, since the OS caches write modifications in the kernel, it may not be written to disk immediately, so the persistence of the AOF mode is still likely to be lost with the modification of Buffon. You can tell Redis through the configuration file that we want to force the OS to write to disk through the Fsync function.

How to configure:
AppendOnly Yes//enable AOF persistence mode
#appendfsync always//Receive write commands to write to disk at the slowest, but guarantee full persistence
Appendfsync everysec//write disk once per second, a good compromise in performance and persistence
#appendfync no//full OS dependent, best performance persistence no guarantee

Publish and subscribe to messages:

A publish Subscription (PUB/SUB) is a message communication pattern that is primarily designed to decouple the coupling between a message publisher and a message subscriber, which is similar to the observer pattern in design patterns. Pub/sub not only solves the direct code level coupling between publishers and subscribers, but also solves the coupling between the two on the physical deployment. As a pub/sub server, Redis serves as a message routing feature between subscribers and publishers. 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.

Virtual Memory:

Redis's virtual memory is not the same as the operating system's virtual memory, but the idea and purpose are the same. is to temporarily swap infrequently accessed data from memory to disk, freeing up valuable memory 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 into multiple
Redisserver outside. Another way to improve database capacity is to use virtual memory to swap infrequently accessed data to disk.

Configuration:
vm-enabled Yes #开启vm功能
Vm-swap-file/tmp/redis.swap #交换出来的value保存的文件路径
Vm-max-memory 1000000 #redis使用的最大内存上限
Vm-page-size #每个页面的大小32字节
Vm-pages 13417728 #最多使用多少页面
Vm-max-threads 4 #用于执行value对象换入患处的工作线程数量

Prompt after startup, also configure:
REALLY-USE-VM Yes

Note: The Redis series blog is a combination of brother and Redis learning video notes!

Redis Learning (6)-redis Advanced Practical Features

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.