Designing for tenant isolation in multi-tenant systems
Trace one invoice
Imagine two tenants each have an invoice numbered 42. A lookup by invoice number alone is ambiguous. Resolve the authenticated user's membership, establish the active tenant, then carry that scope through every read and write.
Check secondary paths
Include tenant identity in cache keys, export jobs and object paths. A correctly scoped database query can still leak a report if two tenants share the same cached filename. Apply equivalent checks when generating download links.
Make isolation testable
Create two test tenants with matching invoice numbers and different totals. Exercise list, detail, update and export operations as each tenant. Assert that attempted cross-tenant changes leave both records intact.