Skip to content

[improve][broker] Cache publish metadata and avoid repeated parsing in the broker send path#25555

Open
dao-jun wants to merge 3 commits intoapache:masterfrom
dao-jun:improve/metadata_parse
Open

[improve][broker] Cache publish metadata and avoid repeated parsing in the broker send path#25555
dao-jun wants to merge 3 commits intoapache:masterfrom
dao-jun:improve/metadata_parse

Conversation

@dao-jun
Copy link
Copy Markdown
Member

@dao-jun dao-jun commented Apr 21, 2026

Motivation

ServerCnx.handleSend sits on the hot publish path, but the same MessageMetadata can be parsed multiple times for a single message. In the persistent-topic flow, producer-side encryption validation, delayed-delivery checks, and deduplication/chunk handling may all reparse the same payload. That adds avoidable CPU overhead
to a latency-sensitive path.

This change reduces that overhead by parsing metadata once per publish attempt and reusing it across the broker publish flow. It also keeps the behavior safe by invalidating the cached metadata after broker interceptors run, so any interceptor-side buffer mutation does not reuse stale data.

Modifications

  • Make send debug logging in ServerCnx lazy, so metadata is not parsed when debug logging is disabled.
  • Create MessagePublishContext before producer-side publish validation starts.
  • Cache MessageMetadata in MessagePublishContext and reuse it within the same publish attempt.
  • Reuse cached metadata in:
    • producer encryption-required validation
    • persistent topic maximum delivery delay validation
    • deduplication paths that previously reparsed message metadata
  • Invalidate the cached metadata after BrokerInterceptor.onMessagePublish(...) to preserve correctness if an interceptor mutates the payload.

Verifying this change

  • Make sure that the change passes the CI checks.

(Please pick either of the following options)

This change is a trivial rework / code cleanup without any test coverage.

(or)

This change is already covered by existing tests, such as (please describe tests).

(or)

This change added tests and can be verified as follows:

(example:)

  • Added integration tests for end-to-end deployment with large payloads (10MB)
  • Extended integration test for recovery after broker failure

Does this pull request potentially affect one of the following parts:

If the box was checked, please highlight the changes

  • Dependencies (add or upgrade a dependency)
  • The public API
  • The schema
  • The default values of configurations
  • The threading model
  • The binary protocol
  • The REST endpoints
  • The admin CLI options
  • The metrics
  • Anything that affects deployment

Copy link
Copy Markdown
Member

@lhotari lhotari left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is a very useful change. I've also been thinking of this when I added a similar optimization for the dispatcher side.
A few review comments.

@dao-jun
Copy link
Copy Markdown
Member Author

dao-jun commented Apr 21, 2026

@lhotari Thanks for your review, the most of pulsar downstream projects(kop, rop, mop...) are not maintained anymore now, maybe some private forks are active, but it's not our duty to maintain their downward compatibility.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants