I have several web applications. All of them have config files. I'm trying to figure out when it's appropriate to store data in a config file and when in database in json or free format? For instance, I have setting for several payment methods: PayPal, Stripe, etc. All of them have different sets of settings, some require an API key, some this, some that and not that. And so on.

When is it better to store such types of config settings in a database and when in config yaml/xml/json files?

And in general, not only these types of config settings, but other types as well.

  • 3
    There are many opinions on this. One thing you can't store in a database is that database's own connection parameters (e.g. jdbc url, or odbc connection string). Other things have various pros and cons. – joshp Jun 5 '18 at 5:06

Since you're specifically asking about those two options I won't go deeply into the 12factor pattern [1] which states:

An app’s config is everything that is likely to vary between deploys. [...] Apps sometimes store config as constants in the code. This is a violation of twelve-factor, which requires strict separation of config from code. [...] Another approach to config is the use of config files which are not checked into revision control.

And finally:

The twelve-factor app stores config in environment variables.

Short answer: If the config, by it's nature can change at runtime, e.g. on a per-user level (so if it's actually a feature of your product like change background color), then put it in the database. If it's integral for your code to work, like database connections, api keys etc. there is no need to accept the overhead of storing it into a another system (which your DB is).

Whilst you can put it in config files, you're life will be better when storing it in env variables. That way it's easy to adjust your code for multiple environments. It's also a good practice not to put sensitive information into version control.

[1] https://12factor.net/config

| improve this answer | |
  • Good point. In addition, one has to assume that access to database may not be successful. In such a case, you still need the application to continue communicating with the user. If the settings are are all in the database (except connection info.), the application will fail. In addition, sensitive settings and settings that affect individual users may better be in a db. – NoChance Jun 6 '18 at 3:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.