Architecture Overview¶
CalcBridge is a multi-tenant Excel calculation platform designed for enterprise financial services. This section provides comprehensive technical documentation of the system architecture.
Technology Stack¶
| Layer | Technology | Purpose |
|---|---|---|
| API | FastAPI (Python 3.11+) | Async REST API with OpenAPI docs |
| Database | PostgreSQL 16 | Primary storage with JSONB + RLS |
| Cache | Valkey (Redis-compatible) | Distributed caching and rate limiting |
| Task Queue | Celery | Async job processing |
| Frontend | Next.js 14 | React-based web application |
| Formula Engine | Custom AST Parser | Safe Excel formula evaluation |
High-Level Architecture¶
graph TB
subgraph Clients["Clients"]
WEB["Web App<br/>(Next.js 14)"]
API_CLIENT["API Clients"]
end
subgraph LoadBalancer["Load Balancer"]
LB["NGINX / ALB"]
end
subgraph APILayer["API Layer"]
FASTAPI["FastAPI<br/>(Async Python)"]
end
subgraph CoreServices["Core Services"]
CALC["Calculation Engine"]
PARSER["Formula Parser"]
AUTH["Auth Service"]
TENANT["Tenant Service"]
end
subgraph Workers["Worker Layer"]
CELERY["Celery Workers"]
end
subgraph DataLayer["Data Layer"]
PG["PostgreSQL 16<br/>(RLS Enabled)"]
VALKEY["Valkey<br/>(Cache + Rate Limits)"]
end
WEB --> LB
API_CLIENT --> LB
LB --> FASTAPI
FASTAPI --> CALC
FASTAPI --> AUTH
FASTAPI --> TENANT
CALC --> PARSER
FASTAPI --> CELERY
FASTAPI --> PG
FASTAPI --> VALKEY
CELERY --> PG
CELERY --> VALKEY Key Architectural Decisions¶
1. AST-Based Formula Parsing¶
Decision: Use Abstract Syntax Tree (AST) parsing instead of dynamic code execution.
Rationale: - Security: Prevents code injection attacks - no arbitrary code execution - Performance: Compile once, execute many times - Extensibility: Easy to add new functions - Debugging: Better error messages with source location
2. Row-Level Security for Multi-Tenancy¶
Decision: Implement tenant isolation at the database level using PostgreSQL RLS.
Rationale: - Defense in Depth: Even application bugs cannot leak tenant data - Performance: PostgreSQL optimizes RLS policies - Audit: Database-level isolation simplifies compliance
3. JSONB for Flexible Data Storage¶
Decision: Store Excel cell data and metadata in PostgreSQL JSONB columns.
Rationale: - Schema Flexibility: Handle varying Excel structures - Query Performance: JSONB indexes enable efficient queries - Type Preservation: Maintain original Excel data types
4. Vectorized Calculations¶
Decision: Process calculations using pandas/numpy vectorized operations.
Rationale: - Performance: 100x faster than row-by-row iteration - Memory Efficiency: Batch operations reduce overhead - Financial Precision: Decimal support for monetary calculations
Architecture Deep-Dives¶
Performance Characteristics¶
| Metric | Target | Typical |
|---|---|---|
| API P95 Latency | <200ms | ~120ms |
| Formula Evaluation | <50ms | ~25ms |
| Workbook Upload (5MB) | <5s | ~3s |
| Concurrent Users | 1000+ | - |
| Formulas per Second | 10,000+ | - |
Directory Structure¶
calcbridge/
├── src/
│ ├── api/ # FastAPI routes and middleware
│ ├── core/ # Auth, tenant context, config
│ ├── calculations/ # Formula parser and engine
│ │ ├── parser/ # Tokenizer, AST nodes, translator
│ │ ├── functions/ # Vectorized Excel functions
│ │ └── services/ # Calculation orchestration
│ ├── db/ # SQLAlchemy models and repos
│ ├── services/ # Business logic services
│ └── workers/ # Celery task definitions
├── db/migrations/ # Flyway SQL migrations
├── frontend/ # Next.js application
├── config/ # Prometheus, Grafana configs
└── tests/ # Comprehensive test suite
Integration Points¶
External Systems¶
| System | Integration Type | Purpose |
|---|---|---|
| Geneva | XML Upload | Portfolio management data |
| JPM | API + Files | Account balances, statements |
| Nomad | File Import | Portfolio tracking |
Internal Services¶
| Service | Protocol | Purpose |
|---|---|---|
| Prometheus | HTTP Pull | Metrics collection |
| Grafana | Dashboard | Visualization |
| Alertmanager | HTTP Push | Alert routing |
Next Steps¶
- System Design - Detailed component architecture
- Data Flow - Excel processing pipeline
- Formula Engine - Safe formula evaluation
- Multi-Tenancy - Tenant isolation patterns
- Security - Authentication and encryption