'What's a proper way to SET aurora_replica_read_consistency in django?

I am looking for a proper way to connect to Aurora from a redundant django app. I have a django app that runs in multiple regions. The Aurora cluster in AWS is configured to have a writable master in one region and a read-only replica with write-forwarding in the other region. This, supposedly, allows apps to "transparently" write to the read-only DB. It's not very transparent because it requires for every session to SET aurora_replica_read_consistency=SESSION. I was hoping to achieve this in the DATABASE session of the django settings file like so:

DATABASES = {
    'default': {
        'ENGINE': 'django_prometheus.db.backends.mysql',
        'NAME': 'mydb',
        'USER': 'admin',
        'PASSWORD': 'DB_PASSWORD',
        'HOST': 'DB_HOST',
        'PORT': '3306',
        'OPTIONS': {
            'init_command': 'SET aurora_replica_read_consistency=SESSION',
        },
    },
}

However, this throws an error on the aurora master, which is writable. The error states something that you can only set this variable on the read-only replica. I.e. my django container won't even boot on the master. I tried screwing with the connection signals; however, I did not find a way to make it run the SET command as the first thing after any new django connection to the DB. Signals seem to require each app to have a signal handler. The way my failover is set up is that the Aurora master may be in any region. I.e. it's not easy to determine whether this django client connects to a master or a replica. I can go as far as ignore the error from the init_command, but I don't understand how it's possible to accomplish. Any good ideas on how to solve this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source