


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]Redis log can be seen, the startup is successful and start monitoring
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, slave1 checks whether it is synchronized
Original master
127.0.0.1:6300> get abc"888"
slave1
127.0.0.1:6301> get abc"888"After inspection, after the master-slave switch, slave2 serves as the new master and other servers serve as slaves and can be used normally. This article explains the related operations of Redis master-slave synchronization and read-write separation settings. For more related content, please pay attention to the PHP Chinese website. related suggestion:
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!

Load balancing affects session management, but can be resolved with session replication, session stickiness, and centralized session storage. 1. Session Replication Copy session data between servers. 2. Session stickiness directs user requests to the same server. 3. Centralized session storage uses independent servers such as Redis to store session data to ensure data sharing.

Sessionlockingisatechniqueusedtoensureauser'ssessionremainsexclusivetooneuseratatime.Itiscrucialforpreventingdatacorruptionandsecuritybreachesinmulti-userapplications.Sessionlockingisimplementedusingserver-sidelockingmechanisms,suchasReentrantLockinJ

Alternatives to PHP sessions include Cookies, Token-based Authentication, Database-based Sessions, and Redis/Memcached. 1.Cookies manage sessions by storing data on the client, which is simple but low in security. 2.Token-based Authentication uses tokens to verify users, which is highly secure but requires additional logic. 3.Database-basedSessions stores data in the database, which has good scalability but may affect performance. 4. Redis/Memcached uses distributed cache to improve performance and scalability, but requires additional matching

Sessionhijacking refers to an attacker impersonating a user by obtaining the user's sessionID. Prevention methods include: 1) encrypting communication using HTTPS; 2) verifying the source of the sessionID; 3) using a secure sessionID generation algorithm; 4) regularly updating the sessionID.

The article discusses PHP, detailing its full form, main uses in web development, comparison with Python and Java, and its ease of learning for beginners.

PHP handles form data using $\_POST and $\_GET superglobals, with security ensured through validation, sanitization, and secure database interactions.

The article compares PHP and ASP.NET, focusing on their suitability for large-scale web applications, performance differences, and security features. Both are viable for large projects, but PHP is open-source and platform-independent, while ASP.NET,

PHP's case sensitivity varies: functions are insensitive, while variables and classes are sensitive. Best practices include consistent naming and using case-insensitive functions for comparisons.


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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1
Powerful PHP integrated development environment

Atom editor mac version download
The most popular open source editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
