⚡ perf: Run Stream Lua Scripts via EVALSHA Instead of Per-Call EVAL - #56
Closed
devin-ai-integration[bot] wants to merge 1 commit into
Closed
devin-ai-integration[bot] wants to merge 1 commit into
devin-ai-integration[bot] wants to merge 1 commit into
Conversation
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Author
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
In Redis mode every streamed token runs two Lua scripts through
ioredis.eval(), which ships the full script source each time:CHUNK_APPEND_LUA(~3.9 KB) fromRedisJobStore.appendChunkandPUBLISH_SEQ_LUA(~1 KB) fromRedisEventTransport.evalPublishSequenced. With the defaultSTREAM_DELTA_COALESCE_MS=0a 1,000-token response sends ~5 MB of Lua text to Redis for a few tens of KB of payload, and Redis SHA1-hashes and re-parses the script body on every call.The stream store and transport now run every script through
evalScript(packages/api/src/cache/redisScript.ts), which sendsEVALSHA <sha1>and falls back toEVAL <script>only when the server answersNOSCRIPT(first use, restart,SCRIPT FLUSH). Same Lua, same key/arg layout, same atomicity and return values; bothevalshaandevalare already in the telemetry proxy's command set, so instrumentation is unchanged. The onepipeline.eval(owner-membership reconcile, not per token) is left as is.Chosen over
defineCommandbecause custom commands are attached to the raw client instance, which theinstrumentIORedisClientproxy does not observe, and because it keeps the existingeval-based mocks meaningful.How it works
Measured against a local Redis 7 (2,000 calls, script padded to
CHUNK_APPEND_LUA's 3,908 bytes,INFO statstotal_net_input_bytesdelta):eval(before)evalsha(after)Per token this removes ~5 KB of Redis ingress across the two hot scripts (≈98%) plus one SHA1 + Lua parse per call on the server, scaling with tokens × concurrent streams.
Change Type
Testing
npx tsc --noEmitandeslintinpackages/apijest RedisJobStore.spec RedisEventTransport.spec(mocks gain anevalshathat answersNOSCRIPT, so they exercise the fallback path)USE_REDIS=truestream integration suites (*.stream_integration.spec.ts, 320 tests) against a single Redis 7 node and against the 3-noderedis-configcluster. Tests that spied onevalto observe or fail a script now callflushScriptCachefirst so the next run goes through the observableEVALfallback, or spy onevalshawhen they only inject an error.Test Configuration:
Redis 7 (docker
redis:7-alpine) on 6379;redis-config/start-cluster.shon 7001–7003.Checklist
Link to Devin session: https://app.devin.ai/sessions/8d4b42d64bd546ab8a7dbaa95c39f01f
Open in Devin Desktop: https://app.devin.ai/desktop/session/8d4b42d64bd546ab8a7dbaa95c39f01f?variant=devin
Requested by: @berry-13