Why & How to Redis? — Part-1

Fahad Ahammed
5 min readJan 29, 2020

I have been using Redis for more than one year. But initially It was only with caching plugins. I did not see beyond that. As caching, it was just like an unimportant image of Redis caught in my head.
You might think why I am saying it as unimportant. Whenever we say caching, we assume it as a volatile thing. But recently I have learned that it is much more efficient than that as it has many more use cases.

I will present you this article with some question-answer. The questions you will get answered is listed below:

  1. What is Redis?
  2. Where can I use Redis?
  3. How to install it in Ubuntu/CentOS?
  4. How to start using Redis?
  5. How to use Redis with Python?

I will try to make some clear explanations on those answers.

What is Redis?

In simple answer, it is a key:value data store. From their website:

Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker.

From above line, first catchy section is “BSD licensed”. In a nutshell,

* You are free to redistribute the software, in binary or source form, as long as the copyright, conditions and disclaimer are present.
* You cannot use the name of the originating organization, or contributors, to promote derivatives of the software, without written consent.

If adhered to, you are free to modify, copy and redistribute BSD-licensed software in either source or binary form as you see fit. You are not required to return code or patches to the upstream BSD-licensed software. You are free to change the license, or charge for derivatives, of the software, be it commercial or proprietary.

BSD-licensed software is particularly attractive to organizations providing a service, such as GMail, or embedded applications, such as your mobile phone or stereo. The company/developer can use BSD-licensed software to produce binary applications or services, without releasing the source code.

After the license, we see that it says it is used as —

  • Database
  • Cache
  • Message Broker

So, We now have a normal definition about the “WHAT” of Redis.

Where can I use Redis?

Before answering this question. I think I need to point out some specific features of Redis.

  • Varied data structure support like strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs and geospatial indexes with radius queries.
  • Built in replication, high availability with extension etc.
  • LRU, LFU eviction policy with caching in purpose.
  • Set ‘Time to Live’ to a Key.
  • Configurable policy on persistence mode.

In simpler way, you can use Redis in-

  • Caching
  • Large Database with faster operations.
  • Token store
  • Session data store for persistence

Let you have an application with session based access. For scalability, you might want to have multiple copy of your application, you can then store session data to Redis instance which will make it available even in restart of the applications and I did say that it is fast.

How to install it in Ubuntu/CentOS?

For containerized deployment or production server deployment, industry depends heavily on Linux based distributions. I personally prefer Ubuntu and then CentOS.
I will explain how to install it in both of them. For CentOS, let’s focus on CentOS7.

CentOS: To install in CentOS, you need EPEL repository. To add the EPEL repository, and update YUM to confirm your change:

$ sudo yum install epel-release
$ sudo yum update

Install Redis:

$ sudo yum install redis

Start Redis:

$ sudo systemctl start redis

To automatically start Redis on boot:

$ sudo systemctl enable redis

Verify that Redis is running with redis-cli:

$ redis-cli ping

If Redis is running, it will return:

PONG

Ubuntu: To install Redis in Ubuntu is simple as the word ‘is’. First need to update the repo and do install it by:

$ sudo apt udpate
$ sudo apt install redis-server

Check if redis is running already after install.

$ redis-cli ping

If the reply is ‘PONG’ then go on or do start it:

$ sudo service redis-server start

To tweak redis to have better performance and flexibility, you need to make sure some configuration.

CentOS has redis configuration file in ‘/etc/redis.conf’ and ubuntu has in ‘/etc/redis/redis.conf’.

First of all you can make sure that redis is listening in only localhost or 127.0.0.1, find below line or make sure bind has 127.0.0.1 set.

bind 127.0.0.1

But you can do make it available for external access. On that case, you should set password.

To set password, search for the line:

requirepass

It should be commented. To enable password authentication, uncomment that line and set a bigger password.

requirepass BIGGER_PASSWORD

I particularly said bigger because as Redis is fast, it is easy to try brute-force faster. They can try 150k passwords per second against a good box.

To have the changes in redis, you need to restart the server.

Enter into Redis console

$ redis-cli

To do any operation in there, it will warn you :

(error) NOAUTH Authentication required.

To login:

$ 127.0.0.1:6379> auth password

This is now enough to dive into the playground.

How to start using Redis?

As I showed above, to use redis console, you need to go to:

$ redis-cli

Then you can just try inserting the data:

127.0.0.1:6379> SET book-store www.rokomari.com
OK
127.0.0.1:6379> GET book-store
www.rokomari.com"
127.0.0.1:6379>

How to use Redis with Python?

First of all you need to make sure you have the redis python package installed.

$ sudo pip install redis

Now let me give you a code snippet to work with redis in Python3.

import redis

class DatabaseRedis:
def __init__(self):
# Database String
self.myredis = redis.Redis(host='localhost', port=6379, db=0) # Without password
#self.myredis = redis.StrictRedis(host='localhost', port=6379, password='PASSWORD', db=0) # With password

def store_value(self, key, value):
if self.myredis.set(key, value):
return True
else:
return False

def get_value(self, key):
if self.myredis.get(name=key):
return self.myredis.get(name=key)
else:
return False

#DatabaseRedis().store_value(key="book-store", value="www.rokomari.com")
print(DatabaseRedis().get_value(key="book-store").decode())

That is it. You can use all the structures like that.

Conclusion

That is it for this part. If you have any query, leave comment below or ask me via email iamfahadahammed@gmail.com. In the next part, I will try to cover some data structures in Redis Database.

--

--

Fahad Ahammed

A learner of Cloud, Linux, and Programming with 7+ years of experience in the related field sharing my day-to-day stories.