Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 834
user1757703 Avatar asked Dec 10 '14 22:12

user1757703


People also ask

What is the Django used for?

What is Django? Django is a high-level Python web framework that enables rapid development of secure and maintainable websites. Built by experienced developers, Django takes care of much of the hassle of web development, so you can focus on writing your app without needing to reinvent the wheel.

Is Django same as Python?

Python and Django are intertwined but not the same. Python is a programming language that's used for many different applications: artificial intelligence, machine learning, desktop apps, etc. On the other hand, Django is a Python framework for full-stack web application development and server development.

Is Django frontend or backend?

“The technically correct answer,” Willison told me when I asked him about this, “is that a backend framework like Django works with any frontend framework, because of separation of concerns: if a web framework can respond to HTTP requests with JSON and HTML, it can be used with anything.”

Is Django for Python only?

HTML and CSS may not be related to Python but there are necessary for Django. Django Templates uses HTML and CSS to structure and style the webpages.


1 Answers

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

like image 116
martintrapp Avatar answered Sep 19 '22 14:09

martintrapp