Rate Limiting Foundations for Distributed Safety
This first day of phase-4 introduces safety primitives that prevent cascading failures across services. It grounds the learner in concrete mechanisms before exploring consensus and fault tolerance. The day matters because Maku's current API surface already contains rate-limiter logic that will later be hardened against distributed threats.
Resources
- 18 min
- 25 minreadingO'ReillyDesigning Data-Intensive Applications Chapter 4
pages 137-152 on load balancing and rate limiting
Codebase anchors
The Tribunal code that demonstrates today's concept. Click the line to open in GitHub or VS Code.
this is the existing rate-limiter implementation we will measure against and later extend for distributed safety guarantees
1/**2 * Rate Limiter with Firestore storage3 * Tracks API usage per IP/user and enforces limits4 */5 6import { getFirestore } from './firestore';7 8export interface RateLimitConfig {9 /** Maximum requests allowed in the window */10 maxRequests: number;11 /** Time window in seconds */12 windowSeconds: number;13 /** Identifier type */14 identifierType: 'ip' | 'user' | 'both';15}16 17export interface RateLimitResult {18 allowed: boolean;19 remaining: number;20 resetAt: Date;21 error?: string;this is the closest existing usage of rate limiting we will measure against on this day
1/**2 * POST /api/maku/brief3 *4 * Receives a 5-question brief from the /maku page and emails it to5 * the founder via Resend. Rate-limited via the existing rate-limiter6 * to stop bot floods.7 *8 * v1: email only. v2 candidate: also INSERT into a service_inquiries9 * table for backlog tracking.10 */11import { NextRequest, NextResponse } from 'next/server';12import { Resend } from 'resend';13import { checkRateLimit, getClientIP, rateLimitResponse } from '@/lib/rate-limiter';14 15export const dynamic = 'force-dynamic';16 17const resend = new Resend(process.env.RESEND_API_KEY || 'placeholder_key_for_build');18const TO = process.env.MAKU_INQUIRY_EMAIL || 'makpalyy@gmail.com';19 20interface BriefBody {21 what?: string;Deliverable
commit adding a 200-word journal entry in docs/journal/day-131.md that identifies one production risk exposed by the current rate-limiter.ts
Quiz · 2 questions
1. Which failure mode does a simple in-memory rate limiter fail to prevent in a horizontally scaled deployment?
2. State one concrete drawback of using a fixed-window counter versus a sliding-window log for API safety.