


Related operations of Redis master-slave synchronization and read-write separation settings
This article introduces the use of the master-slave synchronization function (master, slave) of Redis to enable the program to separate reading and writing, avoid io bottlenecks, and improve data reading and writing efficiency.
Redis supports one master server to synchronize multiple slave servers, and the synchronization uses the publish/subscribe mechanism.
One master can also be layered for multiple slaves. Each slave can synchronize the slave again and expand it into a tree structure.
Redis master-slave synchronization setting
The default port of Redis is 6379. In order not to affect the original Redis, we use the new port
master Configurationredis_master.conf
port 6300requirepass 123456masterauth 123456daemonize yes
slave1 Configurationredis_slave1.conf Set as master’s slave
port 6301slaveof 127.0.0.1 6300requirepass 123456masterauth 123456daemonize yes
slave2 Configurationredis_slave2.conf Set slave
port 6302slaveof 127.0.0.1 6300requirepass 123456masterauth 123456daemonize yes
daemonize to the master to indicate background startup.
requirepass Password for host authentication.
masterauth Verify the password for the slave to access the host, which needs to be consistent with the host's requirepass.
Since master-slave switching needs to be demonstrated later, the verification passwords of the three sets of conf are the same.
Redis master-slave synchronization test
Start master, slave1, slave2 in sequence
redis-server redis_master.conf redis-server redis_slave1.conf redis-server redis_slave2.conf
After execution, check whether the startup is successful
ps aux|grep redis root 1858 Ss 3:55 0:00.01 redis-server *:6302 root 1849 Ss 3:54 0:00.01 redis-server *:6301 root 1842 Ss 3:54 0:00.02 redis-server *:6300
Entermaster, set the value of key abc to 123
redis-cli -p 6300127.0.0.1:6300> auth 123456OK127.0.0.1:6300> set abc 123OK127.0.0.1:6300> get abc"123"
Enter slave1 and slave2 respectively to check whether the data is synchronized
slave1:
redis-cli -p 6301 127.0.0.1:6301> auth 123456OK127.0.0.1:6301> get abc"123" 127.0.0.1:6301>
slave2:
redis-cli -p 6302 127.0.0.1:6302> auth 123456OK127.0.0.1:6302> get abc"123" 127.0.0.1:6302>
EntermasterModify the value of key abc to 456
127.0.0.1:6300> set abc 456OK127.0.0.1:6300> get abc"456"
Check whether slave1 and slave2 are synchronized
slave1:
127.0.0.1:6301> get abc"456"
slave2:
127.0.0.1:6302> get abc"456"
Redis master-slave switching
During the operation process, if there is a problem with the master, we can set up another slave The machine is automatically set to master. The sentinel function of Redis is mainly used here to implement master-slave switching.
sentinel1.conf
port 26301sentinel monitor master 127.0.0.1 6300 2sentinel auth-pass master 123456logfile "/tmp/sentinel.log"daemonize yes
sentinel2.conf
port 26302sentinel monitor master 127.0.0.1 6300 2sentinel auth-pass master 123456logfile "/tmp/sentinel.log"daemonize yes
sentinel monitor master 127.0.0.1 6300 2# The 2 in ## indicates that master-slave switching will be performed only when more than 2 sentinel services detect master failure.
Start two sentinel processes
redis-server sentinel1.conf --sentinel redis-server sentinel2.conf --sentinel ps aux|grep redis root 2643 Ss 4:28 0:00.02 redis-server *:26302 [sentinel] root 2636 Ss 4:28 0:00.02 redis-server *:26301 [sentinel]
Running mode=sentinel, port=26301. Sentinel ID is 3a23343948cd7f26662ccba1d01b92955311ef52 +monitor master master 127.0.0.1 6300 quorum 2+slave slave 127.0.0.1:6301 127.0.0.1 6301 @ master 127.0.0.1 6300+slave slave 127.0.0.1:6302 127.0.0.1 6302 @ master 127.0.0.1 6300Running mode=sentinel, port=26302. Sentinel ID is ce0ee2af6b454205a3e475763945f505a10a7d6a +monitor master master 127.0.0.1 6300 quorum 2+slave slave 127.0.0.1:6301 127.0.0.1 6301 @ master 127.0.0.1 6300+slave slave 127.0.0.1:6302 127.0.0.1 6302 @ master 127.0.0.1 6300+sentinel sentinel 3a23343948cd7f26662ccba1d01b92955311ef52 127.0.0.1 26301 @ master 127.0.0.1 6300+sentinel sentinel ce0ee2af6b454205a3e475763945f505a10a7d6a 127.0.0.1 26302 @ master 127.0.0.1 6300
Terminate the master, test the master and slave After switching the
kill master process, sentinel determines that the master is invalid and performs master-slave switching processing.
The log is as follows:+failover-state-reconf-slaves master master 127.0.0.1 6300+slave-reconf-sent slave 127.0.0.1:6301 127.0.0.1 6301 @ master 127.0.0.1 6300+config-update-from sentinel 3a23343948cd7f26662ccba1d01b92955311ef52 127.0.0.1 26301 +switch-master master 127.0.0.1 6300 127.0.0.1 6302+slave slave 127.0.0.1:6301 127.0.0.1 6301 @ master 127.0.0.1 6302+slave slave 127.0.0.1:6300 127.0.0.1 6300 @ master 127.0.0.1 6302-odown master master 127.0.0.1 6300+slave-reconf-inprog slave 127.0.0.1:6301 127.0.0.1 6301 @ master 127.0.0.1 6300+slave-reconf-done slave 127.0.0.1:6301 127.0.0.1 6301 @ master 127.0.0.1 6300+failover-end master master 127.0.0.1 6300+switch-master master 127.0.0.1 6300 127.0.0.1 6302+convert-to-slave slave 127.0.0.1:6300 127.0.0.1 6300 @ master 127.0.0.1 6302
As can be seen from the log, the master-slave switch performed the following operations:
1. Switchslave2 For the new master, slaveof 127.0.0.1 6300 in redis_slave2.conf is automatically deleted.
2. Automatically update slaveof 127.0.0.1 6300 in redis_slave1.conf to slaveof 127.0.0.1 6302, and use slave2 as the new master. 3.After the original master is restarted, it will be used as a slave, and slaveof 127.0.0.1 6302 will be added to redis_master.conf automatically.
Conduct master-slave synchronization test after restarting the original master
Original masterUpdate key abc to 888, because it is now a slave. So the update failed.
127.0.0.1:6300> set abc 888(error) READONLY You can't write against a read only slave.
slave2 Update key abc to 888
127.0.0.1:6302> set abc 888OK127.0.0.1:6302> get abc"888"
Original master
127.0.0.1:6300> get abc"888"
slave1
127.0.0.1:6301> get abc"888"
Introducing the method of mysql rebuilding table partition and retaining data
Relevant content of PHP generating unique RequestID class
php json_encode does not support the solution to object private attributes
The above is the detailed content of Related operations of Redis master-slave synchronization and read-write separation settings. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

