Skip to content

perf(api): externalise executionID + hold into a meta key to drop cjson.decode from the startTransition hot path - #3815

Open
AdaAibaby wants to merge 1 commit into
e2b-dev:mainfrom
AdaAibaby:perf/redis-eid-held-keys
Open

AdaAibaby wants to merge 1 commit into
e2b-dev:mainfrom
AdaAibaby:perf/redis-eid-held-keys

Conversation

@AdaAibaby

Copy link
Copy Markdown
Contributor

Problem (#3602)

startTransitionScript runs cjson.decode on the full sandbox JSON on Redis's single-threaded event loop. With ExpectExecutionID set (the evictor path) it decodes to compare the execution ID, and since the lifecycle-hold work it decodes unconditionally for the is_held check too. Under concurrent expiry (400+ sandboxes/node, multiple API replicas each running the 50 ms evictor loop) this serialises O(JSON) CPU on the main thread and stalls every other command.

#3603 proposed externalising only executionID. That no longer suffices: is_held(decoded) forces a decode regardless, so the win is cancelled. This PR supersedes #3603 by externalising both fields.

Change

A dedicated meta key beside each record, in the same slot, holds the two fields the hot path needs — executionID and the lifecycle hold (lifecycleHold + lifecycleHoldUntil), US-separated (packMeta).

  • Coherence by construction. Every script that writes the record key (add, start_transition, fenced_set, restore) writes the meta key in the same atomic script; remove deletes it with the record. The record writers are a closed set (verified: no Go-side Set/Del bypasses the scripts), so meta can never drift.
  • Hot path. startTransitionScript reads the meta key with one GET + a string split instead of cjson.decode.
  • Migration. A record written before this change has no meta key; the hot path then falls back to decoding the record exactly as before, so pre-deploy sandboxes stay correct until next written (which sets meta) or expired.
  • add/remove keep decoding the record for their gone-set/return-value logic — not the storm path.

Testing

Full packages/api/internal/sandbox/storage/redis suite passes on Linux with Redis, including TestDifferentialAgainstModel (model-based differential test — the strongest guard against meta/record incoherence), execution_pin, lifecycle_hold, scripts_remove, update_write_retry. go vet clean.

Fixes #3602. Supersedes #3603.

…on.decode from the hot path

startTransitionScript decoded the full sandbox JSON on Redis's single-threaded
event loop for every call with ExpectExecutionID set (the evictor path), and
after the lifecycle-hold work it decodes unconditionally for is_held too. Under
concurrent expiry (400+ sandboxes/node, many API replicas) that serialises
O(JSON) CPU on the main thread and stalls every other command (e2b-dev#3602).

Store the two fields the hot path needs -- executionID and the lifecycle hold
(token + until) -- in a dedicated meta key beside the record, in the same slot.
Every script that writes KEYS[1] (add, start_transition, fenced_set, restore)
now writes the meta key in the same atomic script, and remove deletes it with
the record, so meta can never drift from the record. startTransitionScript
reads the meta key with one GET and a string split instead of cjson.decode.

Migration: a record written before this change has no meta key; the hot path
then falls back to decoding the record exactly as before, so pre-deploy
sandboxes stay correct until they are next written (which sets the meta key) or
expire. add/remove keep decoding the record for their gone-set logic, which is
not the storm path.

Supersedes the stale single-key approach in e2b-dev#3603, which no longer applied
because is_held forced a decode regardless.

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.

perf(api): cjson.decode in startTransitionScript Lua script blocks Redis event loop under concurrent evictions

2 participants