Scrivio Interview library
LeadershipAdvanced7 min read · updated 12 November 2026

Two senior engineers disagree: one says 'the database is just storage, all logic in Java', the other says 'data integrity must be enforced in the database'. You must set a guideline for 40 teams. What would it say?

The guideline would say that both engineers are partly right, and draw the line by the kind of rule. Integrity that must hold for every writer lives in the database as keys, foreign keys, unique and check constraints and proper types; business workflows, rules that change often and integrations live in services with tests and versioning. Set-based data work may use SQL freely, procedural database code needs a stated reason, triggers are limited to narrow technical jobs, and the whole guideline is backed by real examples and an exception process.

Why this question is asked

When two respected seniors disagree in absolute terms, forty teams will pick whichever slogan suits them, and the company ends up with both failure modes at once: databases with no constraints, where a bad import corrupts data silently, and databases full of hidden triggers that nobody dares to touch. Interviewers ask this question to see whether we can turn a debate into a rule people can apply on a Tuesday afternoon without a meeting. In this answer, we will look at what each engineer is really protecting, write the guideline section by section with the reasoning behind each line, and then see how to make forty teams actually follow it.

1What Each Engineer Is Protecting

The engineer who says the database is just storage has usually been burned by logic hidden in stored procedures and triggers: behaviour nobody could test, deployments that had to be coordinated with a DBA, and business rules that only one person understood. They are protecting testability, change speed and the ability to scale code horizontally.

The engineer who says integrity must be in the database has usually been burned by a different incident: a batch job, a second service or a manual fix that bypassed the Java validation and wrote orders without customers or negative stock. They are protecting the data, which outlives every service written against it.

Both concerns are valid, and they are about different kinds of rules. That observation is the core of the guideline: we do not choose a side, we classify the rule.

2Rule One: Integrity for Every Writer Goes in the Database

The first line of the guideline says that any rule which must be true of the stored data, whoever writes it, is enforced by the database. That means primary keys on every table, foreign keys between related tables, UNIQUE constraints for natural uniqueness such as one email per account, CHECK constraints for simple value rules, NOT NULL where a value is required, and sensible types, for example numeric for money, timestamptz for instants and date for dates, instead of text everywhere.

The reason is coverage. Java validation protects only the paths that go through that Java code. A constraint protects every path, including the migration script someone writes in two years. Constraints are also cheap to run, declared in one line, and visible in the schema, so they do not have the downsides that made the first engineer suspicious of database logic.

SQL
-- Required in every new schema, reviewed in every migration
ALTER TABLE order_line ADD FOREIGN KEY (order_id) REFERENCES orders (id);
ALTER TABLE account ADD CONSTRAINT account_email_unique UNIQUE (email);
ALTER TABLE order_line ADD CONSTRAINT qty_positive CHECK (quantity > 0);
UPDATE order_line SET quantity = 0 WHERE id = 5; -- ERROR: violates check constraint "qty_positive"

3Rule Two: Workflows and Changing Rules Go in Services

The second line says that business workflows, rules that change often and anything that talks to other systems live in service code. Approving a loan, calculating a discount, deciding when to send a reminder: these change with the business, need many test cases, must be released with feature flags and rolled back quickly, and often call other services. Application code gives us unit tests, code review in the same repository, debuggers, tracing and horizontal scaling.

A useful test the guideline can offer is the question: would the product manager ask to change this rule? If yes, it belongs in a service. If the rule is a property of the data that no one would ever want to relax, such as an order line belonging to an existing order, it belongs in a constraint.

4Rule Three: Set-Based SQL Yes, Procedural Code With a Reason

The third line protects good SQL from the first engineer's slogan. Bulk updates, reporting queries, data migrations and aggregations should use SQL heavily, because the work runs next to the data and no rows travel over the network. Telling teams to load a million rows into Java to update them one by one would be a bad rule.

Procedural logic in the database, meaning PL/pgSQL functions or stored procedures with loops and branches, is allowed but needs a written reason, such as a measured performance gain that other approaches cannot match. When it is used, it lives in the repository, ships through migrations, has automated tests and is reviewed like Java code.

