Multi-tenant ERP
An ERP from scratch (finance, accounting, accounts payable and HR) built to hold more than one client in the same database without leaking a single row between them.
The problem
A small company that buys an off-the-shelf ERP pays for modules it never uses and cannot change anything. A company that commissions a custom one pays a lot and stays locked to whoever built it. I wanted to understand what sits in between: a system where each client is a tenant in the same database, with the same modules, and the difference between them is configuration, not code.
The interesting part is not writing the screens. It is that the moment two clients share a database, every programming mistake becomes one client’s data leaking into another’s. That is the real problem of this project, and it is what the decisions below are about.
Technical decisions
A global tenant filter that fails closed, not open
At first every query repeated WHERE tenant_id = @x, and then came a global filter with a nasty flaw: if no tenant was resolved, it released the rows of every tenant. Today the DbContext applies an automatic filter to every entity implementing ITenantScoped, and with no tenant in context the query returns nothing.
The price shows up on the rare path. Anyone who needs a deliberate cross-tenant read has to write IgnoreQueryFilters() by hand, with a comment next to it saying why, and an architecture test breaks the build if the comment is missing. It adds friction exactly where code is almost never touched, and in exchange the common mistake stops being possible. One corner is worth recording: the design-time factory runs with no context, so it filters everything out. That is harmless, since it only runs migrations, but I wrote it down in the code itself so nobody “fixes” it wrongly later.
One attribute per controller, which both declares and enforces the permission
Each action used to rewrite its own access check, and discipline failed in practice: one commit leaked data precisely because it trusted the check that was already there, not because someone disabled it. So authorization became a single attribute at the top of the controller, [TenantModule("HR.Employees")], which says which resource that controller exposes and gates it on the same line.

Data is read by the tenant in the route (api/tenants/{tenantId}/…), so that is the tenant the attribute checks membership against. A token selected for another tenant gets a 403, because authorizing against one tenant and reading from another is worse than not authorizing at all. The consequence is that route and token are now coupled, and a client that builds the “wrong” URL gets an error instead of data.
An architecture test that breaks the build when someone forgets
I did not want to rely on code review to remember the attribute. So a test walks the controllers and fails if any action under {tenantId} was left without the gate. A new endpoint written in a hurry does not get through.
But, and almost nobody writes this part down, the test catches forgetting, not getting it wrong. A second test rejects resource codes that do not exist in the catalog, but an employees controller declaring HR.Payments, a code that exists and is the wrong one, passes both and stays wrong. I would rather know exactly where the net has a hole than assume I am covered.
The API connects as a database user that cannot change the schema
The API used to connect as postgres. A superuser ignores every permission check, so one stray DROP SCHEMA public CASCADE would take all 25 tables with it and no grant would stop it. Today there are two database users: one owns the tables and is the only one that runs DDL, the other only reads and writes data. The API uses the second. Transferring ownership of the tables was half the work: revoking CREATE while the application still owned them would have left DROP TABLE just as open.
That means the API can no longer run migrations by itself on startup, since that was what forced it to hold DDL rights, and migrating became a separate deploy step. So that a stale schema does not break later somewhere that does not explain the cause, the API checks for pending migrations on startup and, if any are missing, refuses to serve and prints the command to run. A deploy that forgets the migration now does not come up, instead of coming up broken.
One fixed-cost machine, not a service that scales to zero
The first version ran entirely on Azure: Container Apps, ACR, Key Vault and managed PostgreSQL, scaling to zero when nobody was using it. It looked cheap, but Container Apps charges for cold starts and managed PostgreSQL has a price floor that no amount of idleness lowers.
The last design was on AWS: one 2 GB Lightsail instance in São Paulo running four containers with docker compose (Caddy for TLS, the Angular nginx, the API and PostgreSQL 17), with images in ECR, pushed by GitHub Actions through OIDC, with no AWS key stored in the repository. It came to about US$ 14 a month, fixed. It is a single machine, without the redundancy of a managed service, which suits a system that is just starting and would not suit scale.
What was left out
Caching. There was a Redis module written, working and wired to nothing: the API had an ICacheService that nobody ever injected. I preferred deleting it to pretending it was part of the system. It comes back when caching earns its own cost.
The Azure version was torn down. The AWS one is all Terraform, and every pull request goes through fmt and validate against the real provider before it gets in. The infra repository stays private, because the machine now hosts another project.
A friend tried the system out but barely used it, and in October 2026 I took the ERP offline. The project is stopped and the code is public again.
[Stack]
- .NET
- PostgreSQL
- Angular
- Terraform
- AWS
- Docker
[Links]