The scenario
We have two applications.
TheApp
TheApp is an incredible app which customers love. Each customer gets his own instance of the application, which means each customer will use a different database (name,user,password). The database connection should be decided on the domain from which the request comes in.
req: customerA.foo.tld -> db:(app_cust1, cust1, hunter2)
req: customerB.foo.tld -> db:(app_cust2, cust2, hunter3)
Administration application
Should be able to create/delete TheApp instances for the customers. Therefore it has to setup the new database and write the config to somewhere. The way which decides which db is used for the incoming request should perform well and be easy manageable.
The Question
Which is the best way to decide which database connection should be used for an instance? What performs the best? What scales best?
Answers I came up withâ„¢
I read stuff and those are the ways I came up with:
(wsgi daemon + settings.py) per instance
Each customer will get his own settings.py with the database credentials. The settings may inherit some common stuff from a shared settings file.
For each new setting file a new wsgi instance of the application has to be started. This may scale badly if we have many customers? Also creating the apache vhost files is ugly.
Using 'using' & one settings.py
I could do it like
MyModel.objects.using(THE_CURRENT_DB).all()
and set THE_CURRENT_DB somewhere (middleware thingy?) per request. But it seems ugly to have to do this
everywhere. Also the settings.py/app has to be rewritten everytime a customer gets his
instance.
One settings.py + Application Router
I didn't yet have a look if I can access any information about the request in the router, but if so, I maybe