connecting redis to django rest framework

hi guys, i’m working on a project and i’d like to optimize site performance through caching and i have identified redis as my in-memory solution, however even though i have my redis server up and running in one instance of my linux distribution on my wsl and another instance running my django server, i dont get TCP request from django server although i’ve applied the settings in django settings.py file and also applied the cache_page method to my model view. can anybody help out with why i cant recieve request on my redis server. here is my settings.py and my assessment.py view

from django.views.decorators.cache import cache_page                                                                                                                                                                                            from api.utils.jwt_utils import get_user_from_request                                                                                                                                                                                           from ..models.assessment import Assessment, Question, Answer                                                            from ..models.learner import Learner 
from ..models.admin import Admin
from ..serializers.assessment import AssessmentSerializer, QuestionSerializer, AnswerSerializer                                                                                                                                                 u/cache_page(60 * 15)                                                                                                    u/api_view(['POST'])                                                                                                     u/permission_classes([IsAdminUser])
def create_assessment(request):
""" creates an assessment """
if request.method == 'POST':                                                                                                serializer = AssessmentSerializer(data=request.data)
if serializer.is_valid():

# and my cache settings in my settings.py
CACHES = {                                                                                                                  "default": {                                                                                                                "BACKEND": "django_redis.cache.RedisCache",                                                                             "LOCATION": "redis://127.0.0.1:6379/1",                                                                                 "OPTIONS": {                                                                                                                "CLIENT_CLASS": "django_redis.client.DefaultClient",                                                                }                                                                                                                   }
                                                                                                               }

please reformat your content… :slight_smile:

have you tried to telnet the redis box on the right port, from the “client machine”?
have you run a nmap - maybe the right port is not opened?

your location setting:

"LOCATION": "redis://127.0.0.1:6379/1"

points to the same machine where django is running. Start by updating that so that django points to the right machine?

thank you @plopidou, i see your idea when you say my location is pointing to the same machine where django is running, i’ll work on pointing it to the right direction. also how does telnet and nmap help in my situation? could you point me to resources that could help? also about the formatting, this is my first post on here so it’s a bit complicated finding my way around

telnet and nmap would be useful to detect whether your django node can actually communicate on the desired port with the redis node.

But I think in your case it might not be relevant; if I were you, I would start on that IP address setting value :slight_smile:

about formatting: use triple back ticks ``` to encapsulate your code block(s).

thanks @plopidou i worked on the solutions you provided and made a few observations. firstly when i use telnet to connect to the node the connection is actually successful, i am also able to SET and GET keys and values using redis-cli which is connected to my redis server, however when i check the logs of the redis server i dont see any logs recorded from my redis-cli. do you have any idea why this might happen?

are you sure you connected to one redis “instance” and checked the logs at the same place?
what ip/port did you use in both cases?

hi @plopidou sorry for replying this late, i was able to fix it. once i used redis-cli to connect to my redis-server, i used redis-cli monitor to keep track of changes made to my server. now my caching works and ive been able to use loadtest to track the performance of my server. are the any other concepts you go recommend for me to optimize server performance? i’d be very grateful, thank you

ok cool, glad you fixed your issue :slight_smile:
optimisation is, imho, very much project-specific. There are not readily baked advice to be given. Experiment, test and decide :slight_smile:

but beware of premature optimisation: “premature optimisation is the mother or all evil”, or so they say :slight_smile:

1 Like

thanks @plopidou for your time and effort in helping me out and also willingly sharing your knowledge, i appreciate it.