iaggoCapitanio2
iaggoCapitanio2

Reputation: 13

How Read Replica in django Tenants

I am using Django Tenant for its excellent ability to share a PostgreSQL database in a schema-based manner, which has significantly improved performance for my customers' queries. However, I now need to integrate a read replica for the database to generate reports for my users without impacting the performance of write operations.

've decided to utilize Django's default routing system for this purpose, but I'm encountering some challenges in getting it to work seamlessly with Django Tenants. After extensive research, I found a feature in Django Tenants that seems promising: EXTRA_SET_TENANT_METHOD_PATH (Django Tenants Documentation). However, I'm struggling to find examples or clear guidance on how to implement this correctly.

Here's my current setup with the default Django router system:

class DatabaseRouter(object):

    def db_for_read(self, model, **hints):
        """
        Reads go to the read replica.
        """
        return 'replica'

    def db_for_write(self, model, **hints):
        """
        Writes always go to primary.
        """
        return 'default'

    def allow_relation(self, obj1, obj2, **hints):
        """
        Relations between objects are allowed if both objects are
        in the primary/replica pool.
        """
        db_list = ('default', 'replica')
        if obj1._state.db in db_list and obj2._state.db in db_list:
            return True
        return None

I understand that EXTRA_SET_TENANT_METHOD_PATH could be the key to solving this issue, but I'm not sure how to set up the extra_set_tenant_stuff method to work with the read replica.

Could anyone provide insights or examples on how to configure Django Tenants to read from a replica database for reporting purposes while using the default router system? Any guidance or pointers to relevant documentation would be greatly appreciated.

provide insights or examples on how to configure Django Tenants to read from a replica database for reporting purposes while using the default router system.

Upvotes: 1

Views: 99

Answers (0)

Related Questions