Multiple Databases in a Single Django Project
Django doesn’t yet support splitting your project or models across multiple databases. To scale up your database you use a solution where the application code still believes that it’s connecting to a single database server and database, but you’re actually connecting to a meta-db-server and db (i.e., a federated database). What I describe below is a way to get around that for a specific case where you can dispatch to a different db for different requests.
The first way is to segregate your requests for different DBs into their own Python sub-interpreter in mod_python or mod_wsgi. If you configure each of requests to run in their own Python sub-interpreter (using mod_python or mod_wsgi), you’ll be loading your entire Django project for each of the databases you’re serving.
Using mod_wsgi you’d put something like this in a VirtualHost in your httpd.conf file:
#This maps a hostname to a database
#Set the DB_NAME environment variable for use by Django
RewriteEngine on
RewriteMap host_db_map txt:/etc/httpd/conf/host_db_map.txt
RewriteRule . - [env=DB_NAME:${host_db_map:%{HTTP_HOST}|DEFAULT_DATABASE}]
#We run each in its own Python sub-interpreter with the WSGIApplicationGroup directive
WSGIApplicationGroup %{ENV:DB_NAME}
From my experience, and what I’ve read on the Web, loading Django itself takes roughly between 20 and 30MB of memory, and you’d be loading a Django instance for each one of the databases you add. But there’s another way to do this and continue to load a single Django instance.
Through custom middleware you can set the settings.DATABASE_NAME you want to use based on information contained in the request. The code for that would look something like this:
from django.conf import settings
class DBChooserMiddleWare(object):
“"”
Set the settings.DATABASE_NAME to match the http host
“"”
def process_request(self, request):
#choose_db returns a database name given an HTTP_HOST
#but it could be any sort of logic to choose a db or db server here
settings.DATABASE_NAME = choose_db(request.META[’HTTP_HOST’])
return None
Comments »
No comments yet.
RSS feed for comments on this post.
Leave a comment
Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>
