A Next-Generation Corporate Expense Engine — Designed to process high-concurrency corporate transactions with real-time budget enforcement, AI fraud detection, and 100% ledger integrity.
LedgerX is a B2B SaaS backend for managing corporate spend at scale. Think of it as the financial infrastructure behind platforms like Brex, Ramp, or internal treasury systems at companies like Uber and Airbnb.
- Multi-Tenancy: Organizations & Departments with hierarchical budget controls
- Real-Time Budget Enforcement: Block transactions that exceed department limits before funds leave
- Distributed Architecture: Handle thousands of concurrent employee transactions without database bottlenecks
- Audit-Ready Ledger: Immutable double-entry bookkeeping for compliance (SOX, GAAP)
- AI Risk Orchestration: Real-time anomaly detection using statistical models + rule-based fraud patterns
This is not a CRUD app. It is an event-driven financial processing engine designed to solve the "Double-Spend" problem and handle high-velocity traffic spikes.
- Organizations have Departments with monthly spend limits
- Before processing a transaction, the worker checks:
currentMonthSpend + newAmount ≤ budgetLimit - Pessimistic locking ensures no race conditions (two employees can't overspend simultaneously)
- Implemented an immutable append-only ledger (Credits/Debits) ensuring zero-sum consistency
- Every transaction creates exactly 2 entries:
-$Xfrom source,+$Xto destination - Prevents floating-point errors and ensures audit trails
- Solved the "Double-Spend" problem using database row-level locking (
SELECT ... FOR UPDATE) - Transactions are wrapped in Serializable isolation levels
- Ordered locking to prevent deadlocks
- Decoupled transaction ingestion from processing using BullMQ (Redis)
- API accepts transaction → queues it → returns instantly
- Background workers process asynchronously, handling traffic spikes without DB lock contention
- Custom middleware using Redis to cache request keys
- Network retries don't duplicate financial transactions
- Critical for preventing double-charges from client-side retries
- Statistical Anomaly Detection: Z-Score analysis flags outliers (transactions > 3σ from mean)
- Rule-Based Velocity Checks: Redis sliding windows detect rapid spending patterns
- Event-Driven: Fraud listeners run asynchronously without blocking payments
Backend:
- Node.js, TypeScript, Express
- PostgreSQL (with row-level locking, PL/pgSQL functions)
- BullMQ (Redis-backed job queue)
- Prisma ORM
- Google Gemini AI (NLP query parsing)
Frontend:
- Next.js 14 (App Router)
- TailwindCSS + shadcn/ui
- Recharts (Financial dashboards)
- GSAP + Framer Motion (Animations)
Infrastructure:
- Docker, Redis, Nginx
- CI/CD Ready
Tested with k6 to handle 500+ concurrent requests/sec with zero data inconsistencies.
- Queue Processing: Asynchronous workers drain spikes in
<50msper job - Lock Contention: Optimized via ordered row locking to prevent deadlocks
- Budget Checks: Sub-10ms aggregation queries (indexed on
departmentId+timestamp)
docker-compose up -dcd backend
npx prisma migrate deploy
npx tsx prisma/seed.ts # Creates demo org "TechCorp" with departments# Backend (API + Worker)
cd backend && npm run dev
# Frontend
cd frontend && npm run dev- Go to
http://localhost:3000 - Login as
alice@techcorp.com(Engineering dept, $50k budget) - Try to spend $60k → See budget rejection in real-time ✅
Scenario: TechCorp has 2 departments:
- Engineering: $50,000/month budget
- Sales: $25,000/month budget
Test Flow:
- Alice (Engineering) swipes corporate card for $6,000 → ✅ Success
- Bob (Engineering) tries $48,000 → ❌ Rejected
"Department 'Engineering' budget exceeded. Limit: $50,000, Current: $6,000, Requested: $48,000" - Finance Manager views org-wide dashboard → See real-time spend across all departments
| File | Purpose |
|---|---|
backend/src/workers/transaction.worker.ts |
Core budget enforcement + locking logic |
backend/src/ai/model.ts |
Statistical anomaly detector (Z-Score) |
backend/src/ai/langchainParser.ts |
NLP query parser with Redis caching |
backend/src/queues/transaction.queue.ts |
BullMQ job queue setup |
backend/src/middleware/checkIdempotency.ts |
Redis-based idempotency |
frontend/components/system/DepartmentBudgetCard.tsx |
Real-time budget status UI |
frontend/app/admin/system/page.tsx |
System internals dashboard |
- ✅ Multi-Organization Support (Tenant isolation)
- ✅ Department Budgets with real-time enforcement
- ✅ Role-Based Access Control (Employee, Finance Manager, Org Admin, System Admin)
- ✅ Transaction Reversals (Compensating entries for audit compliance)
- ✅ AI Query Interface ("Show me top expenses this month")
- ✅ Real-Time Infrastructure Monitoring (Queue latency, DB health, transaction throughput)
# Unit Tests
npm test
# Load Testing (Backend)
k6 run tests/load/transaction-spike.js
# AI Features
npx tsx src/ai/test-ai-features.ts- Add SSO (SAML) for enterprise auth
- Implement approval workflows (multi-level)
- Add webhook support for external integrations
- Build mobile app (React Native)
- Add vendor payment automation
MIT