Cloudflare Workers at the application boundary.
Give the Worker a narrow adaptation responsibility and keep business authorization and durable state with an explicit owner. Construct origin requests deliberately and test credentials, retries, placement, and contract errors across both sides.
Inside this article 5 sections +
Use a Cloudflare Worker at the application boundary when it provides a clear adaptation, routing, or request-processing capability. Give it a narrow contract with the backend, explicit credentials, and a defined failure policy. Keep authoritative business state where transactions and ownership can be enforced.
A Worker that becomes an undocumented second backend can make an integration harder to operate. The goal is to explain what the Worker owns, what the origin still decides, and how a customer operation remains traceable across both.
Choose one boundary responsibility
Consider an illustrative reporting application exposing a compact customer API over an older internal service. A Worker can normalize the public route, validate request shape, add a correlation identifier, and translate the response into the public contract. The origin still decides whether the caller may read the requested account report.
Write that responsibility as an interface. List accepted methods and paths, permitted request fields, response fields, timeout behavior, and the error categories clients may receive. Avoid a general-purpose proxy that accepts arbitrary destinations or forwards every incoming header.
The existing SaaS edge boundary guide covers cache and origin protection. This integration design goes further into the Worker-to-origin contract: how the two implementations authenticate, retry, observe, and evolve together.
Construct the origin request deliberately
Build the upstream URL from an allowlisted route and a configured origin, not a destination supplied by the caller. The following JavaScript function demonstrates only route mapping. It does not implement authentication, permission checks, or a complete Worker handler.
function reportOriginUrl(publicUrl, originBase) {
const incoming = new URL(publicUrl);
const match = /^\/api\/reports\/([A-Za-z0-9_-]+)$/.exec(incoming.pathname);
if (!match) throw new Error("Unsupported report route");
return new URL(
`/internal/reports/${encodeURIComponent(match[1])}`,
originBase
).toString();
}
Choose which query parameters and headers are allowed separately. Strip caller-supplied internal identity headers before adding trusted metadata. Preserve the customer credential or exchange it through an explicitly designed mechanism so the origin can make the required resource decision.
Store Worker credentials through secret bindings rather than embedding values in source code. Define rotation and the origin's accepted credential overlap. A deployment that changes only one side of the contract can interrupt every request even when both services remain individually healthy.
Keep placement and state as separate decisions
Running code close to the user is not always the lowest-latency path when each request makes several sequential calls to one distant backend. Measure the complete interaction: client to Worker, Worker to origin, and all required round trips. A fast first hop can hide a slow conversation with the data owner.
Cloudflare offers placement controls, including Smart Placement, whose documented behavior depends on traffic and handler type. Validate the selected mode against the actual Worker; do not assume that a placement setting affects every RPC or named entrypoint in the same way.
Service bindings can connect Workers without exposing an internal Worker publicly. They are useful when splitting an adapter from a shared internal capability, but they are not an automatic private connection to any external origin. Model the actual service boundary and authentication path.
Keep durable workflow state out of incidental in-memory variables. If the Worker starts a long-running operation, obtain a durable operation ID from the owner and return an accepted state. A request handler's lifetime is not a job scheduler or a persistent business ledger.
Define retries by operation semantics
A read can sometimes be retried within the remaining deadline, provided the response still represents the intended version and permissions. A write needs a stable operation identity or another provider-supported deduplication mechanism. A network timeout does not reveal whether the origin committed before the connection failed.
Assign retry ownership to one layer. If the browser, Worker, and origin client all retry independently, the same customer action can amplify into several backend attempts. The timeout budget guide explains how to reserve time for meaningful recovery rather than stacking full timeouts.
Map upstream failures into public errors without leaking internal URLs or credentials. Distinguish unavailable, unauthorized, invalid, and unresolved-operation states. Returning a generic success with an empty payload can make a failed integration appear to be a legitimate empty report.
Keep a response-size policy at the adapter. Buffering a large report to reshape it can consume resources that a streaming path would avoid. Decide which responses can be transformed safely and which should use an explicit download or asynchronous export contract.
Observe the contract at both ends
Propagate a correlation ID and record the public route, origin operation, status category, and duration. Avoid logging raw authorization headers or private report contents. The origin should be able to connect the request to its account decision and durable operation evidence.
Test an allowed request, an unsupported path, a forged internal header, an expired credential, a slow origin, and a response that violates the expected shape. Verify that one account cannot obtain another account's report through either direct or Worker-mediated access.
Roll out the adapter to a bounded route or account cohort and compare the public contract with the previous path. Keep rollback compatible with already accepted operations. Software implementation can connect Worker code, backend contracts, and operating checks into an integration that remains understandable after the initial launch.