Status: Accepted (in force). Reconstructed from implementation.
Context
A payroll run reads from many tables (paygrades, allowances, statutory benefits, tax reliefs, payroll changes, exemptions, absences, exits, requests, reversals) and computes derived amounts for every employee. For large companies that's thousands of employees × dozens of joins per run.
Decision
Do the computation in T-SQL stored procedures and functions inside the tenant database. C# (PayrollService, AdminPayrollsService) only validates input, manages workflow, calls the SP, and reads results back. Procs (spPrepareRegularPayroll, spSubmitRegularPayroll, …) and functions (fnCalculateTaxAmount, fnCalculateProratedAmount, fnCalculateGrossFromNetAmount, …) hold the math.
Consequences
Good:
- Set-based operations over thousands of rows are far faster than row-by-row C#.
- The engine runs close to the data — no round-trips per employee.
- One transaction can commit a whole run atomically.
Bad / costs:
- The math is invisible to C# search — newcomers can't find "where tax is calculated" in the app code (see onboarding traps).
- Changing the engine means writing a migration that drops + recreates a proc — heavier than editing a method.
- SP logic is harder to unit-test than C#.
- Business logic is split across two languages/locations for any given feature.
Alternatives considered
- All-in-C# (EF/LINQ) computation — rejected on performance for large tenants; N+1 and in-memory joins don't scale to full-company runs.
- A dedicated calculation microservice — more moving parts, network cost per employee, and still needs the same data locality.
Verify
⚠️ VERIFY the original performance benchmark that motivated this (if recorded).