Introduction To Redis Using Python And Redis-Py

Overview:

  • Redis is an open source, NoSQL data store.
  • Redis stores keys against values in physical memory, which is also called as the Random Access Memory (RAM).
  • The main idea behind this kind of key-value storage and access model is to serve as a cache between applications that use Redis and a persistent store.
  • The purpose of a cache is to reduce retrieval time of objects, as they are stored in the physical Memory.
  • Redis not only stores string values against keys but also several non-trivial data structures. The types supported by Redis include
    • Bitmaps through Strings
    • Hashes
    • Hyperloglogs
    • Geospatial Indexes
    • Strings of binary safe bytes
    • Lists
    • Sets
    • Sorted Sets
  • Redis support various operations based on the types of data. For example, it supports bit operations on strings, radius queries on Geospatial indexes and range queries on sorted sets.
  • The Redis itself can be used as a sole data store of an application as well.

Redis Keys:

  • Keys are central to Redis data model.
  • Everything that is stored on a Redis instance is stored against a key.
  • All the keys are of type binary safe strings.
  • Keys supported by Redis can have their size up to 512 MB. That is even bitmaps and other types of image data can be stored as keys.
  • Keys can be spread across multiple Redis instances resulting in the formation of distributed key space.

Sharding in Redis:

  • Redis instances can be spread across multiple systems where each system will store a specific set of keys partitioning the key space.
  • There are various methods to partition the key space including range partitioning and hash partitioning to mention a couple of them.
  • Also note that, an instance of a Redis server will make use of a single core of a multicore CPU. Any other configurations on the same machine need to be carefully planned.
  • The system deciding the key distribution could be any of the following:
    • Redis Clients
    • Dedicated Proxy system
    • The Redis server instances
  • Any system in this topology either a Proxy or the Redis Server instance can have asynchronous replication to support fault tolerance.

Persisting Redis data to Disk:

  • Redis is not just an in-memory data store through one can configure Redis that way. In this scheme data is there as long as the server is alive combined with the actual “time to live (ttl)” specified for each key.
  • Redis data can be written to disk as well using any of the two mechanisms available:
    • RDB persistence – RDB is the abbreviation for Redis Database File. Using RDB persistence point in time snapshots of the data are persisted to disk periodically in binary form.
    • AOF persistence – AOF is the abbreviation for Append Only File. Every operation or command that changes the data held by Redis is appended to a log file. This command log is used during the server start up to construct the data.

there are pros and cons in both the RDB approach and the AOF approach and has to be selected based on the need.

Databases in Redis:

  • There are no named databases in Redis.
  • By default, as specified in the redis.conf file 16 databases are allowed on a Redis instance.
  • The database to where the data is written can be selected by specifying the index of the database, which is a number.

Data store vs Cache:

  • Redis can be used either as a cache or as the main database itself.
  • The decision of using the Redis, as a cache or data store has to be carefully decided based on the problem at hand.

Redis-Py the Python Client for Redis:

  • Redis-Py is the Python client for Redis.
  • The class StrictRedis is for the latest Redis version as of this writing and the class Redis is for backward compatibility with older Redis versions.
  • The first step before doing any operation on Redis is to connect to redis by creating a StrictRedis instance.
  • StrictRedis constructor takes the hostname and the port number on which the Redis server is listening for connection along with the database index.

Example - Create a new key and add a string value:

  • A key can be created in Redis by calling the set() method on the StrickRedis instance.
  • The StrickRedis.set()method takes both the key and the value as parameters.
  • Here is a simple Redis-Py example that creates a key called 'Language' in the Redis server.

# Sample Python program that uses Redis-py to connect to a Redis Server

import redis

 

# Create a redis client

redisClient = redis.StrictRedis(host='localhost',

                                port=6379,

                                db=0)

 

# Set a string value for the key named 'Language'

redisClient.set('Language', 'Python 2.x')

 

# Get the value for the key and print it

print(redisClient.get('Language'))

 

# Change the value of they key

redisClient.set('Language', 'Python 3.x')

 

# Get the new value of the key and print it

print(redisClient.get('Language'))

 

Output:

b'Python 2.x'

b'Python 3.x'

 


Copyright 2020 © pythontic.com