(reproduced). NET using Redis (iii) Redis master-slave replication

Source: Internet
Author: User

Redis, like MySQL, has a very powerful master-slave replication feature, and it also supports a master that can have multiple slave, while a slave can have multiple slave to form a powerful multi-level server cluster architecture.
         
Redis's master-slave replication is asynchronous and does not affect master's operation, so it does not degrade the processing performance of Redis. In the master-slave architecture, you can consider turning off the data persistence feature of master and only allow slave to persist, which can improve the processing performance of the primary server. At the same time slave is read-only mode, which avoids slave cached data being modified by mistake.

 1. Configuration

In actual production, the master-slave architecture installs the appropriate Redis services on several different servers. For testing convenience, the configuration of the master and slave backup on my side is tested on my Windows native computer.

1. Install two Redis instances, master and slave. Set the master port to 6379,slave Port set to 6380. Bind is set to: 127.0.0.1. For specific Redis installation steps, refer to the previous post, "Redis Summary (i) Redis installation".

    

2. In slave instance, add: slaveof 127.0.0.1 6379 configuration. As shown in the following:

    

After the configuration is complete, start these two instances, if the output is as follows, the master-slave copy of the schema has been configured successfully.

    

Note: Test on the same computer, master and slave not the same port, otherwise it is not possible to start two instances at the same time.

 2. Testing

On the command line, connect the master server and the slave server respectively. The master is then written to the cache and then read in the slave. As shown in the following:

     
 

called in 3.c#

The master-slave architecture of the Redis read and write is almost the same as a single redis, only a partial configuration and read to distinguish between master and slave, if not clear how to use Redis in C #, please refer to my article, "Redis Summary (ii) How to use Redis in C #."

It is important to note that the Getclient () method in Servicestack.redis can only get the connection in master Redis and not the slave readonly connection. This slave the role of redundant backup, the function of reading does not play out, if there are too many concurrent requests, the performance of Redis will be affected.

So, we need to write and read the time to make a distinction, write the time, call the client. Getclient () to get the Redis link for Writehosts's master. Read, the client is called. Getreadonlyclient () to get the Redis link for the readonlyhost slave.

Or you can use the client directly. Getcacheclient () to get a connection, he will write when the call getclient get the connection, read when the call getreadonlyclient to obtain a connection, so that can be read and write separation, thus leveraging the master-slave copy of Redis function.

1. configuration file App. Config

    <!--Redis Start-to-    <add key= "sessionexpireminutes" value= ""/> <add key=    "Redis_ Server_master_session "value=" 127.0.0.1:6379 "/>    <add key=" redis_server_slave_session "value=" 127.0.0.1:6380 "/>    <add key=" Redis_max_read_pool "value=" "/> <add key=    " Redis_max_write_pool "Value="/>    <!--redis end-->

2. Common class Rediscachehelper for Redis operations

View Code

(reproduced). NET using Redis (iii) Redis master-slave replication

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.