Effective use of Redis with Python and Connection Pool
By Fahad Ahammed
- 2 minutes read - 310 wordsI have been working with redis and python for sometime. It is fun to play with python and redis. There are so many things can be solved by redis.
- Session persistency
- Caching
- Volatile Datastore
- Job or Message Queue
- Search
And many more. But recently I was thinking about using it as main database. I am not yet sure what kind of problem I might face or what things to optimize when it is about scalability or reliability. I will for sure share my experience.
Here I want to talk about –
connection pool of redis.
I was using: https://pypi.org/project/redis/
The code block that works –
Whenever you will call the function get_data you will be able to get the value of a key passed to the function. But as soon as the function is called by 100s of users, it will show problem though it is not so heavy operation, just read the data and pass.
The problem is every call of the function creates a connection to redis and it has limit. To solve this problem, you need to use existing connection or limited connections to do the operation. Main idea is to use a connection pool which will keep a connection for sometime and handle several operations on that connection. No new connection for every operation.
There is another way to do that. Let’s say we have multiple command to make in the redis database.
Let’s say, you want to keep track of the hit count of the key.
Here, two commands get executed using two times calling the r. But we can execute both in a single request.
Heres comes redis pipe.
Now the return will have different result.
There are many more configuration option to make your redis use pleasant in the long run. I will try to share my experience in future. Thank you for reading.