Skip to content

Support DB replicas

In order to support a Postgresql cluster, Arkindex must be able to reach a specific server for writes, and others for read.

Fortunately, it's easy to do with Django: Lookup the PrimaryReplicaRouter in https://docs.djangoproject.com/en/3.0/topics/db/multi-db/

The DATABASES configuration will look like this in prod:

DATABASES = {
  'default': {},
  'primary': {
        'NAME': 'primary',
        'HOST': 'cluster-lb',
        'PORT': 5000,
        'ENGINE': 'django.db.backends.postgresql',
        'USER': 'dbuser',
        'PASSWORD': 'dbpassword',
  },
  'replica': {
        'NAME': 'replica',
        'HOST': 'cluster-lb',
        'PORT': 5001,
        'ENGINE': 'django.db.backends.postgresql',
        'USER': 'dbuser',
        'PASSWORD': 'dbpassword',
  }
}

The custom db router must use primary for write, replica for read. Those names can be hardcoded.

This behaviour must be controllable through the YAML configuration by adding a new section to database to define the replicas:

database:
  # Define the master
  host: cluster-lb
  port: 5000
  name: arkindex_prod
  user: xxx
  password: yyy

  # Define how to access the read only replica
  replica:
    host: cluster-lb
    port: 5001

When replica is defined, the router must be added, and the DATABASES setting must use the primary/replica setup.

If a dev / client does not change its YAML config, it should not change its DB config