notion-reliability-patterns
Graceful degradation when Notion is down: offline cache, retry with exponential backoff, circuit breaker, health checks, and fallback content. Use when building fault-tolerant Notion integrations for production. Trigger with phrases like "notion reliability", "notion circuit breaker", "notion offline fallback", "notion health check", "notion graceful degradation".
What this skill does
# Notion Reliability Patterns
## Overview
Production-grade reliability patterns for Notion integrations. Covers graceful degradation with offline cache when Notion is unavailable, retry with exponential backoff for transient failures, circuit breaker to prevent cascade failures, health check endpoints for monitoring, and fallback content serving when the API is unreachable. All patterns use `Client` from `@notionhq/client` and handle Notion-specific error codes.
## Prerequisites
- `@notionhq/client` v2.x installed (`npm install @notionhq/client`)
- `lru-cache` for in-memory caching (`npm install lru-cache`)
- Python: `notion-client` installed (`pip install notion-client`)
- `NOTION_TOKEN` environment variable set
- Understanding of circuit breaker and retry patterns
## Instructions
### Step 1: Retry with Exponential Backoff
The Notion SDK has built-in retries, but you can customize the behavior for better control over transient errors (429, 500, 502, 503).
```typescript
import { Client, isNotionClientError, APIErrorCode } from '@notionhq/client';
// Classify errors as transient (retryable) vs permanent
function isTransientError(error: unknown): boolean {
if (isNotionClientError(error)) {
return (
error.code === APIErrorCode.RateLimited ||
error.code === APIErrorCode.InternalServerError ||
error.code === APIErrorCode.ServiceUnavailable ||
error.code === 'notionhq_client_request_timeout'
);
}
// Network errors are transient
if (error instanceof Error && error.message.includes('fetch failed')) {
return true;
}
return false;
}
async function retryWithBackoff<T>(
fn: () => Promise<T>,
opts: { maxRetries?: number; baseDelayMs?: number; label?: string } = {}
): Promise<T> {
const { maxRetries = 4, baseDelayMs = 1000, label = 'notion-call' } = opts;
for (let attempt = 0; attempt <= maxRetries; attempt++) {
try {
return await fn();
} catch (error) {
if (!isTransientError(error) || attempt === maxRetries) {
throw error;
}
// Exponential backoff: 1s, 2s, 4s, 8s (with jitter)
const delay = baseDelayMs * Math.pow(2, attempt);
const jitter = delay * 0.2 * Math.random();
const waitMs = delay + jitter;
// Special handling for rate limits: use Retry-After header
if (isNotionClientError(error) && error.code === APIErrorCode.RateLimited) {
const retryAfter = parseInt((error as any).headers?.['retry-after'] ?? '1');
const rateLimitWait = retryAfter * 1000;
console.warn(`[${label}] Rate limited, waiting ${retryAfter}s (attempt ${attempt + 1}/${maxRetries})`);
await new Promise(r => setTimeout(r, rateLimitWait));
} else {
console.warn(`[${label}] Transient error, retrying in ${Math.round(waitMs)}ms (attempt ${attempt + 1}/${maxRetries})`);
await new Promise(r => setTimeout(r, waitMs));
}
}
}
throw new Error('Unreachable');
}
const notion = new Client({ auth: process.env.NOTION_TOKEN });
// Usage: any Notion call with automatic retry
const page = await retryWithBackoff(
() => notion.pages.retrieve({ page_id: 'abc123' }),
{ label: 'get-page', maxRetries: 3 }
);
```
```python
from notion_client import Client, APIResponseError
import time
import random
notion = Client(auth=os.environ["NOTION_TOKEN"])
def is_transient(error):
if isinstance(error, APIResponseError):
return error.status in (429, 500, 502, 503)
return False
def retry_with_backoff(fn, max_retries=4, base_delay=1.0, label="notion"):
for attempt in range(max_retries + 1):
try:
return fn()
except Exception as e:
if not is_transient(e) or attempt == max_retries:
raise
delay = base_delay * (2 ** attempt) + random.uniform(0, base_delay * 0.2)
print(f"[{label}] Retry {attempt + 1}/{max_retries} in {delay:.1f}s")
time.sleep(delay)
```
### Step 2: Circuit Breaker to Prevent Cascade Failures
When Notion has sustained outages, stop hammering the API and fail fast.
```typescript
type CircuitState = 'closed' | 'open' | 'half-open';
class NotionCircuitBreaker {
private state: CircuitState = 'closed';
private failureCount = 0;
private lastFailureTime = 0;
private successCount = 0;
constructor(
private readonly failureThreshold = 5, // Open after 5 consecutive failures
private readonly resetTimeoutMs = 30_000, // Try again after 30 seconds
private readonly halfOpenSuccesses = 2 // Need 2 successes to close
) {}
async execute<T>(fn: () => Promise<T>): Promise<T> {
if (this.state === 'open') {
if (Date.now() - this.lastFailureTime > this.resetTimeoutMs) {
this.state = 'half-open';
this.successCount = 0;
console.log('[circuit] Half-open: testing Notion API');
} else {
throw new CircuitOpenError(
`Circuit open: Notion API disabled for ${Math.round((this.resetTimeoutMs - (Date.now() - this.lastFailureTime)) / 1000)}s`
);
}
}
try {
const result = await fn();
this.onSuccess();
return result;
} catch (error) {
this.onFailure(error);
throw error;
}
}
private onSuccess() {
this.failureCount = 0;
if (this.state === 'half-open') {
this.successCount++;
if (this.successCount >= this.halfOpenSuccesses) {
this.state = 'closed';
console.log('[circuit] Closed: Notion API restored');
}
}
}
private onFailure(error: unknown) {
// Only trip on transient errors, not client errors (400/401/404)
if (!isTransientError(error)) return;
this.failureCount++;
this.lastFailureTime = Date.now();
if (this.state === 'half-open' || this.failureCount >= this.failureThreshold) {
this.state = 'open';
console.warn(`[circuit] OPEN after ${this.failureCount} failures — API calls disabled`);
}
}
getState(): { state: CircuitState; failures: number; lastFailure: Date | null } {
return {
state: this.state,
failures: this.failureCount,
lastFailure: this.lastFailureTime ? new Date(this.lastFailureTime) : null,
};
}
}
class CircuitOpenError extends Error {
constructor(message: string) {
super(message);
this.name = 'CircuitOpenError';
}
}
const circuit = new NotionCircuitBreaker();
// All Notion calls go through the circuit breaker
const query = await circuit.execute(() =>
notion.databases.query({ database_id: dbId, page_size: 100 })
);
```
### Step 3: Graceful Degradation with Offline Cache, Health Checks, and Fallback Content
When Notion is down, serve cached data instead of errors. Include health check endpoints for monitoring.
```typescript
import { LRUCache } from 'lru-cache';
// Offline cache with long TTL — stale data beats no data
const offlineCache = new LRUCache<string, { data: any; timestamp: number }>({
max: 1000,
ttl: 3600_000, // 1 hour — keep serving even if API is down
});
interface QueryResult<T> {
data: T;
source: 'live' | 'cache';
cacheAge?: number; // seconds since last live fetch
}
async function queryWithFallback<T>(
cacheKey: string,
fn: () => Promise<T>
): Promise<QueryResult<T>> {
try {
const data = await circuit.execute(() => retryWithBackoff(fn));
// Update cache on success
offlineCache.set(cacheKey, { data, timestamp: Date.now() });
return { data, source: 'live' };
} catch (error) {
// Circuit is open or all retries exhausted — try cache
const cached = offlineCache.get(cacheKey);
if (cached) {
const ageSeconds = Math.round((Date.now() - cached.timestamp) / 1000);
console.warn(`[fallback] Serving cached data (${ageSeconds}s old) for ${cacheKey}`);
return { data: cached.data as T, source: 'cache', cacheAge: ageSeconds };
}
// No cache — provide fallback content
throw error;
}
}
// Usage: query database with automatic fallback
const { data: pagRelated in Productivity
gitea-workflow
IncludedOrchestrate agile development workflows for Gitea repositories using the tea CLI. Use when working with Gitea-hosted repos and asking to 'run the workflow', 'continue working', 'what's next', 'complete the task cycle', 'start my day', 'end the sprint', 'implement the next task', or wanting guided step-by-step development assistance. Keywords: workflow, orchestrate, agile, task cycle, sprint, daily, implement, review, PR, standup, retrospective, gitea, tea.
microsoft-graph-gateway
IncludedRoute Microsoft Graph work in this workspace. Use when users want to read or write Outlook mail, calendar events, contacts, OneDrive or SharePoint files, Teams, Planner, To Do, users, groups, directory data, or arbitrary Microsoft Graph endpoints from VS Code. Prefer WorkIQ for common read scenarios. Use Microsoft Graph for write actions and gap-read scenarios that need exact Graph properties, filters, permissions, or endpoints.
copilotkit
IncludedUse when building with CopilotKit — setup, development, integrations, debugging, upgrading, or contributing. Routes to the appropriate specialized skill based on the task.
wordly-wisdom
IncludedProvides calibrated decision analysis using Charlie Munger-style multiple mental models, inversion, incentive mapping, circle-of-competence checks, misjudgment audits, second-order effects, and forecast updates. Use when the user asks for an oracle take, a hard call, a decision memo, a premortem, an outside view, a red-team, a sanity-check, what am I missing, think this through, or wants a strategy, hire, investment, plan, product, partnership, or major life choice analysed. Avoid for simple factual lookups or time-sensitive legal, medical, or market questions without fresh evidence.
swain-session
IncludedSession management and project status dashboard. Owns the full session lifecycle (start/work/close/resume), focus lane, bookmarks, worktree detection, and tab naming. Also serves as the project status dashboard — shows active epics, progress, actionable next steps, blocked items, tasks, GitHub issues, and recommendations. Worktree creation is deferred to swain-do task dispatch (SPEC-195). Triggers on: 'session', 'status', 'what's next', 'dashboard', 'overview', 'where are we', 'what should I work on', 'show me priorities', 'bookmark', 'focus on', 'session info'.
gandi
IncludedComprehensive Gandi domain registrar integration for domain and DNS management. Register and manage domains, create/update/delete DNS records (A, AAAA, CNAME, MX, TXT, SRV, and more), configure email forwarding and aliases, check SSL certificate status, create DNS snapshots for safe rollback, bulk update zone files, and monitor domain expiration. Supports multi-domain management, zone file import/export, and automated DNS backups. Includes both read-only and destructive operations with safety controls.