graphene icon indicating copy to clipboard operation
graphene copied to clipboard

MySQL replication for Graphene Django

Open GABAnich opened this issue 3 years ago • 0 comments

enter image description here

I'm using Graphene as a GraphQL server in Django. I set up a MySQL replica in Django. My setting is pretty simple. Read requests -> Replica. Write requests -> Source

Mutations works in the next way: They modify data and then return modified/new data. Graphene modifies data in Source and immediately selecting them from Replica. The problem is that updated data is not immediately appearing in Replica DB (because of Replication delay). As result, some mutations simply do not work.

I have a solution - specifying the Django database in every mutation. Didn't tried it yet. But the project is huge, and this requires a lot of changes in the codebase.

I'm looking for simpler solutions.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'db_name',
        'USER': 'root',
        'HOST': MYSQL_HOST,
        'PORT': '3306',
    },
    'read_replica': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'db_name_replica',
        'USER': 'root',
        'HOST': MYSQL_HOST_REPLICA,
        'PORT': '3306',
    }
}
DATABASE_ROUTERS = ['app.db_router.DatabaseRouter']

class DatabaseRouter:
    def db_for_read(self, model, **hints):
        return 'read_replica'

    def db_for_write(self, model, **hints):
        return 'default'

    def allow_relation(self, obj1, obj2, **hints):
        return True

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        return True

GABAnich avatar Jul 07 '21 16:07 GABAnich