cache Archives - The Linux GURUS https://thelinuxgurus.com/tag/cache/ Learn Linux & DevOPS from THE LINUX GURUS Wed, 06 Jan 2021 17:14:09 +0000 en-US hourly 1 https://i0.wp.com/thelinuxgurus.com/wp-content/uploads/2020/01/cropped-thelinuxgurus_transparent_name.png?fit=32%2C32&ssl=1 cache Archives - The Linux GURUS https://thelinuxgurus.com/tag/cache/ 32 32 148921671 How to setup Redis replication in Linux https://thelinuxgurus.com/how-to-setup-redis-replication-in-linux/ https://thelinuxgurus.com/how-to-setup-redis-replication-in-linux/#respond Wed, 06 Jan 2021 17:14:09 +0000 https://thelinuxgurus.com/?p=1289 One of the features that make REDIS a good caching application is the ability to configure a cluster with one master/primary & one or more...

The post How to setup Redis replication in Linux appeared first on The Linux GURUS.

]]>
One of the features that make REDIS a good caching application is the ability to configure a cluster with one master/primary & one or more slaves/secondary servers. In this tutorial, we will learn to set up redis replication in our Linux servers.

For the purpose of this tutorial, we will be creating a single Primary & a single secondary server. So let’s discuss the pre-requisites for setting up redis replication.

Recommended Read: How to create a free SSL certificate using Let’s Encrypt in Linux

Also Read: How to Schedule a Shutdown in Linux using Crontab?


Pre-Requisites

2 servers with Redis installed, one will act as the Primary server (10.10.10.10 IP address for our scenario) & another will act as the Secondary server (IP address 10.10.10.11 ).

You can read the following tutorials to install Redis in Ubuntu or in CentOS/RHEL. 

Now let’s move on to the configuration part.


Configuring the Redis replication

Primary Server

First, we will start with the Primary server & will configure it. Open the redis configuration file on the primary server. 

Depending on how you have installed it, the configuration file can be ‘/etc/redis/redis/conf’ or ‘/etc/redis/6379.conf’,

$ sudo vi /etc/redis/redis.conf

& look for ‘append’ & ‘appendfilename’, then change it to following,

appendonly yes

appendfilename “appendonly.aof​”

Now restart the redis service to implement the changes made,

$ sudo systemctl restart redis

OR

$ sudo systemctl restart redis_6379 

Now let’s move on to configuring the secondary server.


Secondary Server

Open the redis configuration file on the secondary server, 

$ sudo vi /etc/redis/redis.conf

& look for ‘slaveof’ & change it to following,

slaveof 10.10.10.10 6379

here, ‘10.10.10.10’ is the IP address for the Primary redis server. & ‘6379’ is the port number for redis on that server.  Once changes have been done, restart the service to implement changes,

$ sudo  systemctl restart redis

OR

$ sudo systemctl restart redis_6379

That’s it our setup for redis replication is complete, we will now verify it.


Checking the replication

To check if the master-slave setup is working, connect to the master redis instance using the following command,

$ redis-cli -h 10.10.10.10 -p 6379

 

& run command ‘info’,

10.10.10.10:6379> info

 

We will get the following output, here look for ‘replication’,

# Replication

role:master

connected_slaves:1

Slave0:

ip=10.10.10.11,

port=6379,

state=online,

offset=215,

lag=0

master_repl_offset:215

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:2

repl_backlog_histlen:214

 

Here we can see the IP address for the secondary server i.e. 10.10.10.11, also it shows the number of secondary servers i.e. 1.  

We can also try adding a key on the Primary server, that will be replicated to the secondary server in real-time. To create a test key-value, login to redis instance & execute the following command,

10.10.10.10:6379> set test “testing”

 

now to check the key on the master, run command ‘get test’

10.10.10.10:6379> get test “testing”

 

Now, login to slave server & check if the key ‘test’ has been replicated or not,

$ redis-cli -h 10.10.10.11 -p 6379

 

& run ‘get test’ command,

10.10.10.11:6379> get test “testing”

 

This shows that the key has been replicated to the slave server as well & our master-slave data replication is working fine.


Promoting Secondary server as Primary

So in case, the Primary server fails or there is any other issue, we can promote the secondary server as the primary server. To do this connect to the redis instance of the secondary server,

$ redis-cli -h 10.10.10.11 -p 6379

& execute the following command,

10.10.10.11:6379> SLAVEOF NO ONE

Now the secondary server will act as the primary server. You will be required to update the IP address of redis to the application where you have configured it.

Also if there is more than one redis secondary server, then you are required to update the field,

slaveof 10.10.10.11 6379

on all the secondary servers to point to a new primary server.


Reconnecting to Original Primary server

Once the original primary server is up, we can then reconnect the secondary server to the primary server by the following method, connect to the redis instance of the secondary server,

$ redis-cli -h 10.10.10.11 -p 6379

& execute the following command,

10.10.10.11:6379> SLAVEOF 10.10.10.10 6379

That’s it, this was our tutorial on how setup Redis Replication in Linux. Please do send in any questions or queries using the comment box below.

If you think we have helped you or just want to support us, please consider these:-

Connect to us: Facebook | Twitter | Linkedin

