Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way to manage redis connections in django

I have a django project that uses a redis (just one db in redis).

Currently I do this:

In my settings.py file, I have:

from redis import Redis
REDIS_CONNECTION = Redis()

Anytime I want to call this connection (on my many views.py file in different apps in the project) I do it this way:

from django.conf import settings
settings.REDIS_CONNECTION.lpush("testlist", "hello")

Is there a problem with this approach? I do not want to keep creating new connections to redis if it is not necessary.

like image 593
Trewq Avatar asked Feb 04 '15 15:02

Trewq


People also ask

Can Redis handle multiple connections?

Redis can handle many connections, and by default, Redis has a maximum number of client connections set at 10,000 connections. You can set the maximum number of client connections you want the Redis server to accept by altering the maxclient from within the redis. conf file.

Does Redis need connection pool?

If we create a separate Redis connection for a thread it will be overhead for the Redis server. We need to have a pool of connections in multi-threaded environments to address these challenges. This allows us to talk to Redis from multiple threads while still getting the benefits of reused connections.

Does Django support Redis?

With the release of Django 4.0, Redis is now a core supported caching backend. Redis is available as part of Memorystore, Google Cloud's managed in-memory data store.


1 Answers

From the official package documentation:

Behind the scenes, redis-py uses a connection pool to manage connections to a Redis server. By default, each Redis instance you create will in turn create its own connection pool. You can override this behavior and use an existing connection pool by passing an already created connection pool instance to the connection_pool argument of the Redis class. You may choose to do this in order to implement client side sharding or have finer grain control of how connections are managed.

(see https://pypi.python.org/pypi/redis/)

If you want to use a centralized pool, instantiate one in a centralized place and every time you create a new instance pass it that new instance:

pool = ConnectionPool(host='localhost', port=6379, db=0)
r = Redis(connection_pool=pool)

In my modest opinion (not an expert) I would keep using the default way you've been working with and fallback to this approach only when you face performance issues.

Eager optimization can be worse than no optimization IMO.

like image 99
bitoiu Avatar answered Sep 28 '22 06:09

bitoiu