Introduction to Redis

Jan 27, 2017

It’s been a long break for the blog posts. But this means I have been working my ass off, so there are tons of stuff piled up to talk about! First one coming to my mind is Redis.

Redis is an in-memory data structure store. For this reason, it’s incredibly fast. It has key-value structure and supports data structures such as strings, hashes, lists, sets, sorted sets and so on.

It can be used either as a database, if relational ones keeps bugging you. Or you can cache your data in Redis, because it already stores everything in memory. Plus it has the capability of a message broker, where you can publish messages through Redis, or might even use it as your load balancer and task queueing purposes.

It has a great interactive tutorial where you can jump into it and start trying Redis immediately.

Connecting to Redis Server

After you follow the instructions and set up the redis-server, import the module “redis” in Python and create a connection by using StrictRedis as:

con = redis.StrictRedis(host="localhost", port=6379, db=0)

You can either check your Redis db in terminal by redis-cli command, or you can download one of the desktop managers for Redis. I have been using Redis Desktop Manager and it was free back then. You can still find its earlier versions and keep using it as free. There is also FastoRedis and many other open source GUI for Redis.

Basic Commands

Once you get your redis-server up and running, and you’ve got yourself a connection, why keep waiting? Here are some basic Redis keys commands:

con.set("key", "value")

>> "value"


These are just about enough for your key-value storage purposes. For storing lists in Redis:

  • To push an item to the tail of a list:
con.rpush("key", "item")
  • To get the length of a list:
>> 1
  • To insert an item to the head of a list:
con.lpush("key", "otheritem")
  • To get the list, i.e. for looping throug it (from head to tail, that's what 0 & -1 stand for):
con.lrange("key", 0, -1)
>> ["otheritem", "item"]

That’s just about it! There are other commands for sets, managing queue lists, hashes, publish/subscribe messaging and so on in the command reference.