TheLinuxGURUS are thankful for your continued support.

The post How to setup Redis replication in Linux appeared first on The Linux GURUS.

]]>
https://thelinuxgurus.com/how-to-setup-redis-replication-in-linux/feed/ 0 1289
Install Redis on CentOS / RHEL https://thelinuxgurus.com/install-redis-on-centos-rhel/ https://thelinuxgurus.com/install-redis-on-centos-rhel/#respond Mon, 14 Dec 2020 15:42:45 +0000 https://thelinuxgurus.com/?p=1268 Redis is an open-source in-memory key-value data structure store that can be used as a cache server, message broker, etc. We can use a number...

The post Install Redis on CentOS / RHEL appeared first on The Linux GURUS.

]]>
Redis is an open-source in-memory key-value data structure store that can be used as a cache server, message broker, etc. We can use a number of data types with redis like strings, lists, maps, sets, sorted sets, HyperLogs, bitmaps, streams, and spatial indexes.

It also provides a number of features like,

  • Replication
  • data persistence
  • LRU eviction
  • High availability using sentinel
  • Publish / Subscribe support for data
  • Keys with limited TTL
  • Automatic Failover
  • LUA scripting, etc

Recommended Read: Install Redis on Ubuntu

Also Read: How to Host Multiple Websites with Nginx in Linux

So let’s start the process to install Redis on CentOS or RHEL.


Install Redis on CentOS

Redis packages are available on EPEL repository, so we are required to install the EPEL repository on our CentOS/ RHEL system first. Install it with the following command,

# yum install epel-release

Once the epel repository has been installed, we can install redis with the following command,

# yum install redis

Start service using the following command,

# systemctl start redis


Another method to install Redis on CentOS

With this method, we can install the latest version of redis or for that matter any version we need. We will installing the latest version at this time, i.e. 6.0.9.. Download the redis package with the following command,

# wget https://download.redis.io/releases/redis-6.0.9.tar.gz

Next extract the redis package,

# tar -xvf  redis-6.0.9.tar.gz

Now goto the extracted folder,

# cd redis-6.0.9

& then goto folder ‘deps’,

$ cd deps

next compile packages, ,

$ make hiredis lua jemalloc linenoise

$ make geohash-int

Next, we will move back to the main directory i.e. ‘redis-6.0.9’

$ cd ../

execute  ‘make’ & ‘make install’ commands

$ make

$ make installation

Once these commands have been executed, we will install the init scripts, init script will setup a redis service with port number, config file, log file & a data directory. To run run init script,

$ cd utils\

& run the install_server.sh script,

$ ./install_server.sh

We will now be asked with some information regarding redis server, as shown below,

Welcome to the redis service installer

This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379]

Selecting default: 6379

Please select the redis config file name [/etc/redis/6379.conf]

Selected default – /etc/redis/6379.conf

Please select the redis log file name [/var/log/redis_6379.log]

Selected default – /var/log/redis_6379.log

Please select the data directory for this instance [/var/lib/redis/6379]

Selected default – /var/lib/redis/6379

Please select the redis executable path [/usr/local/bin/redis-server]

Selected config:

Port : 6379

Config file : /etc/redis/6379.conf

Log file : /var/log/redis_6379.log

Data dir : /var/lib/redis/6379

Executable : /usr/local/bin/redis-server

Cli Executable : /usr/local/bin/redis-cli

Is this ok? Then press ENTER to go on or Ctrl-C to abort.

Copied /tmp/6379.conf => /etc/init.d/redis_6379

Installing service…

Successfully added to chkconfig!

Successfully added to runlevels 345!

Starting Redis server…

Installation successful!

Modify any settings as per your requirements & can also install more than one redis instances by just changing the redis port number.

Now to start the redis service, command is a bit different as service name will be different,

# systemctl start redis_6379

So just change the port number to start only a single redis instance.


Testing Redis installation

We can now check if the redis has been installed by running the following command,

$ redis-cli –version

We can also connect to the installed redis server with the following command,

$ redis-cli

then check the response to ping command for redis,

redis > PING

In response, we should get PONG as output.


Allowing remote connections to Redis installation

By default, we can only connect to redis server from the local server. To allow connections to redis installation from remote machines, we need to make changes to redis configuration file,

$ sudo vi /etc/redis/redis.conf

then look for section that says ‘bind 127.0.0.1’ & change it

bind 0.0.0.0 

to allow connections from all remote servers or we can also mention single or multiple IP addresses to allow connections from those servers only, like

bind 10.10.10.10 192.168.1.10

here, connections will be allowed from two machines with IP addresses 10.10.10.10 & from 192.168.1.10.

Once the changes have been made, we need to restart the redis service to apply changes,

$ sudo systemctl restart redis

This completes our tutorial on how to install Redis on CentOS & RHEL. Please do send in any questions or queries using the comment box below.

If you think we have helped you or just want to support us, please consider these:-

Connect to us: Facebook | Twitter | Linkedin

TheLinuxGURUS are thankful for your continued support.

The post Install Redis on CentOS / RHEL appeared first on The Linux GURUS.

]]>
https://thelinuxgurus.com/install-redis-on-centos-rhel/feed/ 0 1268