Skip to content

Graceful Auth Degradation — Handling Auth Service Outages

DodaTech Updated 2026-06-28 1 min read

In this tutorial, you'll learn about Graceful Auth Degradation. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

Graceful degradation ensures your application remains operational during authentication service outages.

// Circuit breaker for auth service
const authCircuitBreaker = new CircuitBreaker({
  timeout: 3000,
  errorThresholdPercentage: 50,
  resetTimeout: 30000,
  name: 'auth-service'
});

authCircuitBreaker.fallback(async (req) => {
  // Fallback to cached token validation
  const token = extractToken(req);
  if (!token) throw new Error('No token');

  const hash = crypto.createHash('sha256').update(token).digest('hex');
  const cached = await redis.get(`auth:validated:${hash}`);

  if (cached) {
    const decoded = JSON.parse(cached);
    // Allow but log degradation
    logger.warn('Auth degradation: using cached validation', {
      userId: decoded.sub,
      cacheAge: Date.now() - decoded.cachedAt
    });
    return decoded;
  }

  // Try local JWT validation (stateless)
  try {
    const decoded = jwt.verify(token, publicKey, {
      algorithms: ['RS256'],
      issuer: 'https://auth.example.com'
    });
    return decoded;
  } catch {
    throw new Error('Auth degraded and unable to validate');
  }
});

// Degraded mode middleware
app.use('/api', async (req, res, next) => {
  try {
    const result = await authCircuitBreaker.fire(req);
    req.user = result;
    next();
  } catch (err) {
    // Determine if degraded mode is acceptable for this route
    const isReadOnly = req.method === 'GET';
    const isPublicRoute = publicRoutes.includes(req.path);

    if (isPublicRoute) return next();

    if (isReadOnly && process.env.AUTH_DEGRADED_MODE === 'read_only') {
      logger.warn('Allowing read-only access during auth degradation', { path: req.path });
      req.user = { sub: 'degraded', role: 'reader' };
      res.setHeader('X-Auth-Mode', 'degraded');
      return next();
    }

    return res.status(503).json({
      error: 'auth_unavailable',
      message: 'Authentication service temporarily unavailable'
    });
  }
});

Graceful degradation prevents complete application unavailability when auth infrastructure experiences issues.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro