Menu

Protecting a NestJS API from a flaky AI service with a circuit breaker
📰

Protecting a NestJS API from a flaky AI service with a circuit breaker

DEV Community: nestjs·Ziad Taha·5 days ago
#kku1OMEZ
#devtonestjs#devto#dev#nestjs#service
Long article (290 words) — no AI summary yet.
Reading 0:00
15s threshold

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.

The problem

Face 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.

The approach

A small circuit breaker class with three states:

CLOSED: calls go through. Failures are counted.

OPEN: after too many failures, calls fail right away with a clear error. No waiting.

HALF_OPEN: after a backoff delay, one trial call is allowed. If it works, the breaker closes.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.