Repository navigation
Fix chat stream persistence blocking per-chunk latency - #1968
Open
rkalghatgi wants to merge 1 commit into
Open
rkalghatgi wants to merge 1 commit into
rkalghatgi wants to merge 1 commit into
Conversation
ActiveConversationStreamSession.publish() ran a synchronous Cosmos DB read-modify-write on every single streamed chunk (via append_stream_session_event + _persist_metadata), blocking the next token from being yielded. With enable_redis_cache disabled, this produced a flat ~0.5-1.5 second delay per streamed word, reproducing identically locally and deployed, independent of model/reasoning effort/agents. Root-caused via live instrumentation: the Azure OpenAI stream itself is fast (confirmed via curl and a bare-SDK script), and the read side of this cache was already an in-memory hit — only the write side was synchronously hitting Cosmos per chunk. Nothing reads this persisted state more than once per reconnect/status/cancel request, so per-chunk durability was pure overhead. Fix: publish() now buffers the event text and latest metadata in memory and only flushes (one batched Cosmos/Redis write) every ~0.75s or on the stream's terminal event; close() flushes any trailing state first. No new threads/locks/queues — same single-threaded call order as before, just invoked less often. The live SSE delivery path (BackgroundStreamBridge) is untouched. Adds append_stream_session_events_batch() (Cosmos-fallback + Redis) in app_settings_cache.py alongside the existing single-event function, which collaboration's CollaborationEventSession continues to use unchanged. Bumps VERSION 0.261.065 -> 0.261.066. Files changed: - application/single_app/route_backend_chats.py - application/single_app/app_settings_cache.py - application/single_app/config.py - functional_tests/test_cosmos_wave1_cache_fallback.py (new batch-append tests) - functional_tests/test_chat_stream_persist_batching.py (new) - docs/explanation/fixes/CHAT_STREAM_PERSISTENCE_LATENCY_FIX.md (new) Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Contributor
|
rkalghatgi please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
This branch has not been deployed
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.
ActiveConversationStreamSession.publish() ran a synchronous Cosmos DB read-modify-write on every single streamed chunk (via append_stream_session_event + _persist_metadata), blocking the next token from being yielded. With enable_redis_cache disabled, this produced a flat ~0.5-1.5 second delay per streamed word, reproducing identically locally and deployed, independent of model/reasoning effort/agents.
Root-caused via live instrumentation: the Azure OpenAI stream itself is fast (confirmed via curl and a bare-SDK script), and the read side of this cache was already an in-memory hit — only the write side was synchronously hitting Cosmos per chunk. Nothing reads this persisted state more than once per reconnect/status/cancel request, so per-chunk durability was pure overhead.
Fix: publish() now buffers the event text and latest metadata in memory and only flushes (one batched Cosmos/Redis write) every ~0.75s or on the stream's terminal event; close() flushes any trailing state first. No new threads/locks/queues — same single-threaded call order as before, just invoked less often. The live SSE delivery path (BackgroundStreamBridge) is untouched.
Adds append_stream_session_events_batch() (Cosmos-fallback + Redis) in app_settings_cache.py alongside the existing single-event function, which collaboration's CollaborationEventSession continues to use unchanged.
Bumps VERSION 0.261.065 -> 0.261.066.
Files changed:
Summary
Linked issue
Refs #
Release Notes & Latest Features
Is this visible to end users?
Is this admin-facing (Admin Settings, governance, deployment, config)?
Should this become a Latest Feature card?
Screenshot needed for the card?
Version bump
application/single_app/config.pyVERSIONthird segment bumped, or not needed because this is docs-onlydeployers/version.txtbumped, or not needed becausedeployers/was not changedTesting / validation
Documentation
Security checklist
@swagger_route(security=get_auth_security())sanitize_settings_for_user()