user1757703
user1757703

Reputation: 3015

Django & Redis: How do I properly use connection pooling?

I have a Redis server which I query on almost every Django view for fetching some cached data. I've done some reading on some stackoverflow questions and learned that making a new Redis connection via r = redis.StrictRedis(host='localhost', port=6379, db=0) for every single web request is bad and that I should be using connection pooling.

Here is the approach I came up with for connection pooling in Django:

In settings.py so I can pull it up easily in any Django view as this is like a global variable:

# Redis Settings
import redis
REDIS_CONN_POOL_1 = redis.ConnectionPool(host='localhost', port=6379, db=0)

In some views.py:

from django.conf import settings  
REDIS_CONN_POOL_1 = settings.REDIS_POOL_1   
r = redis.Redis(connection_pool=REDIS_CONN_POOL_1)
r.get("foobar") # Whatever operation  

So, my question is: Is this the right way to do connection pooling in Django? Are there any better approaches you guys use for those have experienced a similar scenario like this? This is probably better than my old approach of opening and closing a new connection to redis on every request.

EDIT: Gathered my understanding about why it's wrong to open a new connection on every request from this stackoverflow question.

Upvotes: 30

Views: 11463

Answers (1)

martintrapp
martintrapp

Reputation: 819

A better approach would be to setup redis as your Django's cache backend with Django redis cache app. It gives you a done solution for your problem and you can use Django's official cache library to reach redis whenever you want get or set cached information. You can also avoid compatibility issues in your application if you decide to change your cache backend to something else.

Here's an easy to follow tutorial:

Using Redis as Django's session store and cache backend

Upvotes: 4

Related Questions