{"slug": "protecting-a-nestjs-api-from-a-flaky-ai-service-with-a-circuit-breaker", "title": "Protecting a NestJS API from a flaky AI service with a circuit breaker", "summary": "A developer built FaceMark, a student attendance system whose NestJS API calls a separate FastAPI service running DeepFace/TensorFlow for face recognition, and wrapped that AI call in a three-state circuit breaker so login, courses and other endpoints keep working while the recognition service is slow or down. The breaker prevents requests from piling up behind a stuck AI service and returns a clear error message instead of cascading failures across the API. The code is public on GitHub under NestJS 10 with JWT auth, Swagger docs, rate limiting and automated tests.", "body_md": "When your NestJS API depends on an AI service that can be slow or down, every request that waits on it makes the whole API feel broken. In FaceMark, a student attendance system, the NestJS API calls a separate FastAPI service that runs DeepFace/TensorFlow for face recognition. That service is the weakest link, so I put a circuit breaker between the two.\n\nFace recognition takes time and sometimes fails. Without protection, a stuck AI service ties up requests in the NestJS API until timeouts pile up, and users see errors on endpoints that have nothing to do with faces.\n\nA small circuit breaker class with three states:\n\nThe NestJS side wraps the AI call in this breaker, so login, courses and the rest of the API keep working while the AI service recovers.\n\nIf your product depends on an AI model or a third-party API, decide up front what the user sees when it is down. A breaker plus a clear error message is a small amount of code and saves you from cascading failures.\n\nThe code is public: [https://github.com/ZiadtahaM/FaceMark](https://github.com/ZiadtahaM/FaceMark) (NestJS 10, JWT auth, Swagger docs, rate limiting, automated tests).\n\nI am a junior-plus full-stack developer (about 2 years Angular, 1 year NestJS) in Alexandria, Egypt, available for NestJS/TypeScript API work at $20-25/hour or a fixed price for a small scope. Message me on LinkedIn: [https://www.linkedin.com/in/ziad-taha-dev](https://www.linkedin.com/in/ziad-taha-dev)\n\n*Drafted with AI assistance, reviewed against the repository.*", "url": "https://wpnews.pro/news/protecting-a-nestjs-api-from-a-flaky-ai-service-with-a-circuit-breaker", "canonical_source": "https://dev.to/ziadtaha/protecting-a-nestjs-api-from-a-flaky-ai-service-with-a-circuit-breaker-1bhc", "published_at": "2026-11-25 19:39:14+00:00", "updated_at": "2026-11-25 19:48:14.720636+00:00", "lang": "en", "topics": ["artificial-intelligence", "ai-infrastructure", "computer-vision", "developer-tools"], "entities": ["FaceMark", "NestJS", "FastAPI", "DeepFace", "TensorFlow", "GitHub", "Ziad Taha"], "also_reported_by": [], "takeaway": "Wrapping a slow or failure-prone AI service call in a three-state circuit breaker keeps unrelated API endpoints responsive and replaces cascading timeouts with a clear error message.", "recommendation": {"advice": "Put a circuit breaker around any call to an AI or third-party service that can hang, and decide up front what users see when it is open.", "playbook": "production-ops", "playbook_url": "https://wpnews.pro/playbooks/production-ops"}, "alternates": {"html": "https://wpnews.pro/news/protecting-a-nestjs-api-from-a-flaky-ai-service-with-a-circuit-breaker", "markdown": "https://wpnews.pro/news/protecting-a-nestjs-api-from-a-flaky-ai-service-with-a-circuit-breaker.md", "text": "https://wpnews.pro/news/protecting-a-nestjs-api-from-a-flaky-ai-service-with-a-circuit-breaker.txt", "jsonld": "https://wpnews.pro/news/protecting-a-nestjs-api-from-a-flaky-ai-service-with-a-circuit-breaker.jsonld"}}