82

NOTE The audience of programmers.se and dba.se is different, and will have different viewpoints, so in this instance I think it's valid to duplicate What are the arguments against or for putting application logic in the database layer? on programmers.se.

I couldn't find discussion on dba on this already, and the original post says it all, so:

Most software developers want to keep application logic in the application layer, and it probably feels natural for us to keep it here. Database developers seem to want to put application logic in the database layer, as triggers and stored procedures.

Personally I would prefer to keep as much as possible in the application layer to make it easier to debug and keep the responsibilities of the layers separate.

What are your thoughts on this, and what should or should not be ok to implement in the database layer?

N.B. I'm not the OP for that question, but left the original wording intact.

Phil Lello
  • 1,469
  • 1
  • 11
  • 9
  • 4
    Comparing the answers here and on SO, the gap is striking. The developers protest at the delays entailed from centralizing processes in the database, but to the DBAs that's a good thing. Forcing people to put more time and effort into requesting a new view or sproc reduces the number of contact points with the data, making it easier to maintain consistency and reducing the number of points of optimization. – Jon of All Trades Apr 10 '12 at 21:35
  • 1
    It seems to me that the answers here assume a certain way of using the database (multiple applications, allowing some users direct database access, etc) I think that's the main reason for the difference. – JMD Coalesce Oct 12 '17 at 09:57

10 Answers10

65

Assorted thoughts...

Your database code will outlive your application client technology. Think of ADO.NET -> Linq -> EF as well as assorted ORMs. Whereas you can still run SQL Server 2000 code from last millenium against all of the above client technologies.

You also have the multiple client issue: I have .net, java and Excel. That's 3 sets of application logic.

"Business logic" shouldn't be confused with "data integrity logic". If you do have clients starting transactions and doing assorted checks, that's a lot of db calls and a long transaction.

Application logic doesn't scale for high data volumes. We have 50k rows per second using stored procs. A sister team using Hibernate can't get one per second

gbn
  • 69,809
  • 8
  • 163
  • 243
43

I want all the logic that has to apply to all users and all applications in the database. That's the only sane place to put it.

The last Fortune 500 I worked at had applications written in at least 25 languages hitting their OLTP database. Some of those programs moved to production in the 1970s.

The alternative to implementing this kind of requirement in the database is to let every application programmer reimplement all or part of it 100% correctly, every time they fire up their editor, from the day they first walk through the door until the company goes out of business.

What are the odds?

Isn't this the single biggest "don't repeat yourself" on the planet?

31

I'm moving my old answer across unedited from programmers.se, as answers seem pretty polarised between sites.

I know I'm in for a world of hurt here, but put business logic in the database because:

  • You can allow business power users direct access to the database and not worry about them screwing it up (or worry less than you would with app-based logic)
  • A power user can create new reports without waiting for a new software release.
  • You can test SP / TRIGGER code in an a copy of the database, just like you do testing app-based logic
  • You can keep the SQL to create sp's and triggers in text files (you should be doing this anyway for table/view code)
  • You can mix and match languages without porting business logic
  • You can make changes to business logic without upgrading every bit of software
  • You audit structure changes the same way you audit database activity - via logging
  • Vastly improved security and fine-grain access control (most app-based logic implementations use their own security model, so the data is far easier to compromise. Reversible password encryption is not uncommon)
  • Database-side user security greatly reduces the damage/theft rogue SQL can do

The cons are: - Developers threatened when users become less reliant on developers for custom reports - Developers need to learn another programming language

Neither of those should matter to a skilled developer.

Interesting to note, most answers talk in terms of 'application logic', not 'business logic', as if the software isn't there to provide a business function.

Phil Lello
  • 1,469
  • 1
  • 11
  • 9
  • 1
  • Stored procs/triggers can provide a level of abstraction that allows you to make structural changes in the database without having to change all of the application code. * Not every user of the database will faithfully use your middleware. * C'mon, a foreign key is a business rule!! * Removing all checks/constraints/code from the db means it can't protect itself against inconsistency/corruption. * Every app doesn't call for queue-driven transactionless designs like the one eBay developed after they became successful and could afford to build that. * SQL isn't all that hard, folks.
  • – Craig Tullis Apr 01 '14 at 01:33