Slow Performance of Django Web App

76 views
Skip to first unread message

Kunal Mittal

unread,
Jun 11, 2023, 3:01:16 PM6/11/23
to Django users
I've a django application serving 10k+ users daily, that application has everything to be on the top of performance metrics but still some API's have high latencies, application serves lots of pages with nice css and various images, on most the pages they are being served by s3 while on some it's via django static files. We have implemented caching on akamai for every static file that application serves, whenever that cache disappears latency of each and every API spikes exponentially, at that time even loading metrics becomes a pain, server does not pick those requests. Can anyone suggest what's going on here.

Mz Gz

unread,
Jun 11, 2023, 3:36:18 PM6/11/23
to django...@googlegroups.com
What server do you use ?

On Sun, 11 Jun 2023, 10:00 pm Kunal Mittal, <mittal...@gmail.com> wrote:
I've a django application serving 10k+ users daily, that application has everything to be on the top of performance metrics but still some API's have high latencies, application serves lots of pages with nice css and various images, on most the pages they are being served by s3 while on some it's via django static files. We have implemented caching on akamai for every static file that application serves, whenever that cache disappears latency of each and every API spikes exponentially, at that time even loading metrics becomes a pain, server does not pick those requests. Can anyone suggest what's going on here.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d32868ba-a2df-41e3-8f41-3ecc3c1b3471n%40googlegroups.com.

carlos

unread,
Jun 11, 2023, 8:24:49 PM6/11/23
to django...@googlegroups.com

On Sun, Jun 11, 2023 at 1:00 PM Kunal Mittal <mittal...@gmail.com> wrote:
I've a django application serving 10k+ users daily, that application has everything to be on the top of performance metrics but still some API's have high latencies, application serves lots of pages with nice css and various images, on most the pages they are being served by s3 while on some it's via django static files. We have implemented caching on akamai for every static file that application serves, whenever that cache disappears latency of each and every API spikes exponentially, at that time even loading metrics becomes a pain, server does not pick those requests. Can anyone suggest what's going on here.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d32868ba-a2df-41e3-8f41-3ecc3c1b3471n%40googlegroups.com.


--
att.
Carlos Rocha

Kunal Mittal

unread,
Jun 12, 2023, 1:23:06 AM6/12/23
to Django users
Hi, we are using Django 1.8.5
Architecture is like -  Nginx side car -> wsgi server (gunicorn) -> application

Kunal Mittal

unread,
Jun 12, 2023, 1:24:28 AM6/12/23
to Django users
I have read various docs on performance optimization, but still not getting what's taking to render templates and what's the bottleneck when cache get cleared, I takes around 12 hours for application to run smoothly after that event.

Vishesh Mangla

unread,
Jun 12, 2023, 1:45:31 AM6/12/23
to django...@googlegroups.com

Vishesh Mangla

unread,
Jun 12, 2023, 1:46:11 AM6/12/23
to django...@googlegroups.com
Try cdn, and use Statics based on screen size 

Kunal Mittal

unread,
Jun 12, 2023, 2:29:41 AM6/12/23
to Django users
Yes, we do use CDN but when that CDN's cache purges, latency spikes. Trying to figure out that.

Steven Mapes

unread,
Jun 12, 2023, 9:11:25 AM6/12/23
to Django users
To start with I'd look at upgrading the project to the latest version of Django and Python as Python 3.10 and 3.11 have some performance improvements and you'll need to be on a Django 4 version to support them.

That aside it sounds like you've got good caching architecture setup  since it's working well when cached but that the architecture without the caching is not up to the job. You need to investigate where the performance hit is coming in, most likely it'll be database performance. Once thing to check would be whether you are using things like like prefetch_related, select_related, and defer with any queryset or are you always returning the whole object. If you are returning the whole object do you need to? 

After that it depends on how you are using and exposing Django as to what I'd suggest you check next. Are you using DRF as API backend to the pages in S3 or are you serving pages via Django and then caching those?  If it's the latter, run it locally with caching turned off and debug toolbar installed and look at each pages output to see how many queries you are hitting per page and the performance of each.

Since you are using a lot of caching how are you maintaining the cache? Are you letting it expire and then self populate or are you using warm-ups to fetch the update information and then update the cache prior to it expiring? If you're doing the former but the latter fits your setup look to switch to that. In those instances you can update the fetch the results to update the cache outside of a user transaction

Kunal Mittal

unread,
Jun 12, 2023, 12:42:01 PM6/12/23
to Django users
Hi Steven, thanks for the guided suggestion, really means a lot. Will try following these points. 
As of now I did some digging and found execution of render function is the real culprit. 
On my local machine it took more than 5 minutes to render homepage, assuming my homepage does a lot of things I tried replacing it with a basic html page with only
one line, It took 6 seconds to execute render on that for the first call. Can someone please suggest what's the reason behind this or is there any alternative of render function.
 

Nisha Jose

unread,
Jun 13, 2023, 1:50:14 PM6/13/23
to django...@googlegroups.com
Try to check any N+1 issue available in the code. 


Vishesh Mangla

unread,
Jun 13, 2023, 2:15:26 PM6/13/23
to django...@googlegroups.com
Try profiling your django code

Vishesh Mangla

unread,
Jun 13, 2023, 2:16:17 PM6/13/23
to django...@googlegroups.com

Shaheed Haque

unread,
Jun 14, 2023, 1:05:10 AM6/14/23
to django...@googlegroups.com
Have you tried using https://django-debug-toolbar.readthedocs.io/en/latest/? It can be a little fiddling about with, but it's time based profiling support has been invaluable in tracking down a few critical queries. 

On Sun, 11 Jun 2023, 20:00 Kunal Mittal, <mittal...@gmail.com> wrote:
I've a django application serving 10k+ users daily, that application has everything to be on the top of performance metrics but still some API's have high latencies, application serves lots of pages with nice css and various images, on most the pages they are being served by s3 while on some it's via django static files. We have implemented caching on akamai for every static file that application serves, whenever that cache disappears latency of each and every API spikes exponentially, at that time even loading metrics becomes a pain, server does not pick those requests. Can anyone suggest what's going on here.

--

Vishesh Mangla

unread,
Jun 14, 2023, 2:14:09 AM6/14/23
to django...@googlegroups.com
One important question, what webserver are you using?

Lucifer Black

unread,
Jun 17, 2023, 10:10:11 AM6/17/23
to django...@googlegroups.com
thank you you already learned how I updated the AP earlier today


From: django...@googlegroups.com <django...@googlegroups.com> on behalf of Vishesh Mangla <manglav...@gmail.com>
Sent: Wednesday, June 14, 2023 1:12:52 AM
To: django...@googlegroups.com <django...@googlegroups.com>
Subject: Re: Slow Performance of Django Web App
 
Reply all
Reply to author
Forward
0 new messages