Read replica support
See original GitHub issueIssue
I had a simple DB router:
class ReadReplicaRouter:
def db_for_read(self, model, **hints):
"""
Return one of the replicas
"""
return random.choice(["replica1", "replica2"])
def db_for_write(self, model, **hints):
# Always return the default database
return "default"
This caused errors because the search_path for the read replica was never set, only the default database.
I saw #179 which allowed you to select which database you would like to operate on, but I believe this still only allows for 1 database
Related to: #243
My solution
For my project, I have subclassed TenantMainMiddleware
and made a slight alteration to the process_request
function, replacing
from django.db import connection
#...
connection.set_schema_to_public()
#...
connection.set_tenant(request.tenant)
With
from django.db import connections
#...
for con in connections:
connections[con].set_schema_to_public()
#...
for con in connections:
connections[con].set_tenant(request.tenant)
Suggestion
Is this something that is safe for all users? I’m happy to create a pull request, maybe with a setting with a list of names of database connections that would have their connections set?
Solution full code
from django.db import connections
from django_tenants.middleware import TenantMainMiddleware
from django_tenants.utils import get_tenant_domain_model
class ReadReplicaTenantMiddleware(TenantMainMiddleware):
def process_request(self, request):
# Connection needs first to be at the public schema, as this is where
# the tenant metadata is stored.
for con in connections:
connections[con].set_schema_to_public()
hostname = self.hostname_from_request(request)
domain_model = get_tenant_domain_model()
try:
tenant = self.get_tenant(domain_model, hostname)
except domain_model.DoesNotExist:
return self.no_tenant_found(request, hostname)
tenant.domain_url = hostname
request.tenant = tenant
for con in connections:
connections[con].set_tenant(request.tenant)
self.setup_url_routing(request)
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:15 (5 by maintainers)
Top Results From Across the Web
Amazon RDS Read Replicas | Cloud Relational Database
Amazon RDS Read Replicas are one or more replicas of a given relational database instance to serve high volume read traffic in order...
Read more >Amazon RDS Read Replicas: 4 Easy Methods - Learn | Hevo
Amazon RDS Read Replication is a feature on Amazon RDS that allows you to create one or more read copies of the primary...
Read more >Create read replicas | Cloud SQL for MySQL
A read replica is a copy of the primary instance that reflects changes to the primary in almost real time, in normal circumstances....
Read more >Working with MySQL read replicas - 亚马逊云科技
With cascading read replicas, your RDS for MySQL DB instance sends data to the first read replica in the chain. That read replica...
Read more >AWS RDS Read Replicas - Jayendra's Cloud Certification Blog
RDS Read Replicas provide enhanced performance and durability for RDS. · RDS Read Replicas allow elastic scaling beyond the capacity constraints ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@mmcclelland1002 great work on that PR! Are you able to show the code you’ve written to work with the
EXTRA_SET_TENANT_METHOD_PATH
feature?@adnathanail @dendrochronology I made a PR for this feature: https://github.com/django-tenants/django-tenants/pull/760 Please feel free to contribute and/or support this change to get merged in!