A running journal of architectural decisions, technical hurdles, and engineering breakthroughs encountered during the development of Quantify.
- The Challenge: As a financial platform, a single trade endpoint had to validate requests, fetch market data, calculate portfolio balances, check for margin requirements, and execute the transaction. This quickly made standard routing files bloated and difficult to debug.
- The Solution: Refactored the backend into a strict, production-ready layered architecture. Decoupled the codebase into a Controller Layer (gatekeeper for HTTP requests), a Service Layer (isolated core quantitative business logic), and a Repository Layer (the exclusive gateway to MongoDB/Mongoose). This keeps the codebase modular and ready if the database layer ever shifts to a relational database like PostgreSQL.
- The Challenge: Initially, the platform managed two separate schemas—one for Transactions (cash) and one for Portfolios (assets). This led to "Data Desync," where numbers between the two files occasionally failed to match.
- The Solution: Implemented Schema Consolidation by merging the related data into a single, unified
UserTradingDataschema. This minimized database calls, eliminated sync issues, and made the mathematical calculations highly reliable.
- The Challenge: Ran into an infrastructure "ghost" where the server caught itself in an infinite loop and crashed. The culprit was a circular dependency in the logging setup: Winston was configured to send production logs via HTTP to the server's own port, triggering an infinite request-log-request cycle.
- The Solution: Deep-dived into middleware and network transport logic to completely isolate the logging infrastructure. Separated the flows to ensure clean, uncolorized file logging for production and vivid console logs strictly for development.
- The Challenge: Encountered the common
ERR_HTTP_HEADERS_SENTnode error when backend execution paths allowed the server to inadvertently attempt to send multiple HTTP responses back to the client for a single request. - The Solution: Enforced strict control flow by systematically utilizing the
returnkeyword whenever a response (such as an early error status) is invoked, instantly halting downstream execution.
- The Challenge: Experienced a critical margin calculation bug where a user's balance would abruptly jump from 5,000 to over 11 lakhs after a transaction. This happened because JavaScript was implicitly treating numbers like strings during shorthand assignment operators.
- The Solution: Added robust input sanitization by wrapping all incoming numeric inputs explicitly in
Number(). Reconstructed the logic to dynamically calculate total purchase costs and subtract them directly from the current balance rather than relying on original state resets.