SaaS Architecture & Optimization

TL;DR
Role: Full Stack Developer (Lead Backend)
Timeline: 10 Months (Team of 2)
Tech Stack: React, Bootstrap, Express.js, MongoDB
The Goal: Scale a swimming franchise management platform by resolving severe database bottlenecks and implementing strict business logic for coach-to-student assignments.
1. The Problem: The "Data Bottleneck"
As the franchise's user base grew to handle hundreds of active students, the application's initial MVP architecture began to fail. The main administrative dashboard experienced severe latency (loading times of 3-5 seconds), crippling the staff's ability to check in students quickly at pool locations with poor internet connectivity.
I isolated the root causes to three core issues:
- Client-Side Filtering: The Express API sent all database records to the React frontend to be filtered, bloating the JSON payload and freezing the browser UI.
- Database Scans: MongoDB was executing full collection scans for filtered queries, causing massive CPU spikes on the database cluster.
- Inefficient Query Plans: Complex sorting requirements prevented the system from utilizing standard database indexes.
2. The Solution & Core Features
To resolve the bottleneck and support franchise growth, I refactored the data-fetching architecture and built out scalable core features.
A. Dynamic Backend Query Builder
I migrated all search and filter logic out of React and into the Express backend. I built a dynamic query builder service that intercepts frontend requests and constructs optimized MongoDB aggregation pipelines on the fly based on active filters (Status, Membership Type, Coach).
B. Cursor-Based Pagination
I replaced the payload-heavy API requests with a robust cursor-based pagination system, enabling smooth infinite scrolling on the frontend and drastically reducing server bandwidth.
C. Strict Coach Assignment Engine
Beyond performance, I engineered the business logic for the platform's Coach-to-Attendee assignment system. The backend dynamically calculates real-time lane capacity. If a transfer violates a membership limit, the API rejects the payload, preventing overbooking.

3. The Engineering Challenges
The primary challenge was completely restructuring how the application queried complex relational data without breaking existing frontend features or causing downtime.
To eliminate the CPU-heavy full collection scans (COLLSCAN), I analyzed the application's query patterns and implemented tailored Compound Indexes in MongoDB. This covered the most frequent admin filter combinations (e.g., filtering by active students under a specific location, sorted by sorted by attendance count).
By shifting the architectural approach from an O(N) linear database scan to an O(logN) indexed lookup, I ensured the query speed remains lightning-fast and constant, regardless of whether the database holds 8,000 or 100,000+ records.
4. The Results & Impact
The backend refactor achieved an "instant search" feel for end-users, completely resolving the poolside check-in delays for the franchise staff.
| Metric | Before Optimization | After Optimization | Impact |
|---|---|---|---|
| Average Latency | 1.23 seconds | 0.12 seconds | 10x Faster |
| Documents Scanned | ~8,000 docs | ~300 docs | 96% Reduction |
| MongoDB Execution | COLLSCAN | IXSCAN | Fully Indexed |
- Performance: API response time reduced by ~90% for listed endpoints.
- Efficiency: Drastically reduced server bandwidth usage by serving tightly paginated data.
- Scalability: Future-proofed the database queries to remain consistently fast as the franchise continues to open new locations.