CSE Roadmap

Common tier · about 3 hours · 5 parts

Part 4 of 5 · 2 min read

When to use server-side logic, and when not to

The real trade-off between enforcing a rule close to the data, and hiding logic from the application layer.

When to use server-side logic, and when not to

Think of a rule enforced by a building's own locked doors. Compare that to the same rule written only in an employee handbook a new hire might never read.

In plain words: a rule enforced inside the database, such as a constraint or a trigger, applies no matter which application or script touches the data, including a future one nobody has written yet. The cost is that this logic becomes invisible to anyone reading only the application's own code, and can surprise a developer who did not know a trigger existed.

A worked example. A trigger enforcing "a book's price can never be set below zero" protects every future script equally. An application-layer check for the same rule only protects whichever code paths remember to call it.

The key lines. No new code here; this part is a decision, not a mechanism.

Remember this

Enforce a rule in the database when it must hold no matter what touches the data. Keep business logic that changes often, or that needs clear visibility to every developer, in the application layer instead. Mixing both without a clear reason is how "hidden logic" bugs happen.

For the curious A real trigger surprise

A common real incident: a developer writes a bulk UPDATE expecting a certain number of rows changed, but an existing trigger on that table also updates a second table on every row, multiplying the actual work done far beyond what the UPDATE statement alone suggests. Checking for triggers before running an unfamiliar bulk operation is a genuinely useful habit.

Quick notes

  • A stored function returns a value, usable inside a query; a stored procedure performs an action, called on its own.
  • A trigger runs automatically on INSERT, UPDATE or DELETE, never called directly.
  • Server-side logic enforces a rule no matter what touches the data, at the cost of being invisible to application code.
  • A trigger can silently multiply the real work of a seemingly simple statement.
  • Keep frequently changing business logic in the application layer; keep universal, rarely changing rules in the database.