Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed explanation of how to realize the Separation of Database read and write by Redis

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Preface

Redis is a kind of NoSQL document database, which is stored in memory through the structure of key-value. The speed of Redis reading is 110000 times / s, and the speed of writing is 81000 times / s. Redis is a key-value storage system. Similar to Memcached, data is cached in memory for efficiency. The difference is that redis will periodically write updated data to disk or modify operations to additional record files, and on this basis to achieve master-slave (master-slave) synchronization. In some cases, it can play a good complementary role to the relational database. It provides clients such as Java,C/C++ (hiredis), PHP, Java, Perl, etc. It is very convenient to use.

Here is an example to achieve the read-write separation of redis. The steps are as follows:

Step 1: download redis

Download address on the official website: https://redis.io/download

Local download address: https://www.jb51.net/softs/541181.html

Download the latest stable version, decompress and copy it to ~ / redis

Compile the code:

$make$ test

Step 2: configure redis

Edit the redis.conf file

Bind 127.0.0.1port 6379

Copy the redis.conf file, rename it to slave.conf, and open the edit

Bind 127.0.0.1port 6380slaveof 127.0.0.1 6379

Step 3: run the service

Open [main service]

$src/redis-server

Open [slave service]

$src/redis-server slave.conf

The client running [main service]

$src/redis-cli

A client running [slave service]

$src/redis-cli-h 127.0.0.1-p 6380

View the relationship between master and slave services

$src/redis-cli info replication

Step 4: test the server

The following example demonstrates that some data is stored in the master server and then queried from the slave server.

It can be seen that the backup data of the main server is successfully obtained from the server.

If we are saving data from the server, what will be the result?

Prompt error:

(error) READONLY You can't write against a read only slave.

Indicates that data can only be read from the server, not written.

The data is read in the slave server and written in the master server.

In this way, the read-write separation function of redis database is realized.

Summary

The above is the whole content of this article, I hope that the content of this article has a certain reference and learning value for your study or work, if you have any questions, you can leave a message and exchange, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report