The replication function of redis is to support data synchronization between multiple databases. One is the master database (master) and the other is the slave database (slave). The master database can perform read and write operations. When a write operation occurs, the data is automatically synchronized to the slave database. The slave database is generally read-only and receives For data synchronized from the master database, a master database can have multiple slave databases, while a slave database can only have one master database.
Recommended: redis introductory tutorial
The master-slave replication function of redis can effectively separate the reading and writing of the database and improve the load capacity of the server. The main server mainly Responsible for write operations, and the slave server is mainly responsible for read operations
Master-slave replication process:
1: When a slave database is started, a sync command will be sent to the master database ,
2: After receiving the sync command, the main database will start saving the snapshot in the background (performing RDB operations), and cache the commands received during the save period.
3: When the snapshot is completed , redis will send the snapshot file and all cached commands to the slave database.
4: After receiving it from the database, the snapshot file will be loaded and the received cached command will be executed.
Note: Versions before redis 2.8: When the master-slave database is synchronized, the slave database will re-perform the above operation after being disconnected and reconnected due to network reasons, and resuming the download is not supported. Redis2.8 and later support breakpoint resumption.
Note: Starting from Redis 2.8, in order to ensure data security, you can configure min-slaves-to-write to allow a master node to perform write operations only when it has at least N slave nodes. The slave node pings the master node once per second, and the master node records the last time each slave server sent a ping to it. Users can specify the maximum network delay min-slaves-max-lag and the minimum number of slave servers required to perform write operations through configuration
min-slaves-to-write min-slaves-to-write 3 min-slaves-max-lag 10
If there are at least min-slaves-to-write slave servers, And the delay value of these servers is less than min-slaves-max-lag seconds, then the main server will perform the write operation requested by the client. As long as one condition is not met, the write operation will not be performed and the master server will return an error to the client requesting the write operation.
2. Master-slave replication deployment:
Redis master-slave structure supports one master and multiple slaves
Master node: 192.168.1.170
Slave node: 192.168.1.171
Note: The configuration of all slave nodes is the same
Method 1: Manually modify the configuration file
Only additional modifications are required in the slave node The slaveof attribute in the redis configuration file can
slaveof 192.168.1.170 6379
Start redis on the 170 master node and view the redis info information (execute the info command)
Start redis on the 171 slave node
View redis info information
Method 2: Dynamic setting
Connect to the slave node server through redis-cli and execute the following command.
3. Issues that need to be paid attention to in master-slave replication:
① If you use master-slave replication, make sure your master has persistence activated. Or make sure it doesn't automatically restart after crashing. The slave is a full backup of the master, so if the master is restarted with an empty data set, the slave will also be cleared.
② If the master database has a password set when configuring the redis replication function, you need to set the password of the master database through the masterauth parameter in the slave data configuration file, so that the slave database will automatically use it when connecting to the master database. The auth command is authenticated. It is equivalent to a password-free login.
Related recommendations:
mysql video tutorial: https://www.php.cn/course/list/51.html
The above is the detailed content of Introduction to redis master-slave replication. For more information, please follow other related articles on the PHP Chinese website!

Key features of Redis include speed, flexibility and rich data structure support. 1) Speed: Redis is an in-memory database, and read and write operations are almost instantaneous, suitable for cache and session management. 2) Flexibility: Supports multiple data structures, such as strings, lists, collections, etc., which are suitable for complex data processing. 3) Data structure support: provides strings, lists, collections, hash tables, etc., which are suitable for different business needs.

The core function of Redis is a high-performance in-memory data storage and processing system. 1) High-speed data access: Redis stores data in memory and provides microsecond-level read and write speed. 2) Rich data structure: supports strings, lists, collections, etc., and adapts to a variety of application scenarios. 3) Persistence: Persist data to disk through RDB and AOF. 4) Publish subscription: Can be used in message queues or real-time communication systems.

Redis supports a variety of data structures, including: 1. String, suitable for storing single-value data; 2. List, suitable for queues and stacks; 3. Set, used for storing non-duplicate data; 4. Ordered Set, suitable for ranking lists and priority queues; 5. Hash table, suitable for storing object or structured data.

Redis counter is a mechanism that uses Redis key-value pair storage to implement counting operations, including the following steps: creating counter keys, increasing counts, decreasing counts, resetting counts, and obtaining counts. The advantages of Redis counters include fast speed, high concurrency, durability and simplicity and ease of use. It can be used in scenarios such as user access counting, real-time metric tracking, game scores and rankings, and order processing counting.

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.

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

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.

Use of zset in Redis cluster: zset is an ordered collection that associates elements with scores. Sharding strategy: a. Hash sharding: Distribute the hash value according to the zset key. b. Range sharding: divide into ranges according to element scores, and assign each range to different nodes. Read and write operations: a. Read operations: If the zset key belongs to the shard of the current node, it will be processed locally; otherwise, it will be routed to the corresponding shard. b. Write operation: Always routed to shards holding the zset key.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools