Intermittent Database Connection Errors and Slow Page Loads on My Django Website

Hi everyone, I’m running a Django-based website that focuses on restaurant content mainly Texas Roadhouse menus, reviews, and food guides. Everything has been working great for several months, but lately, I’ve been experiencing performance issues and intermittent database connection errors that I can’t seem to track down. I’m hoping someone here might be able to help me troubleshoot or point out what I might be missing.

The biggest problem is that certain pages (especially those that pull dynamic menu data from the database) take a long time to load — sometimes up to 10 seconds and occasionally throw a “OperationalError: MySQL server has gone away” message. I’m using MySQL as my database backend with Django 4.2, hosted on a VPS. I’ve already tried increasing the CONN_MAX_AGE and wait_timeout settings, but the issue persists intermittently. Interestingly, it happens more often when the site experiences moderate traffic spikes.

Another issue is that Django’s ORM seems to be creating duplicate queries. When I check the debug toolbar, I see multiple redundant SELECT statements for the same data, especially in my view that handles menu categories and items. I’ve tried using select_related() and prefetch_related(), which helped slightly, but I still get a high number of queries per page load. I’m not sure if this is an architectural problem in my view logic or if I need to restructure how I’m querying related models.

I also implemented caching using Redis, but it doesn’t seem to be helping as much as I expected. My cache hit rate is under 40%, and even cached pages sometimes load slowly. I’m wondering if my cache keys aren’t being reused correctly or if I’m not caching the right data. The site uses Django’s built-in cache framework with Redis as the backend, configured in settings.py. Could this be related to how my sessions are stored or how I’m invalidating cache keys?

Another strange behavior is that static files occasionally fail to load after a deployment. I’m using WhiteNoise for static file serving, and I’ve verified that collectstatic runs successfully, but users sometimes report missing CSS or image files. The logs show 404 errors for files that definitely exist in the /staticfiles/ directory. Restarting Gunicorn or clearing the cache usually fixes it temporarily, which makes me think it’s a caching or file reference issue.

Has anyone else run into similar problems combining Django, MySQL, Redis, and WhiteNoise in production? I’d love some advice on how to stabilize the database connections, optimize ORM performance, and make caching more predictable. My website doesn’t have heavy traffic, but these issues have started affecting usability. Any tuning tips or example configurations would be hugely appreciated I really want to get my Texas Roadhouse content site back to running smoothly. Sorry for the long post!

What operating system are you running this on? I see where you’re using Django 4.2, what version of Python are you using? Are you using any third-party apps? Any atypical configurations? What’s the size of your VPS? Is your MySQL instance local on that system, or on a different server?

I would think the first steps would be to gather more technical information when these errors occur. You’d want to gather your logs from all available sources to see if there’s something happening at the system level that is interfering with your application. (Not just the web server, but also for Gunicorn, syslog, and mysql.)

Also, I’d suggest decreasing CONN_MAX_AGE, and possibly setting it to 0.

The duplicated queries issue can only be addressed with seeing the code causing this. There’s no way to diagnose it otherwise.

The server is running on Ubuntu 22.04, with Python 3.10 and Django 4.2. MySQL is running locally on the same VPS (4 vCPU / 8GB RAM). I’m using a handful of third-party apps like django-redis, django-debug-toolbar, and a couple of menu-related custom apps, but nothing too unusual. I don’t think I’ve made any major atypical configuration changes outside of caching and database timeouts.

I’ll dig into all the logs you mentioned Gunicorn, syslog, MySQL, and the application logs — to see if there’s a pattern around the connection drops or system-level interruptions. It makes sense that something deeper could be interfering, especially since the issues only show up during brief traffic spikes.

I’ll also try reducing CONN_MAX_AGE or setting it back to 0 to see if that stabilizes the MySQL connections. I had increased it thinking persistent connections might help, but I can see how that might backfire if connections idle too long or MySQL closes them unexpectedly.

For the duplicate queries issue, I understand you’d need to see the actual view code I’ll try to isolate the problem and provide a small reproducible snippet in a follow-up message. It’s probably tied to how I’m structuring the menu category lookups.

Appreciate the guidance I’ll collect the logs and test the connection settings, then report back with more specifics. Thanks again for pointing me in the right direction!