5Rule Four: Triggers Only for Narrow Technical Jobs

Triggers are the part of database logic that causes the most surprises, because they run invisibly on every write. The guideline allows them only for narrow technical uses: maintaining updated_at, writing an audit row, or keeping a denormalised search column in sync. Each trigger is documented in the schema with a comment and listed in the team's runbook. Business rules in triggers, such as a trigger that creates an invoice when an order changes status, are not allowed, because nobody reading the service code would ever find them.

The guideline on one page
Kind of ruleWhereExample
integrity for every writerconstraints and typesFK, UNIQUE, CHECK (qty > 0), numeric money
changing business rulesservice codediscounts, approvals, reminders
integrationsservice codepayment provider, email, events
set-based data workSQL statementsbulk status update, reports
procedural database codeonly with a written reasonmeasured hot path, tested, reviewed
triggerstechnical jobs onlyupdated_at, audit rows
A rule people can apply alone, without a meeting.

6Making Forty Teams Follow It

A guideline that is only a page on a wiki changes nothing. To make it stick, I would back every rule with examples from our own systems: the incident where a missing foreign key let an import create orphaned payments, and the outage where a hidden trigger doubled the time of a bulk load. Real stories from our company persuade engineers far better than principles.

I would also make the defaults automatic. A schema linter in the migration pipeline can flag tables without primary keys, foreign key columns without constraints, money stored as double, and new triggers. Exceptions are not forbidden, but they go through a short review in the architecture forum, are recorded as decision records, and are revisited after six months. Finally, I would bring both senior engineers into writing the guideline. Each of them owning half of it is the fastest way to end the disagreement for the teams watching them.

7Follow-up questions

What if a team says constraints slow down their inserts?

Constraints do cost something, mostly the index lookups behind foreign keys and unique checks, but the cost is usually small compared to the network round trip. We ask them to measure. If a specific constraint is a measured bottleneck on a very hot path, that is a documented exception, and we usually find that a missing index on the referencing column was the real problem.

Should validation then be duplicated in Java?

Yes, for user-facing rules. The service validates to give a friendly error message early, and the constraint stays as the last line of defence. The duplication is deliberate: they serve different purposes, one for the user experience and one for the data.

How do you handle legacy systems that break the guideline?

We do not demand a rewrite. The guideline applies to new code and to code being changed anyway, and teams add constraints to old tables when they touch them, after checking existing data with a query that finds violations. Hidden business triggers get documented first and moved into services when the area is next reworked.

8Conclusion

In this answer, we saw that the two engineers protect different things, testable and changeable logic on one side and data that every writer must respect on the other. The guideline therefore classifies rules: integrity in constraints and types, workflows, changing rules and integrations in services, set-based work in SQL, procedural database code only with a reason, and triggers only for narrow technical jobs. It works when it is backed by our own incidents, automated checks in migrations and a visible exception process.

What to say in the interviewAbout 90 seconds aloud

Open with

I would not pick a side, because the two engineers are protecting different things. One wants business logic to be testable, versioned and scalable; the other wants data to stay correct whoever writes it.

Then, if they want more

So the guideline classifies rules. First, integrity that must hold for every writer goes in the database: primary keys, foreign keys, unique and check constraints, NOT NULL and proper types such as numeric for money. Those protect us from batch jobs, second services and manual fixes that skip the Java validation, and they cost one line each.

Second, business workflows, rules that change often and integrations go in services, with unit tests, versioning and feature flags. A quick test is whether a product manager would ever ask to change the rule; if yes, it is service logic.

Third, set-based data work such as bulk updates and reports should use SQL heavily, but procedural code in the database needs a written reason and is versioned, migrated, tested and reviewed like Java. Fourth, triggers only for technical jobs like updated_at or audit rows, documented, never hidden business rules.

To make forty teams follow it, I would illustrate each rule with an incident from our own systems, add a schema linter to the migration pipeline, and run exceptions through the architecture forum as recorded decisions. And I would ask both engineers to co-author it.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Two senior engineers disagree: one says 'the database is just storage, all logic in...": https://scrivio-interview-library.netlify.app/databases/stored-code/98fdfd9c2f/