How to clear Redis data: Use the FLUSHALL command to clear all key values. Use the FLUSHDB command to clear the key value of the currently selected database. Use SELECT to switch databases, and then use FLUSHDB to clear multiple databases. Use the DEL command to delete a specific key. Use the redis-cli tool to clear the data.

To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.

On CentOS systems, you can limit the execution time of Lua scripts by modifying Redis configuration files or using Redis commands to prevent malicious scripts from consuming too much resources. Method 1: Modify the Redis configuration file and locate the Redis configuration file: The Redis configuration file is usually located in /etc/redis/redis.conf. Edit configuration file: Open the configuration file using a text editor (such as vi or nano): sudovi/etc/redis/redis.conf Set the Lua script execution time limit: Add or modify the following lines in the configuration file to set the maximum execution time of the Lua script (unit: milliseconds)

Using the Redis directive requires the following steps: Open the Redis client. Enter the command (verb key value). Provides the required parameters (varies from instruction to instruction). Press Enter to execute the command. Redis returns a response indicating the result of the operation (usually OK or -ERR).

Using Redis to lock operations requires obtaining the lock through the SETNX command, and then using the EXPIRE command to set the expiration time. The specific steps are: (1) Use the SETNX command to try to set a key-value pair; (2) Use the EXPIRE command to set the expiration time for the lock; (3) Use the DEL command to delete the lock when the lock is no longer needed.

Use the Redis command line tool (redis-cli) to manage and operate Redis through the following steps: Connect to the server, specify the address and port. Send commands to the server using the command name and parameters. Use the HELP command to view help information for a specific command. Use the QUIT command to exit the command line tool.

There are two types of Redis data expiration strategies: periodic deletion: periodic scan to delete the expired key, which can be set through expired-time-cap-remove-count and expired-time-cap-remove-delay parameters. Lazy Deletion: Check for deletion expired keys only when keys are read or written. They can be set through lazyfree-lazy-eviction, lazyfree-lazy-expire, lazyfree-lazy-user-del parameters.
