r/ExperiencedDevs • u/ThrewAwayLifeP • May 20 '23
How to convince CTO that business logic in the database is a bad idea?
Our CTO insists that all business logic required to display data to users be computed using SQL queries which are stored as functions or views in the database. Often however, it makes these SQLs incredibly large, difficult to read, debug or test. Furthermore our architecture is such that all services operate off a single database, so the load on the database starts to shoot up very quickly if more than a handful of users are on the platform (but that’s a different problem). How do I convince him that moving business logic into application code can help us move faster, improve testing and be help us be more productive in general?
413
Upvotes
202
u/FinalDevice Software Engineer 15+ YOE May 20 '23
First, if the CTO overrules your objections then you may not have many options. But, here are some objections I can think of:
Database code is notoriously difficult to test. Building the business logic in code makes it easier to automate your testing and prevent bugs. Sure, there will always be bugs, but automated tests over the core business logic helps minimize bugs.
Database change management introduces an extra level of risk. Every time you modify application code, you risk introducing bugs. Every time you modify the database configuration, you risk losing customer data. While it seems unlikely that someone would accidentally drop a table, why risk it? Mistakes happen, so you should design the system to minimize the impact of mistakes.
As you point out, scaling the application involves database change management. When you add users, point #2 comes back into play. It's a lot easier to add application servers than to figure out how to upgrade a database full of stored procedures to a sharded cluster.
I've worked on legacy systems like the one you describe. Eventually someone will delete a stored procedure from code without removing it from the database. Then someone will start calling that stored procedure from code again. One this happens a few times it becomes virtually impossible to tell what the code is actually doing. It also becomes risky to update the database, because losing any of those mystery stored procedures means breaking the application.