Unleashing the power of Redis x Django

Caching Data and Queuing Jobs using Redis in Django

Samhita Alla
Level Up Coding

--

Background photo from Unsplash

1. Caching Data — An Alternative to Django Session

The session framework in Django isn’t preferable when there’s a humongous amount of data to be fetched, loaded, and set on a per-site-visitor basis. The error message, “The request’s session was deleted before the request completed. The user may have logged out in a concurrent request, for example.” could pop up when there’s an enormous amount of session data to be loaded. As session data gets loaded for every request, storing huge data can slow down your application. A Redis cache, in such cases, could act as the doppelgänger! Without any further ado, let’s quickly check how that’s done.

Step 1: Installing redis and django-redis library

yum install redis
pip3 install django-redis

Handy commands to start Redis Service:

On Linux: systemctl start redis.service
On Windows: redis-server.exe

Step 2: Defining Cache in settings.py

A cache has to be instantiated by defining all the required parameters — BACKEND defined the cache backend to be used, LOCATION creates a socket connection, and CLIENT_CLASS is the default Redis client.

# Cache
CACHES = {
'default': {
'BACKEND': 'django_redis.cache.RedisCache',
'LOCATION': 'redis://127.0.0.1:6379/',
'OPTIONS': {
'CLIENT_CLASS': 'django_redis.client.DefaultClient',
}
}
}

Step 3: Replicating Session in views.py

A django session is usually identified by a key-value pair. To replicate the same behavior whilst defining a cache, the session key has to be procured to isolate values on a per-site-visitor basis. A key-value pair can be set and retrieved using hset() and hget() methods of the redis client respectively.

2. Redis Queue — A Background Job handler

Users do not prefer to wait for a page to load; they intend to quickly move from one webpage to the other. In such cases, executing long-running background tasks asynchronously is often sought-after, when building web applications. Redis Queue queues the tasks and runs them one after the other (this could be made asynchronous by controlling the number of workers) in the background without interrupting the user’s view. Here’s a short how-to guide on installing and running Django Redis queue.

Step 1: Installing django rq library

yum install redis
pip3 install django-rq

Handy commands to start Redis Service:

On Linux: systemctl start redis.service
On Windows: redis-server.exe

Step 2: Instantiating the queue in settings.py

A queue has to be added in the INSTALLED_APPS section — HOST pertains to the server, PORT is the default port used by Redis, and DB pertains to the first database.

INSTALLED_APPS = [
...
'django_rq',
]
RQ_QUEUES = {
'default': {
'HOST': 'localhost',
'PORT': 6379,
'DB': 0,
}
}

Step 3: Defining URL in urls.py

In the main project urls.py folder, define the Django Redis queue URL.

urlpatterns = [
path("django-rq/", include('django_rq.urls')),
...
]

Step 4: Defining a background job in tasks.py

Create a file named tasks.py in the django app folder. Define a function there.

Step 5: Calling redis queue in views.py

In views.py, the previously defined function has to be called. The queue ‘default’ is fetched, a timeout is set, and the task is thereby queued.

Step 6: django-rq on web

If server is on localhost, the URL would be localhost/django-rq.

django-rq on web

‘default’ queue is associated with 1 worker and the ‘jira’ queue, with 2 workers.

Handy command: To start ‘default’ rq worker —

python3 manage.py rqworker default

I hope this article has helped you. Thanks for reading!

--

--