DEV Community

Debojyoti Chakraborty
Debojyoti Chakraborty

Posted on • Originally published at Medium

Redis : Your db for in memory

Hello everyone knows what is redis, it’s the most popular in-memory data structure store used as a database, cache, message broker, and streaming engine. It offers great data structures out of the boxes such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes, and streams.

And out of all things it is a open source software so everyone can take a look at the source code, if you want countribute check out the link

GitHub - redis/redis: Redis is an in-memory database that persists on disk. The data model is…
Redis is an in-memory database that persists on disk. The data model is key-value, but many different kind of values…

Today I will show you how to use redis with python to create your database in memory using redis. So let’s jump into it.

First you need python and redis installed in your machine, best option is to use docker and redis for this deploy your redis docker instance with one command in your terminal:

docker run --name redis -p 6379:6379 -d redis

It will create a redis instance at port 6379. Now it’s created let’s create our python code to store the sata into redis database. For this we need redis library from pip. If you don’ have it on your machiner install it with this command.

pip install redis
Now let’s code…

First create a file with anyname , let’s say and add this code,

Connect to Redis
The following code creates a connection to Redis using redis-py:

import redis
r = redis.Redis(
To adapt this example to your code, replace the following values with your database’s values:

In line 4, set host to your database’s hostname or IP address
In line 5, set port to your database’s port
In line 6, set password to your database’s password
Example code for Redis commands
Once connected to Redis, you can read and write data with Redis command functions.

The following code snippet assigns the value bar to the Redis key foo, reads it back, and prints it:

open a connection to Redis

r.set('foo', 'bar')
value = r.get('foo')
Enter fullscreen mode Exit fullscreen mode

Example output:

$ python
So this is how you can use redis and python to create your database in redis, also you can use for caching for applications. Below is some more resources for you to follow.

Redis with Python
To use Redis with Python, you need a Python Redis client. The following sections demonstrate the use of redis-py, a…

Redis | The Real-time Data Platform
Build and run faster apps with the world's leading real-time data platform.

Top comments (0)