WHAT IS REDIS MASTER SLAVE?
Redis Master Slave setup is same as any other Master Slave setup. It allows you to maintain multiple copies of same data automatically. That will serve the purpose of backup and scaling. You can use Redis Slave to serve the request the reading requests and hence your master load decreases.
We will start with installation of Redis Server, then Redis Slave and then will setup Redis Mater Slave replication.
About Redis:
Redis is an open source, BSD licensed, advanced key-value cache and store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets, sorted sets, bitmaps and hyperloglogs.
Many languages have Redis bindings, including: ActionScript, C, C++, C#, Clojure, Common Lisp, Dart, Erlang, Go, Haskell, Haxe, Io, Java, JavaScript (Node.js), Lua, Objective-C, Perl, PHP, Pure Data, Python, R, Ruby, Scala, Smalltalk and Tcl.
Install Redis
There are too many ways to install Redis, we will discuss two ways:
- One from source code that works for most of Unix based Operating system and Mac OS X.
- Another using apt package manager(that works on Debian/Ubuntu systems).
If you want to run both Master Slave on same machine and not familiar with Unix Systems; better to do that setup with source code(method1).
Install Redis by Method1(through source code):
Open Shell/Terminal/Command Prompt:
1
2
3
4
| tar xzf redis-2.8.19. tar .gz cd redis-2.8.19 make |
‘make’ will take few seconds or up to 1-2 minutes to finish and your Redis is installed
How to start it?
5
| src /redis-server |
Lets Verify it:
6
7
8
9
10
| src/redis-cli 127.0.0.1:6379> set foo bar OK 127.0.0.1:6379> get foo "bar" |
By default Redis runs on port 6379, you can change it very easily from redis.conf file.
Install Redis by Method2 (using apt package manager):
Open Shell/Terminal/Command Prompt:
1
2
| sudo apt-get update sudo apt-get install redis-server |
Your installation is done and it will start automatically, lets check…
Lets Verify it:
3
4
5
6
7
| redis-cli redis 127.0.0.1:6379> set foo bar OK redis 127.0.0.1:6379> get foo "bar" |
Till this point single instance of Redis is running fine, if you face any issue, please comment below with error message. Next thing is to run Redis in Master Slave mode. It means we will run two instances of Redis. Here we will run both the instances on same machine. But on production you have to do it on different machines, otherwise there is no meaning to setup master slave.
Stop Redis
1
| redis 127.0.0.1:6379>shutdown |
Config changes to convert Redis instance to Redis Master:
Now we will change in config file to run mater slave. Steps are very simple and config properties are self explanatory.
Method1:
1
| vi redis.conf |
Method2:
1
| sudo vi /etc/redis/redis.conf |
Change Listening Interface:
By default Redis listens for connections from all the network interfaces available on the server. It is possible to listen to just one or multiple interfaces using the “bind” configuration directive, followed by one or more IP addresses. like bind 192.168.1.100 10.0.0.1 If the bind option is not specified all the interfaces will listen for incoming connections.
Lets comment it, if not.
Find:
bind 127.0.0.1
Replace with
# bind 127.0.0.1
Security or adding password authentication
We should use redis with password authentication, to make redis connection secure. But it will solved one more issue. Without password sentinels works like charm. But with password authentication in Redis most of the people do same mistake. That is why we are taking password approach, you can skip this step if you don’t want to use Redis with password.
Find:
# requirepass foobared
Replace with
requirepass abc123
Config changes to setup Redis Slave:
copy redis.conf to slave.conf if you want to run master and slave on same machine. Otherwise just use redis.conf on other machine and do the above two steps again on new machine conf file.
1
2
| cp redis.conf slave.conf vi slave.conf |
Now open file slave.conf
Also change the port if both the master and slave are running on same machine. You can’t run two instances with same port in single machine.
Find:
port 6379
Replace with
port 6380
Declare the Master Details:
In slave.conf file set master host and port details, so that master slave can communicate. If on different machine add master machine IP address.
Find:
# slaveof <masterip> <masterport>
Replace with
slaveof 127.0.0.1 6379
Authentication:
Set password that is required by Slave to connect to Master Redis server. Means the password we set earlier for master.
Find
# masterauth <master-password>
Replace with
masterauth abc123
Lets Start the Redis Master Slave
Method1:
1
2
| src /redis-server redis.conf src /redis-server slave.conf |
Method2:
1
2
| /usr/bin/redis-server /etc/redis/redis .conf /usr/bin/redis-server /etc/redis/slave .conf |
Now we will test the Redis master slave setup on shell
1
2
3
4
5
6
7
8
| redis-cli -p 6379 abc123 127.0.0.1:6379> keys * (empty list or set) 127.0.0.1:6379> set foo bar OK 127.0.0.1:6379> get foo "bar" 127.0.0.1:6379> quit |
Now check same on Redis Slave
1
2
3
4
5
6
| redis-cli -p 6380 -a abc123 127.0.0.1:6380> keys * 1) "foo" 127.0.0.1:6380> get foo "bar" 127.0.0.1:6380>quit |
Setup of Redis master slave is done, if you want to run one more slave, just repeat the steps after we copied the redis.conf to slave.conf.
No comments:
Post a Comment