Appearance
Step 6: System Architecture & Data Flow
This document details the multi-tier distributed architecture of the Cooking Nexus platform, outlining request lifecycles, entity relationships, caching strategies, and 100x horizontal scaling patterns.
6.1 High-Level Architecture Topology
Cooking Nexus employs a hybrid micro-modular edge architecture:
- Edge Presentation & Ingestion Tier: Single-Page React 18 client served via CDN with client-side routing, Web Audio timers, and local session caching.
- Serverless Edge AI Tier: Deno-based Supabase Edge Functions proxying requests to Google's Gemini 3 Flash model via Server-Sent Events (SSE).
- Core Telemetry & Ingestion Tier: High-throughput Express 5 Node.js service ingesting kitchen optical safety frames and issuing cryptographic JWT Magic Links.
- Data Persistence Tier: MongoDB replica set storing user identity, member preference documents, and append-only safety incident logs.
6.2 Data Flow & Request Lifecycles
1. Kitchen Safety Hazard Lifecycle
- The overhead range hood camera evaluates video frames. Upon detecting smoke or open flame patterns, it dispatches an HTTP POST request to
/api/visionwith a JSON payload containingdeviceId,hazard, andtimestamp. - The Express server passes the request through
express.json()into [controllers/safetyController.js](file:///Users/avishjain/avishjain9-repos/26-cooking-nexus/controllers/safetyController.js). - The controller delegates to [services/visionService.js](file:///Users/avishjain/avishjain9-repos/26-cooking-nexus/services/visionService.js), which invokes
SafetyLog.create(). - Mongoose validates the document schema and executes an insert into the
safetiescollection in MongoDB. - An HTTP 200 JSON confirmation with the generated MongoDB
_idreturns to the edge device.
2. Edge AI Streaming Chat Lifecycle
- The user asks a cooking substitution question on
AiChatPage.tsx. streamChat()in [src/lib/ai-stream.ts](file:///Users/avishjain/avishjain9-repos/26-cooking-nexus/27-kitchen-companion-pro/src/lib/ai-stream.ts) opens a POST request with the message array andmode: "chat"to the edge function.- [supabase/functions/chef-ai/index.ts](file:///Users/avishjain/avishjain9-repos/26-cooking-nexus/27-kitchen-companion-pro/supabase/functions/chef-ai/index.ts) prepends the system prompt and forwards the payload to
https://ai.gateway.lovable.dev/v1/chat/completionsrequestinggoogle/gemini-3-flash-previewwithstream: true. - As Gemini generates tokens, the Lovable gateway streams Server-Sent Events chunks (
data: {...}). - The client
TextDecoderprocesses newline-delimited chunks, decodesdelta.content, and dispatches updates to the React component state, achieving instant token streaming.
6.3 Database Schema & Entity Relationships
Database Indexes & Rationale
User.email:{ email: 1 }, { unique: true }— Enforces unique account constraints and provides O(1) lookup speed during Magic Link verification.Member.email:{ email: 1 }, { unique: true }— Fast profile retrieval on login.SafetyLog.createdAt: Indexed via{ timestamps: true }— Supports time-series queries for recent hazard audits and allows future TTL auto-expiry.
6.4 Caching & Error Handling Architecture
Caching Layers
- Browser Cache: Static assets (Vite JS bundles, WebP food photography, fonts) are served with
Cache-Control: public, max-age=31536000, immutable. - Client Server-State Cache: TanStack React Query maintains an in-memory client cache (
staleTime: 5 minutes), eliminating duplicate network requests when switching between/app/recipesand/app/kitchen. - Planned Redis Layer: Frequently accessed member profiles and active recipe JSON objects will be cached in Redis with a 15-minute TTL.
Error Handling Across Layers
6.5 Scaling to 100x Traffic & First 3 Bottlenecks
Current vs 100x Workload Projections
- Current Load: ~10 req/sec; 100 daily active kitchens; ~50k AI tokens/day.
- 100x Load: ~1,000 req/sec; 10,000 concurrent cooking sessions; ~5M AI tokens/day; ~15 Gbps video reel streaming bandwidth.
Top 3 Bottlenecks & Remediation Plan
Bottleneck 1: Synchronous Vision Ingestion Writes to MongoDB
- Problem: At 1,000 frames/sec, opening direct Mongoose connections for every
/api/visionPOST will exhaust MongoDB connection pools and cause thread starvation in Express. - Remediation: Introduce a distributed message queue (RabbitMQ or Apache Kafka) or Redis Streams in front of the database. The
/api/visionendpoint immediately pushes to the queue and respondsHTTP 202 Acceptedin <5ms, while background worker processes batch-insert logs into MongoDB.
Bottleneck 2: External AI Gateway Concurrency & Rate Limits
- Problem: Lovable/Gemini rate limits (HTTP 429) will throttle concurrent users during peak dinner prep hours (6 PM - 8 PM).
- Remediation: Implement a semantic cache in Redis using Vector Embeddings. If a user asks "How do I substitute buttermilk in pancakes?", and that query has already been answered with high cosine similarity, serve the cached response instantly without calling the external LLM.
Bottleneck 3: Video Reel Bandwidth & Storage Egress
- Problem: Serving high-definition culinary reels directly from a single web origin will saturate network interfaces and incur catastrophic egress costs.
- Remediation: Store source MP4 assets in Cloudflare R2 / AWS S3 and distribute video manifests via AWS CloudFront CDN with HLS adaptive bitrate streaming (HLS/DASH), reducing origin egress to near zero.