Getting started with Redis

Introduction

 

Redis is a NoSQL or non-relational, advanced key-value store and in-memory database server. It’s open source with BSD Licensing.

Redis is written in C. And as it supports wide range of Data Structures, it’s also known as Data Structure server. The supported data types include strings, lists, hashes, sets & sorted sets.

Redis supports pipelining which means the client can send multiple requests to the server without having to wait for the replies at all, and finally reads the replies in a single step.

Since Redis is NoSQL, there are no tables & no database-defined.

 

Features

 

  •  NoSQL database makes the management of data easier. So, both read & write operations are fast.
  •  Being written in C makes it even faster to operate.
  •  Combines the adavantages of in-memrory & schema-less design patterns.
  •  Wide range of Data Structure support with additional support for range queries, bitmaps, etc.
  •  Best suited for Caching. Can work as Primary Database too.
  •  Wide API support for language like Java, Python, C, C++, etc.
  •  Master/Slave support, read replication and client-side sharding to scale write performance.
  •  Message queue support and can work as a Channel Layer.
  •  Ahead of file(AOF) & Point in time support for replicating data to persistant storage.
  •  Atomic operations & supports pipe-lining which reduces network round trip time in case multiple operations need to be performed

Above features make Redis a viable candidate for Caching solution.

But there can be limitations as mentioned below.

  • Data stored in Redis server would be lost if server crashes unexpectedly. Redis accomodates this problem by allowing you to write the data to persistent storage regularly.
  • Since Redis manages in-memory databases, the RAM size restricts the database size.

 

 

Supported Data Types

 

   Source:- https://redislabs.com

 

 

Backup Policy

 

Redis offers two ways to write/backup data to persistent storage.

  • Snapshot:- It offers Point in Time backup i.e Redis can save current status of DB to disk. At any time one can restore to this point by restoring the backup file. Use command SAVE or BGSAVE to start a snapshot. BGSAVE is SAVE, running in background instead.
  • AOF(Ahead of file):- In this, only write queries are written to a file as they hit the Redis server. So, one needs to replay all the write queries to restore the DB.

Restoring from Snapshot is really fast. In case the DB crashes before the next Snapshot is made, you can only restore what last successful snapshot has saved. So, the changes in data after the last backup cannot be restored.

Restoring from AOF can be a time intensive process as every single write query will be executed. You may have queries which took longer time so they would cause more delay. Redis doesn’t write queries to disk straightaway. Rather it flushes the queries/data to buffers and then it’s the responsibility of OS to write them to disk.

 

 

How Redis compares with other databases

 

Source:- https://redislabs.com

 

 

Setup

 

Install the Redis-server using below command.

Redis-Cli is a command line redis client interface used to connect to local/remote redis-server.

Type “redis-cli” on terminal to start the client interface.

By default it connects to local redis-server on default port(6379). These default values can be changed by using command line options.

Redis-cli lets you manage server and its configuration.

Some examples:-

Above we map the key “name” to value “myfirstkeypair”

Above we added values to set “myfirstset”.

Notice the return integer. Integer 1 means new value added to SET and int 0 means value already present.

Similarly many other operations can be peformed.

Run “redis-benchmark” to benchmark your redis installation, it performs 100000 operations on different data types.

 

 

APIs Support

 

Redis has a great support for APIs. Check https://redis.io/clients for available options.

Now, we will check how Redis DB can be managed using Python API.

We need install Python library “redis” first. We are using Python3.

Note:- Use apt install python3-pip to install pip3 if needed.

Below code snippet creates a set & then deletes the set.

The output of file is:-

As the output shows, five values(numbered from 0 to 4) were successfully added.

 

In the next blog, we will see how Redis can be used for Session management.

 

Thank you for Reading this!! Hope it helps.

. . .

Comment

Add Your Comment

Be the first to comment.

css.php