Config file vs Database

Sometimes, I’m confused to use config or database for storing configurations.
Where to store the site details (title, slogan, email, etc) ? Config file or database ?
Where to store the list of active plugins ?
Where to store the list of user roles ?

How to decide to store in a config file or database ?

I would not classify that as configuration. if you use a CMS, the CMS should take care of that.

if the app is not your own project (i.e. if you mean a framework’s plugins you use), then the framework/app should do that for you.

there is no need to store a list of roles. what you need to save is which user has which permission.
other than that the “list” is coded where you grant this permissions.

I’m now learning to build modular CMS. Any recommendations ? What’s the pros and cons of using either file or database?

files are generally faster to load (at least for small files), but databases are structurally coherent (and can hold near unlimited data)

My basic rule is to never hard code anything if I can avoid it, so I personally put as much into the database as possible.

My general rule-of-thumb is “will it ever be edited”. Email, logo, title, slogan etc might change down the road so I would save to DB. Admin can then edit these “settings” in their section of the site.

1 Like

I believe Drupal 8 has one of best systems for handling configuration. Long story short “configuration” is stored in YAML files. The way in which Drupal 8 implements the system caters to the needs of continuous integration very well.

To me, the question is, does your system need to scale? Storing configuration in files (which aren’t centrally accessible for all servers in a scaled architecture) means you are screwed, should configs change. You’d need to somehow make sure all the servers are sync’ed up, which can be painful. So, configuration should be in a centralized (and very safe) location. None of the frameworks today are aimed at scaled architecture, because they all store configuration in local files. If all you have is single server, then storing config data in files like that is fine. If you need to scale, you’ll need another more centralized solution like in a database. Naturally, there are other concerns, when scaling out a web application.

Also, from a PHP process standpoint, you will want your config data to be compiled into pure PHP at one point, so that you don’t have to constantly make a call to the database or a central file system or even the local file system (for other file formats like XML or YML) to load the config for each request (aka the killer bootstrap process). This would be wasteful activity.

Scott

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.