Search code examples
pythonmysqldjangogoogle-cloud-platformgoogle-cloud-sql

Google Cloud SQL w/ Django - Extremely Slow Connection


Edit:

After doing some further investigation, the delay seems to be more Django than the Cloud SQL Proxy.

I added a couple of print statements at the start and end of a view, and they print instantly when the request is made, but it takes a further 60 seconds for the page to load.

I've stripped back the template files to include only the bare bones, removing most scripts and static resources and it's still pretty similar.

Changing my view to return a simple HttpResponse('Done') cuts the time drastically.

Whilst developing locally I am using Django to serve the static files as described in the docs. Again, I don't have this issue with other projects though.

Original Post:

I've recently noticed my Django application is incredibly slow to connect to my Google Cloud SQL database when using the Cloud SQL Proxy in my local development environment.

The initial connection takes 2-3 minutes, then 60 seconds per request thereafter. This applies when performing migrations or running the development server. Eventually the request completes.

I've tried scaling up the database but to no effect (it's relatively small anyway). Database version is MySQL 5.7 with machine type db-n1-standard-1. Previously I've used Django Channels but have since removed all references to this.

The Middleware and settings.py are relatively standard and identical to another Django app that connects in an instant.

The live site also connects very fast without any issues.

Python version is 3.6 w/ Django 2.1.4 and mysqlclient 1.3.14.

My database settings are defined as:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': os.getenv('DB_NAME'),
        'USER': os.getenv('DB_USER'),
        'PASSWORD': os.getenv('DB_PASSWORD'),
        'PORT': '3306',
    }
}

DATABASES['default']['HOST'] = os.getenv('DB_HOST')
if os.getenv('GAE_INSTANCE'):
    pass
else:
    DATABASES['default']['HOST'] = '127.0.0.1'

Using environment variables or not doesn't seem to make a difference.

I'm starting the Cloud SQL Proxy via ./cloud_sql_proxy -instances="my-project:europe-west1:my-project-instance"=tcp:3306.

After invoking the proxy via the command line I see Ready for new connections. Running python manage.py runserver shows New connection for "my-project:europe-west1:my-project-instance" but then takes an age before I see Starting development server at http://127.0.0.1:8000/.

I'm also noticing several errors in Stackdriver:

  • _mysql_exceptions.OperationalError: (2006, "Lost connection to MySQL server at 'reading initial communication packet', system error: 95")
  • django.db.utils.OperationalError: (2013, "Lost connection to MySQL server at 'reading initial communication packet', system error: 95")
  • AttributeError: 'SessionStore' object has no attribute '_session_cache'

These appear - or don't - from time to time without changing any settings.

I've read they may be an access rights issue but the connection is eventually made, it's just incredibly slow. I'm authorising via the Google Cloud SDK, which seems to work fine.


Solution

  • Eventually I found that the main source of the delay was a recursive function being called in one of my admin forms (which delayed the initial startup) and context processors (which delayed each load). After removing it, the pages loaded without issue. It worked fine when deployed to App Engine or when using a test/local SQLite database, though, which is what made debugging a little harder.