Question

The default configuration of a backend for a Twelve-Factor App is to put its (network) location in an environment variable, e.g. DB=mysql://user:pw@host/db_name.

But what is the recommended way if your backend configuration is more dynamic? In my case the app could use 1 to n backends depending on the environment. My idea is to use a serialization format like JSON and put it in a single variable like

DB="{1: 'host:port', 2: 'host:port'}"

Is this the preferable way to deal with such a situation or what would be better alternatives?

Pas de solution correcte

Autres conseils

I don't know if that is the "preferred" method, but I think it would work.

You will want to document this idiosyncrasy for future developers, and maybe add some verbose errors/logging pointing to misconfigurations in this unusual setup. If you only want to specify one database, for example, it should be clear that it needs to be specified in quasi-JSON style anyway.


I think more 12-factor-y, though, would be to set up an intermediate load-balancer between your servers. That way, your app can connect to the load balancer or a single server with very little transparency as to the nature of the underlying service.

So instead of:

SetEnv DB {mysql://something@somewhere, mysql://something@somewhereelse}

you would have

SetEnv DB mysql://something@load-balancer

and the load-balancer handles the multiple databases behind the scenes.


You could also have a 2-db system, one for reads and one for writes. The reads could be load-balanced and replicate the master-server.

SetEnv READ_DB  mysql://something@load-balancer
SetEnv WRITE_DB mysql://something@master-server

then in a test situation they would use the same connection string:

SetEnv READ_DB  mysql://something@load-balancer
SetEnv WRITE_DB $READ_DB  # not sure about syntax here
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top