0

I have an application written in a Django framework that draws off a PostgreSQL database.

Because we're very sensitive of downtime and redundancy, the database uses streaming replication to two hot-standby servers. However, as it currently stands when the master DB goes down I have to manually change the database server address in settings.py for Django to switch over.

I'm not terribly familiar with Django database routing yet, so I'm not sure how to go about it, but would it be possible to configure all three databases in Django to have the routing layer automatically determine which one of them is currently the active master and route its queries accordingly?

(During run time would be perfect, but I'll cheerfully settle for 'at start up' and add a line in the procedures document to restart the uwsgi vassal when reacting to a database failover).

Shadur
  • 345
  • 3
  • 18

1 Answers1

0

You can use multiple databases using Django. ORM has really good support of it. This is possible from settings.py. Also, it's possible to set default database, which will be used by Django (As a default).

This is from documentation

DATABASES = {
            'default': {
                'NAME': 'app_data',
                'ENGINE': 'django.db.backends.postgresql',
                'USER': 'postgres_user',
                'PASSWORD': 's3krit'
            },
            'users': {
                'NAME': 'user_data',
                'ENGINE': 'django.db.backends.mysql',
                'USER': 'mysql_user',
                'PASSWORD': 'priv4te'
            }
        }

keys of DATABASES dict are just "aliases".

Django's QuerySet supports using(db_alias) method which helps you to make routing between databases.

gachdavit
  • 1,223
  • 1
  • 6
  • 7
  • 2
    Yes, I'd gotten that far, but what I need is for Django to automatically decide which to use depending on which database is open for writes (and therefore, the current Master) – Shadur May 22 '19 at 07:43
  • If i understand correctly your question,,, When django performs queries, for example Mode.objects.all() (This case 'default' is used)... Model.objects.all() <==> Model.objects.using('default').all() – gachdavit May 22 '19 at 07:45
  • 1
    I've amended the question, because apparently I wasn't entirely clear. I know how to use multiple databases. What I *want* is for django to *automatically* determine which database it should be using depending on which of them is currently the master. – Shadur May 22 '19 at 07:49
  • You would need to define a Database router, check if the Django is able to make a connection to database. If not, return a different database. from `DATABASES` settings. Please follow this post for more details: https://stackoverflow.com/questions/26608906/django-multiple-databases-fallback-to-master-if-slave-is-down – Vidya Sagar May 22 '19 at 07:49
  • I think you're asking too much of Django, this is not what it is made for. In my opinion, a better solution is to have a CNAME for your database and some other infrastructure that pings the master database and updates the CNAME to one of your standby databases when the primary is unresponsive. Django will be unaware that anything changed, but the DNS will route the queries to the right place. – Brad Martsberger May 22 '19 at 13:12
  • @BradMartsberger I'm pretty sure using DNS to handle failover isn't even *remotely* fast enough for what I'm trying to accomplish. – Shadur Dec 18 '19 at 20:34