Architecting a Multi-Tenant SaaS Billing Platform: A Relaso Engineering Deep Dive
The Complexity of Financial Multi-Tenancy
Building a SaaS application is challenging. Building a SaaS financial application is an entirely different beast. When you are dealing with other companies' money, tax ledgers, and compliance documents, the standard 'move fast and break things' methodology does not apply. In this technical deep dive, we are pulling back the curtain on the Relaso Billing Pro architecture to explain exactly how we engineered a multi-tenant system that guarantees strict data isolation, penny-perfect transaction integrity, and regulatory compliance.
1. Strict Data Isolation at the ORM Level
In a global multi-tenant architecture, thousands of different companies share the same underlying database infrastructure. The absolute worst-case scenario for a SaaS platform is 'data bleed'—where Company A accidentally sees Company B's invoices or customer list.
To prevent this, we rely on a heavily guarded Prisma schema where absolutely every core entity—whether it is an Invoice, a Product, a Contact, or a JournalEntry—contains a mandatory organizationId foreign key. But schema design is only half the battle; the enforcement must be foolproof.
We implemented a strictly typed Repository layer (e.g., InvoiceRepo) that acts as an impenetrable gateway between the Next.js API routes and the database. The Repository methods enforce that an organizationId must be passed into every single findMany, update, or delete query. It is syntactically impossible for a developer on our team to write a query that fetches 'all invoices' without scoping it to the currently authenticated user's organization. This defense-in-depth approach ensures that even if an API route is misconfigured, the data access layer will reject the cross-tenant query.
2. Penny-Perfect Financial Integrity with Database Transactions
A billing platform cannot afford race conditions. Imagine a scenario where two accountants from the same company click 'Finalize Invoice' on two different computers at the exact same millisecond. If the system is not perfectly atomic, both invoices might be assigned the number 'INV-2026-004', or stock levels might be deducted twice.
To solve this, Relaso utilizes Prisma's $transaction API extensively. When an invoice is finalized, a complex chain of events must occur perfectly or fail completely:
- The draft invoice status must be updated to FINALIZED.
- A unique, sequential invoice number must be acquired and locked.
- The inventory count for every line item must be decremented.
- Double-entry accounting journals (Dr Accounts Receivable, Cr Sales Revenue, Cr GST Payable) must be posted to the ledger.
- Commission logic for Affiliate Agents must be calculated and accrued.
By wrapping this entire 5-step process in an ACID-compliant database transaction, we guarantee that if the inventory deduction fails due to insufficient stock, the invoice is never finalized, and the journal entries are immediately rolled back. There are no 'half-finished' states in Relaso.
3. The Gapless Sequence Generator
Under GST and international tax laws, tax invoice numbers must be strictly sequential (e.g., INV-001, INV-002) with absolutely no gaps. A missing invoice number triggers an immediate red flag during a tax audit.
Standard database auto-incrementing IDs are terrible for this. If a transaction rolls back, standard databases often 'burn' that auto-incremented ID, leaving a gap. Furthermore, in a multi-tenant system, every organization needs its own independent sequence (Company A is on INV-005, Company B is on INV-999).
We engineered a dedicated 'Sequence Table' that acts as a locking mechanism. When an invoice finalizes, the system runs a SELECT ... FOR UPDATE on the specific organization's sequence row. This locks the row exclusively. It reads the current prefix (e.g., 'INV-'), reads the current counter (e.g., 42), increments it to 43, assigns 'INV-43' to the invoice, and releases the lock. If two requests come in simultaneously, the database queue forces the second request to wait a few milliseconds until the first request completes and releases the lock. The result? 100% gapless, thread-safe invoice numbering at any scale.
4. Immutable Audit Trails and Soft Deletes
In financial systems, you can never truly delete anything. If a user deletes a product from the catalog that was used on an invoice three years ago, that historical invoice must still render perfectly during an audit.
To achieve this, we do not use SQL DELETE statements for core financial entities. Instead, we use 'Soft Deletes'. Every table has a deletedAt timestamp column. When a user deletes a product, we simply stamp the current time in that column. Our Repository layer automatically filters out any records where deletedAt !== null for all catalog dropdowns and list views. However, historical invoices joining to that product ID will still successfully resolve the relationship, ensuring your past financial data remains utterly immutable and compliant.
Conclusion
Building a robust SaaS billing engine requires prioritizing integrity and isolation above all else. By leveraging strict ORM scoping, atomic transactions, collision-aware sequence generators, and soft-delete architectures, Relaso Billing Pro provides an enterprise-grade financial foundation that businesses can trust with their most critical operations.
Suggested Articles
The Hidden Costs of Manual Accounts Receivable (And How to Automate It)
Discover why manual A/R processes are draining your cash flow and how implementing automat...
Read Article →5 Ways to Modernize Your Enterprise Procurement Process
Is your purchasing department still running on paper forms and emails? Learn how to implem...
Read Article →Announcing Relaso Billing Pro v2.4.0: Healthcare Aggregator & Commission Engine
Version 2.4.0 is live! We are thrilled to announce a massive update tailored specifically ...
Read Article →New Feature Alert: Dynamic Pricing & Catalog API
Quick update: We have just released the new Dynamic Pricing Rules engine and an expanded R...
Read Article →Ready to implement these practices?
Start using Relaso Billing Pro to automate your compliance and billing workflows today.
Start 14-Day Free Trial