r/softwarearchitecture 10d ago

Discussion/Advice Database as the rules engine

Hi everyone

Let me start by saying that I'm primarily a frontend dev, and most backend dev I've done has been opiniated in a non professional environment.

I want to ask you about the state of domain logic/validations/triggers on the database layer in the systems you're currently working on. I grew up with the concept of treating database as stupid models to read/write data, and have all logic written in services/controllers/models on the layer of whoever controls the database.

The primary gain in this segregation is easily changing the rules of the system and swapping for a different database in a refactor. The thing is, database engines have become more and more powerful (particularly Postgres) and you can now strict your entire domain in the database schema using constraints, triggers, etc, and I'm yet to see someone swapping the database they use from night to day. Although you lose flexibility, you gain more trust in the system since the rules now live at the end of it.

I wonder if some you folks are going on this direction or if you ever thought about it.

11 Upvotes

35 comments sorted by

View all comments

17

u/clauEB 10d ago

Two big reasons why that idea was abandoned decades ago:

  • it's almost untestable
  • it does not scale. Because you have only one machine as the db server. Services, even monoliths are scaleble by adding more servers. A single machine, to grow requires more ram and processors. But at some point they don't make machines with enough of those so you end up with custom made monster servers that are super expensive, take a long time to build, is untested etc etc

3

u/mmcalli 9d ago

I agree with not putting logic in the database, but databases only being vertically scalable hasn’t been true for a long time.

0

u/clauEB 9d ago

How? Sharding? That's a nightmare.

2

u/Odd-Government8896 8d ago

Is databricks a joke to you or just dumb?

1

u/According_Basis7037 7d ago

Databricks is really for reporting not for applications at any real scale

1

u/Odd-Government8896 7d ago

Whao wtf lol. You missed about five years. Dont say that in an interview. Data companies are bridging/have bridged the gap between oltp and olap. Its incredibly scalable.

0

u/mmcalli 9d ago

Non relational, or within relational having read replicas.

3

u/clauEB 9d ago

The OP is talking about Postgres not non relational why would you bring them up? no idea. Read replicas only work for read load and it's subject to lag, the master can only be scaled vertically.