// Stack

R2

Last updated ·

R2 is Cloudflare's S3-compatible object storage, reachable from Workers through a binding and from other systems through the S3 API, with no fees for data transferred out to the internet. EOXLABS uses R2 for the files a system stores (uploads, documents, media, exports and backups) while D1 or a Durable Object keeps the record of each object.

What does R2 guarantee about reads and writes?

Cloudflare's R2 consistency model is strong and global for the operations that matter to an application. After a write, an overwrite, a metadata update or a delete completes, every reader everywhere sees the new state, and a listing includes every object that exists at that moment. Among R2's own operations, only permission changes for access keys are eventually consistent, taking up to a minute.

Three details in the same document shape EOXLABS designs:

  • Concurrent writes to one key: the last writer to complete wins. Nothing merges the two; the other write is silently replaced.
  • Conditional writes are atomic. A PutObject with If-None-Match: * is evaluated against the committed state at the moment the write commits, so of several concurrent attempts to create the same key, at most one succeeds and the rest receive 412 PreconditionFailed.
  • Caching relaxes all of this. When a bucket is served through a custom domain with caching enabled, a deleted object can still be served from cache, a cached 404 can hide a new upload, and an overwritten object can be served stale until its TTL expires. The Workers binding and the S3 API bypass the cache.

How does EOXLABS use R2 in practice?

Immutable keys. EOXLABS writes each object once, under a key that includes a tenant prefix and a unique file ID or content hash, and never overwrites it. That one rule sidesteps last-writer-wins races, the limit of 1 concurrent write per second to the same key, and stale cache entries on public domains.

Create-once writes with a checksum. The R2 Workers API accepts conditional headers and an integrity hash on put(), and returns null when a precondition fails:

const sha256 = await crypto.subtle.digest("SHA-256", bytes);

const stored = await env.FILES.put(`t/${tenantId}/${fileId}`, bytes, {
  onlyIf: new Headers({ "If-None-Match": "*" }), // create once, never overwrite
  sha256,                                        // R2 checks the received bytes against this hash
  httpMetadata: { contentType },
  customMetadata: { tenantId },
});

if (stored === null) return new Response("Object already exists", { status: 409 });

Large files skip the Worker body. A Worker's request body is capped by the zone plan, so large uploads use multipart upload, with up to 10,000 parts, or presigned URLs that let a browser upload straight to R2 for a window between 1 second and 7 days. The Workers binding cannot sign those URLs, so EOXLABS signs them with a small SigV4 implementation such as aws4fetch. Unfinished multipart uploads are aborted automatically after 7 days.

Bytes in R2, facts in D1. R2 stores the object and a little metadata. Ownership, retention class, checksum and access history live in D1, where they can be queried. CONDUIT, the EOXLABS edge file-transfer product, follows this split: ingest coordinated by a Durable Object flows through a private tunnel into R2, with checksums and access history recorded in D1.

Processing on arrival. Event notifications send a message to a queue when a bucket changes, so scanning, thumbnailing or text extraction run in a consumer Worker rather than on the upload path. That is the entry point for document and media data pipelines.

Lifecycle and location. Lifecycle rules move objects to Infrequent Access or delete them by age. A bucket created with a jurisdictional restriction (eu, fedramp or us) keeps its objects in that jurisdiction.

Can R2 hold backups that ransomware cannot delete?

R2 bucket locks block overwrites and deletes for objects under a prefix, for a fixed period, until a date, or indefinitely. They also stop lifecycle rules from removing locked objects, and a bucket cannot be emptied while any lock rule exists.

The limit of that protection is who can change it. Lock rules are bucket configuration, and Cloudflare documents removing them through the dashboard, Wrangler or the API. Amazon S3 Object Lock in compliance mode, by contrast, prevents any user, including the account root user, from deleting a protected version before its retention date. When EOXLABS designs an immutable backup on R2, the credential that can edit bucket configuration is therefore part of the threat model: it is separate from every credential the application or backup agent holds. Ransomware-resilient backups and security and resilience cover the full design.

How does R2 compare with Amazon S3 and Workers KV?

R2Amazon S3Workers KV
Data sent to the internetNo egress charge on any storage classBilled per GB after the first 100 GB per month, aggregated across AWS servicesBilled for keys read and written, and for data stored
ConsistencyStrong read-after-write, including listingsStrong read-after-write for PUT and DELETE, including listingsEventually consistent across locations
Largest single itemJust under 5 TiB per objectUp to 50 TB with multipart upload25 MiB per value
Write protectionBucket lock rules per prefixObject Lock, including compliance mode; requires versioningNo retention lock
Access from a WorkerNative binding, or the S3 APIS3 API with signed requestsNative binding

For egress-heavy work such as media delivery in media and entertainment, the first row usually decides the question. For small configuration values read on every request, the edge cache in Workers KV fits better than an object store.

When not to use R2

  • Data that needs queries. The object API lists by prefix; it does not filter or join records. Anything searched or reported on belongs in D1 or Postgres, with R2 holding the referenced bytes.
  • A key rewritten many times per second. Writes to one key are limited to 1 per second and the last writer wins. Counters and shared documents belong in a Durable Object.
  • Retention that no administrator may ever shorten. Bucket lock rules can be removed by someone with configuration rights. If a regulation requires the stronger guarantee, an S3 bucket with Object Lock in compliance mode is the better target.
  • Infrequent Access for data that is read often. That storage class charges a retrieval fee per GB and bills a 30-day minimum storage duration, so it only pays off for archives that are rarely read.

R2 is the storage layer for files across the EOXLABS stack.

→

Related

+

More stack

Have a system that can’t go dark?

Start a project →