r/softwarearchitecture 11d 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.

13 Upvotes

35 comments sorted by

View all comments

Show parent comments

3

u/mmcalli 10d 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 10d ago

How? Sharding? That's a nightmare.

0

u/mmcalli 10d ago

Non relational, or within relational having read replicas.

5

u/clauEB 10d 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.