Should you always write to the database

Certified Lead Developer

Hello,

I think common practice is to always call a write to database node if a value could have changed. I was asked by our DB team if this was really needed. They proposed only calling the write to DB when a value has changed. I am wondering if anybody has implemented this before? I have several ideas how to do this, but do not know if it really adds value. Caching a value, or an additional query is properly going to take more resources than just overwriting data with the same values.

 

-Josh

  Discussion posts and replies are publicly visible

  • Hello Josh,

    One of the best practices is to think on the reports when designing your database. Most of the times when you change from screen to screen you must be changing something on the flow and the user taking action(entering information or taking a decision or changing the status of something). So you end up with the need to write “last updated by” and “last updated at”.

    This doesn’t apply when you are showing multiple screens read only and not changing the information at all. If you want to store how the user navigates then you are now writing to the database like an audit trail.

    But if you are not doing nothing to the information, recording a decision or doing something please don’t call the write to the database again on that entity. From the screens you might be able to know if something changed. And then you can decide if you will call the write to DB or not.

    As a common practice I do is to have 1 process model named “write to DB” which based on the inputs (pvs) you decide if you write to the DB or not.

    I can recommend to be careful on calling the database because call you make to the DB you consume when you are not saving other information . actually I would recommend to use the write to multiple to reduce the times you hit the DB.

    So, they are asking you a reasonable measure to reduce the hits to the database which is a good practice.

    To check if a value have changed you have multiple ways to do it, just one flag for each cdt and update the flag to true if something changed on the screen, and false if it was reverted. I don’t think it is a big deal to implement it on a screen and it would be beneficial in the long term.

    Hope this helps
    Jose
  • Hey Josh,

    This can be put up like how long / the importance you need the information.
    Suppose if you don't need the data for a longer duration like displaying the previous data in quick tasks etc.
    In the above case if the data is not of that importance and can be pulled from active instances, then we don't need any DB intervention.

    Suppose if its critical and you are also using that data anywhere else, it requires DB intervention.

    Hence you can categorize as if you can pull up that data directly from the pv's and maintain and display accordingly OR storing to DB.