Skip to content

[Performance]: Reduce synchronous streaming startup and first-event latency #209

Closed

Description

@MisterVVP

Proposal

Context

PR #204 improves persistent HTTP connections for unary traffic, but streaming responses still use a different lifecycle and close the HTTP connection when the stream finishes.

The PR #204 normal performance artifact shows that the two targeted streaming scenarios degrade significantly as concurrency increases, especially on JSON-RPC.

At concurrency 64 with the in-memory backend:

Scenario / metric gRPC HTTP+JSON JSON-RPC
SendStreamingMessage_FiniteStream first-event p95 12.64 ms 16.42 ms 42.02 ms
SendStreamingMessage_FiniteStream completion p95 17.17 ms 16.52 ms 42.65 ms
SubscribeToTask_FirstEventLatency first-event p95 10.24 ms 21.92 ms 35.80 ms

At concurrency 1, first-event p95 is roughly 0.4–1.5 ms for these paths, which suggests that concurrency-related scheduling, worker, serialization, and event-delivery overhead is a major part of the remaining cost.

The current HTTP+JSON client also creates a StreamHandle::WorkerThread for every SSE stream in HttpJsonTransport::StartSseStream(). JSON-RPC has the same general synchronous streaming model and additionally performs JSON-RPC envelope parsing for every SSE event.

Issue #169 already tracks a separate opt-in asynchronous server-streaming API and gRPC callback transport. This issue should not duplicate or block on that API redesign. The focus here is improving the existing synchronous/public streaming paths.

Goal

Reduce first-event and finite-stream completion latency for:

  • SendStreamingMessage_FiniteStream
  • SubscribeToTask_FirstEventLatency

without breaking the existing synchronous client/server APIs, stream semantics, cancellation behavior, or transport compatibility.

Scope

1. Profile stream startup and delivery phases

Measure enough detail to distinguish:

  • client stream-handle/session creation;
  • worker-thread creation and scheduling;
  • connection establishment/reuse behavior;
  • request encoding;
  • server dispatch and stream-session creation;
  • first event production;
  • SSE / JSON-RPC event framing;
  • socket write/flush;
  • client SSE parsing;
  • JSON-RPC envelope conversion;
  • observer dispatch;
  • cancellation and terminal completion.

Do not optimize based only on total scenario time when the scenario already exposes dedicated first-event/completion metrics.

2. Reduce per-stream worker startup overhead

Investigate replacing or amortizing per-stream worker-thread creation for HTTP+JSON and JSON-RPC while preserving current asynchronous observer semantics.

Possible directions include:

  • bounded/shared stream execution;
  • reusable worker infrastructure;
  • transport-owned execution rather than one newly-created OS thread per stream;
  • avoiding unnecessary thread hand-offs before the first event.

The implementation must preserve safe cancellation, destruction, and completion semantics.

3. Reduce event parsing and serialization overhead

  • Avoid unnecessary generic protobuf/JSON round trips in JSON-RPC SSE event handling.
  • Reduce temporary strings and copies in SSE parsing/framing.
  • Avoid redundant response metadata/header copies where safe.
  • Preserve protocol validation and error mapping.

4. Review server-side synchronous streaming hot paths

Profile and optimize avoidable:

  • watcher/polling wake-ups;
  • locks and condition-variable transitions;
  • event copies;
  • flush delays;
  • terminal-event bookkeeping.

Do not replace the existing public synchronous API with the async API proposed in #169.

5. Evaluate finite-stream HTTP connection lifecycle

PR #204 intentionally keeps streaming responses connection-closing under the current HTTP/1.1 model.

Investigate whether finite streams can safely avoid unnecessary connection setup/teardown overhead or use a reusable framing/lifecycle strategy without changing A2A semantics. This is an investigation item, not a requirement to force keep-alive where protocol/library constraints make it unsafe.

Performance validation

Keep the existing benchmark semantics unchanged and compare before/after results for:

  • SendStreamingMessage_FiniteStream
  • SubscribeToTask_FirstEventLatency

Across:

  • gRPC
  • HTTP+JSON
  • JSON-RPC

At:

  • concurrency 1
  • concurrency 4
  • concurrency 16
  • concurrency 64

Use the in-memory backend first to isolate transport/runtime overhead, then verify with PostgreSQL.

Prefer repeated equivalent focused runs and median results when evaluating small deltas.

Acceptance criteria

  • A profile/breakdown identifies the dominant first-event and completion costs before optimization.
  • Existing synchronous/public client and server APIs remain source-compatible.
  • Stream event ordering, cancellation, terminal delivery, and error behavior remain correct.
  • SendStreamingMessage_FiniteStream first-event or completion p95 improves by at least 15% at concurrency 64 on HTTP+JSON and JSON-RPC in equivalent repeated in-memory runs.
  • SubscribeToTask_FirstEventLatency first-event p95 improves by at least 15% at concurrency 64 on HTTP+JSON and JSON-RPC in equivalent repeated in-memory runs.
  • gRPC streaming performance does not materially regress (>5% median) even if the implementation work is primarily HTTP/JSON focused.
  • No target scenario reports operation errors.
  • If stream execution is changed to shared/bounded workers, tests prove cancellation/destruction safety and bounded worker growth under concurrent streams.
  • Existing streaming interoperability tests pass.
  • ./scripts/verify_changes.sh passes.
  • Mandatory TCK conformance remains at 100% for executed/non-skipped requirements.
  • The PR includes a before/after table for first-event p95, completion p95, and throughput, plus the exact performance configuration used.

Out of scope

References

Activity

  1. added
    performance
    on Aug 11, 2026
  2. MisterVVP commented on Aug 25, 2026

    @MisterVVP
    OwnerAuthor

    This is next

  3. self-assigned this
    on Aug 25, 2026
  4. moved this from Todo to In Progress in a2a-cpp SDK Roadmapon Aug 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

performance

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions