{"slug": "how-babysea-uses-cloudflare-to-secure-and-control-execution-at-scale", "title": "How BabySea uses Cloudflare to secure and control execution at scale", "summary": "BabySea, an execution infrastructure provider for generative media, detailed how it uses Cloudflare as a pre-execution control plane across its US, EU, and Japan API regions. The company layers protocol validation, WAF rules, multi-tier rate limiting, managed threat rules, OpenAPI-based API Shield schema validation, and session-level identity tracking at the edge so invalid or abusive requests are rejected before reaching its control plane and inference execution layer. BabySea says the setup prevents malformed traffic from consuming execution resources and stops untrusted clients from dominating capacity.", "body_md": "[BabySea](https://babysea.ai) is execution infrastructure for generative media. That means we don't just run requests. We control how they execute across inference providers, with routing, failover, and lifecycle management.\n\nAs workloads scale, the problem is not just routing requests across providers. It's ensuring invalid, abusive, or malformed requests never reach execution, where every mistake has a real cost.\n\nWe use [Cloudflare](https://www.cloudflare.com) as the edge control layer to enforce correctness, security, and observability before requests reach our system.\n\nThis post breaks down how that works in production.\n\nBabySea runs across three regions:\n\n`api.us.babysea.ai`` api.eu.babysea.ai``api.jp.babysea.ai`\nEach region exposes the same API surface, backed by different infrastructure.\n\nCloudflare sits in front of all entry points and acts as a:\n\npre-execution control plane\n\nBefore a request reaches our application, it must pass:\n\nThese controls operate as independent layers, each enforcing a different constraint before execution begins.\n\nWhat we are preventing is not theoretical:\n\nEach layer exists to eliminate a specific failure mode before it reaches the control plane.\n\nBefore applying any higher-level rules, we enforce basic protocol correctness:\n\nThis ensures:\n\nonly structurally valid requests are processed further\n\nWithout this layer, invalid requests would propagate deeper into the system and fail after resources have already been allocated.\n\nWe don't treat WAF as a security add-on. We treat it as:\n\nexecution constraint enforcement\n\nWe explicitly reject malformed or suspicious patterns:\n\nonly valid execution paths reach the system\n\nWithout this layer, malformed requests would propagate into execution, where failures become expensive instead of cheap.\n\n**Abuse and scanning protection**\n\nWe aggressively block automated scanning:\n\nThis removes:\n\nbackground noise before it becomes load\n\n**Edge hardening**\n\nWe added rules for:\n\nEven if upstream systems can handle these, we block them at the edge:\n\nfail fast, before origin cost is incurred\n\nRate limiting is not just protection. It shapes system behavior.\n\nWe apply multiple layers:\n\n**Global API limit**\n\n`x` requests per minute per IP (pre-auth)\n**Auth protection**\n\n**Endpoint-specific limits**\n\nThese rules ensure:\n\nuntrusted clients cannot dominate execution capacity\n\nWithout rate control at the edge, invalid or abusive traffic would compete directly with legitimate execution workloads.\n\nWe leverage Cloudflare managed protections:\n\nThis layer handles:\n\nknown attack patterns and global threat signals\n\nIt allows us to absorb common threats without pushing that complexity into the application layer.\n\nThe most important layer is API Shield.\n\nWe define our API surface using OpenAPI:\n\nCloudflare validates incoming requests against this schema.\n\n**What this gives us**\n\nWithout schema validation at the edge, invalid requests would only fail inside the application layer, after resources have already been consumed.\n\nTraditional rate limiting is IP-based. That breaks in real systems:\n\nWe track sessions using:\n\n`Authorization` header as identity\n\nThis enables:\n\nResult:\n\nAll enforced at the edge.\n\nIP-based control alone cannot reliably identify abuse in distributed systems.\n\nSession-level tracking allows us to enforce behavior constraints per identity, not per network.\n\n```\nClient Request\n  ↓\nCloudflare Edge\n  ├─ Request Validation (Protocol Enforcement)\n  ├─ WAF (Execution Guardrail)\n  ├─ Rate Limiting (Pre-Execution Control)\n  ├─ Managed Rules (Threat Protection)\n  ├─ API Shield (Contract Enforcement)\n  ├─ Session Intelligence (Beyond IP)\n  ↓\nBabySea Control Plane\n  ├─ Access Control\n  ├─ Credit Lifecycle\n  ├─ Protocol Translation\n  ├─ Policy Routing\n  ├─ Failover Orchestration\n  ├─ Failure Handling\n  ├─ Artifact Pipeline\n  ├─ Event System\n  ↓\nExecution Layer\n```\n\nThe control plane defines how execution should happen. The execution layer carries it out.\n\nEach layer removes a different class of risk before it propagates.\n\nno single failure allows invalid or abusive execution to reach the system\n\nOnce a request passes edge validation, it enters a lifecycle that must remain consistent across providers, retries, and failures.\n\nWe treat execution as a controlled state machine:\n\nThis prevents critical failure classes:\n\nExecution is not just calling a provider.\n\nit is maintaining correctness across asynchronous, multi-provider systems\n\nA key part of that lifecycle is ordering. We create the generation record before reserving credits, so billing and state transitions are attached to a single durable generation ID from the start.\n\n```\nconst { data: record } = await adminClient\n  .from('file_assets')\n  .insert({\n    account_id: accountId,\n    generation_data: {\n      generation_provider_order: toProviderArray(providerOrder),\n      generation_status: 'pending',\n    },\n  })\n  .select('id, account_id, generation_id')\n  .single();\n\nconst { reserved } = await reserveCredits(\n  accountId,\n  model,\n  record.generation_id,\n  undefined,\n  generationResolution,\n);\n```\n\nThat ordering matters. It avoids reserve-then-cleanup ambiguity and makes every economic event traceable to the same lifecycle record.\n\nIn a multi-provider execution system, billing correctness cannot depend on good luck or a single success path.\n\nWe enforce a simple invariant:\n\none generation can reserve once, charge once, and refund once\n\nThat rule is backed by the database, not just application logic.\n\n```\nCREATE UNIQUE INDEX IF NOT EXISTS idx_credit_ledger_charge_idempotent\n  ON public.credit_ledger (generation_id) WHERE type = 'charge';\n\nCREATE UNIQUE INDEX IF NOT EXISTS idx_credit_ledger_refund_idempotent\n  ON public.credit_ledger (generation_id) WHERE type = 'refund';\n```\n\nCredits are also reserved atomically. That prevents concurrent requests from both passing a balance check and spending the same balance twice.\n\n```\nUPDATE public.credits\nSET tokens = tokens - p_tokens\nWHERE account_id = p_account_id\n  AND tokens >= p_tokens;\n```\n\nThis is what turns billing into a system property instead of a best effort.\n\nFailover is usually presented as a simple fallback story. In practice, it is a consistency problem.\n\nA provider can fail late. A webhook can arrive after the next provider has already been considered. Storage may already contain a completed output even when the local request path thinks it failed.\n\nWe account for that explicitly.\n\nFirst, provider order is reordered by health so degraded providers are moved back without changing the public execution contract.\n\n```\nconst { reordered: sequence } = await reorderByHealth(baseSequence);\nproviderOrder = sequence.map((s) => s.provider).join(', ');\n```\n\nThen, after a failed attempt, we check storage and database state before spending money on the next provider.\n\n```\nconst { data: storageFiles } = await adminClient.storage\n  .from('file')\n  .list(`${record.account_id}/${record.generation_id}`);\n\nif (storageFiles && storageFiles.length > 0) {\n  result = {\n    provider: step.provider,\n    predictionId: 'webhook-delivered',\n    status: 'succeeded',\n    providerModelId: step.providerModelId,\n  };\n  break;\n}\n```\n\nThis is the difference between \"trying another provider\" and controlling a distributed execution lifecycle.\n\nA multi-provider system degrades quickly if provider-specific fields leak into the public API.\n\nWe keep the public contract unified and intentionally normalize provider differences into BabySea's own schema model.\n\nThat includes rules like:\n\nratio-based sizing instead of exposing raw width and height\n\nintersection of supported formats across active providers\n\npricing dimensions modeled as core execution fields, not provider-specific knobs\n\nA simplified example:\n\n```\n// width: z.number().int().min(256).max(1440).optional(),\n// excluded - uses ratio-based sizing, not pixel dimensions\n```\n\nAnd at the schema level, core execution fields are ordered and enforced consistently across models:\n\n```\ngeneration_prompt\ngeneration_ratio\ngeneration_output_format\ngeneration_output_number\ngeneration_input_file\ngeneration_duration\ngeneration_resolution\ngeneration_generate_audio\ngeneration_provider_order\n```\n\nThis is what lets multiple providers behave like one execution system instead of many inconsistent APIs.\n\nExecution correctness is not only about providers. It is also about where requests are allowed to exist.\n\nOur middleware enforces API-only domains, separates marketing and dashboard surfaces, and strips headers that should never influence account identity.\n\n```\nif (!pathname.startsWith('/v1')) {\n  return new NextResponse(\n    JSON.stringify({\n      status: 'error',\n      error: {\n        code: 'BSE1001',\n        type: 'not_found',\n        message: 'Restricted access',\n      },\n    }),\n    { status: 404, headers: { 'Content-Type': 'application/json' } },\n  );\n}\n\nconst requestHeaders = new Headers(request.headers);\nrequestHeaders.delete('x-account-id');\n\nreturn NextResponse.next({\n  request: { headers: requestHeaders },\n});\n```\n\nThat matters because execution systems are also multi-tenant systems. The edge has to enforce those trust boundaries before requests reach the control plane.\n\nIn generative systems, execution is the most expensive part of the pipeline.\n\nEvery invalid request that reaches execution is not just an error. It is wasted compute, wasted time, and unnecessary cost.\n\nOur design ensures that:\n\nThis is why edge enforcement is not optional. It is part of execution itself.\n\nGenerative workloads are fundamentally different:\n\nWithout strict edge control:\n\nCloudflare allows us to enforce: correctness before execution.\n\nAt BabySea, execution infrastructure is not just routing and failover.\n\nIt is:\n\nCloudflare is not just a security layer for us.\n\nIt is where execution constraints begin.\n\nBy the time a request reaches our system, it has already been validated, constrained, and shaped.\n\nExecution is not just what happens inside the system.\n\nit starts at the edge", "url": "https://wpnews.pro/news/how-babysea-uses-cloudflare-to-secure-and-control-execution-at-scale", "canonical_source": "https://dev.to/ariesrandy/how-babysea-uses-cloudflare-to-secure-and-control-execution-at-scale-4l2", "published_at": "2026-11-22 03:05:38+00:00", "updated_at": "2026-11-22 03:12:29.403453+00:00", "lang": "en", "topics": ["ai-infrastructure", "ai-tools", "generative-ai", "mlops"], "entities": ["BabySea", "Cloudflare"], "also_reported_by": [], "takeaway": "Enforce request validation, WAF, rate limiting, and OpenAPI schema checks at the edge so invalid or abusive requests fail cheaply before consuming origin execution resources.", "recommendation": {"advice": "Put schema validation, WAF rules, and layered rate limits at the edge so malformed or abusive requests are rejected before they reach your execution layer and incur cost.", "playbook": "production-ops", "playbook_url": "https://wpnews.pro/playbooks/production-ops"}, "alternates": {"html": "https://wpnews.pro/news/how-babysea-uses-cloudflare-to-secure-and-control-execution-at-scale", "markdown": "https://wpnews.pro/news/how-babysea-uses-cloudflare-to-secure-and-control-execution-at-scale.md", "text": "https://wpnews.pro/news/how-babysea-uses-cloudflare-to-secure-and-control-execution-at-scale.txt", "jsonld": "https://wpnews.pro/news/how-babysea-uses-cloudflare-to-secure-and-control-execution-at-scale.jsonld"}}