Skip to content

feat(api): opt-in shared template cache scope for cross-team layer dedup - #3749

Open
AdaAibaby wants to merge 1 commit into
e2b-dev:mainfrom
AdaAibaby:feat/opt-in-shared-cache-scope
Open

AdaAibaby wants to merge 1 commit into
e2b-dev:mainfrom
AdaAibaby:feat/opt-in-shared-cache-scope

Conversation

@AdaAibaby

Copy link
Copy Markdown
Contributor

What

Add an opt-in, default-off env var TEMPLATE_CACHE_SCOPE that makes the template layer cache shared across teams instead of per-team.

Layer caching is already content-addressed (HashKeys = sha256 over base + keys, no team), but the storage path is prefixed by cacheScope, and the API hardcodes cacheScope = teamID. So two teams that build byte-identical layers each get a private copy of the index + tar (build-cache bucket) and the buildID-keyed memfile/rootfs (template bucket). The content hash already proves the bytes are identical.

Change

packages/api/internal/template-manager/cache_scope.go (new): cacheScopeFor(teamID) returns TEMPLATE_CACHE_SCOPE when set, else teamID.String().

Both call sites now use it:

  • create_template.go — TemplateCreateRequest.CacheScope
  • upload_template_layer_files.go — InitLayerFileUploadRequest.CacheScope

Orchestrator is unchanged — it already honors the CacheScope field in both requests.

Behavior

  • Unset (default): identical to today — scope is teamID, per-team isolation preserved.
  • Set: every team uses the same scope, so identical layers (and the buildIDs they resolve to, hence memfile/rootfs) are stored and reused once.

Safety

A shared scope removes per-team cache isolation (a team can hit / prove existence of / reuse another team's cached layers). That is unacceptable for multi-tenant E2B Cloud, fine for a single trusted org / self-host. Default-off keeps multi-tenant behavior intact; sharing is a deliberate operator opt-in.

Relation to #3742

Orthogonal and complementary. #3742 reclaims unreferenced {buildID}/ prefixes by liveness; it never merges duplicates that are each still referenced. This PR collapses future duplicates to one copy; #3742 later reclaims the old copies once they age out of reference.

Closes #3748

Layer caching is content-addressed but the storage path is prefixed by
cacheScope, which the API hardcodes to teamID. Two teams building identical
layers each get a private copy of the index+tar and the buildID-keyed
memfile/rootfs. In a single-trust-domain deployment that is pure waste.

Add TEMPLATE_CACHE_SCOPE: when set, use it as the scope for every team so
identical layers dedup across teams; when unset, fall back to teamID
(unchanged upstream behavior). Orchestrator already honors the CacheScope
field in TemplateCreateRequest / InitLayerFileUploadRequest, so no change
there. Default-off keeps per-team cache isolation intact for multi-tenant.

Refs e2b-dev#3748
@cla-bot cla-bot Bot added the cla-signed label Oct 26, 2026
AdaAibaby pushed a commit to AdaAibaby/infra that referenced this pull request Nov 25, 2026
…layer dedup

Read-only audit of the build-cache bucket: groups {scope}/files/{hash}.tar by
content hash across scopes and reports cross-scope duplicate layers, wasted
bytes, and a delete-candidate manifest. Deletes nothing; physical deletion is
a separate operator-gated step that consumes the manifest.

Complements template-gc (e2b-dev#3742): that tool reclaims unreferenced builds by
liveness in the TEMPLATE bucket; this one reports content-duplicate layers in
the BUILD_CACHE bucket. Neither merges the other's data. Pairs with the
opt-in shared cache scope (e2b-dev#3748/e2b-dev#3749) that prevents new duplicates.

Refs e2b-dev#3742 e2b-dev#3748
@AdaAibaby
AdaAibaby force-pushed the feat/opt-in-shared-cache-scope branch from 92b28f4 to 2687a34 Compare November 25, 2026 13:56

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow opt-in shared template cache scope for cross-team layer dedup (single-trust-domain deployments)

2 participants