I have a number of attributes I need for various page loads and other backend tasks, and I'm debating on whether storing these things in a database or calculating them on the fly.

For instance, if there are files that users can upload, and you want to track the size, space taken, format, etc. would it be better to calculate these things once and store them along with the location of the file in the database, or just grab the file each time and get the file attributes manually?

Another use case is shopping cart items. Is it better to calculate the price of an item and store that in a row with the shopping cart table, or calculate the given price each time a page loads. In this case, changes to the price based on site-wide sales, discounts, markups, etc. would not be reflected once the item has been added to the cart unless the prices are updated through another method when sales/discounts/markups are applied. This isn't the best example, but hopefully you understand the idea; maybe you have a better example.

In both of these examples, the source material is available to get the answers from which is key to the question. Obviously, one has a lot overhead for every page load would could be a lot depending on the situation, however the other seems to have less dependence on database integrity in terms of making sure it is always accurate and up-to-date (which I think I prefer). I'm not looking for a specific answer here, because I'm sure it will depend on many variables, but I am looking for a best practice or a method to determine the best solution.

NOTE: This is a similar question but has gotten very little response and no answers.

  • 1
    Both use cases are valid questions, and really are better answered separately. As is I think this question is too broad. The file question and price question are both answerable as separate questions on the site - and good questions too. Commented Aug 23, 2018 at 20:42
  • Ditto @GregBurghardt comment. And asked separately I'd bet there are relevant answers in stack Overflow/SW Engineering
    – radarbob
    Commented Aug 24, 2018 at 19:15
  • Thank you for your suggestions. This questions was asked on stackoverflow but was recommended I ask here. If they are separate questions, I suppose I would ask for answer that illustrates the different considerations in play and make recommendations for deciding on my own in future situations. Is this too much?
    – user58446
    Commented Aug 25, 2018 at 22:35

1 Answer 1


For so many reasons I think it's best to store all attributes (that you mentioned) in the database.

  1. Better search results for user

  2. Easy to maintain for all platforms (web, mobile, voice enabled assistants etc) because you are avoiding all the conversion logic by storing in database.

  3. Faster loading on any kind of device irrespective of network speed and client device capability. Today most of the companies prefer storing redundant data if it helps making service better.

  4. Stored data can be used for machine learning, data analytics etc.

Things like converting currency, timezone can be done outside of database if necessary. But main copy of the attributes you mentioned are suitable to be stored in database.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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