Repository navigation
Cache the expanded Avro schema and inline tags per schema in AvroSerializer and AvroDeserializer - #2457
Open
Alejandro (alexon1234) wants to merge 1 commit into
Conversation
AvroSerializer and AvroDeserializer ran fastavro.expand_schema and get_inline_tags on every message (confluentinc#2255). Cache both per Schema, and compute them in the deserializer only when it executes rules. Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Alejandro (alexon1234)
requested review from
a team and
Matthew Seal (MSeal)
as code owners
November 25, 2026 15:42
|
❌ The following commits have a committer email that isn't linked to a Github account, so their CLA status can't be verified. Please link the email to your Github account (or correct your git config) and push again: |
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues were identified for the Avro schema-caching changes.
No open issues 🎉
What changed in this PR
This PR addresses the Avro performance regression reported in #2458 by caching schema-derived data in the synchronous and asynchronous serializers and deserializers.
Changes:
- Cache expanded schemas and inline tags per
Schema, avoiding repeated work for deserialization and serialization withuse.latest.version. - Move deserializer cache access inside the block requiring a serialization context and subject.
- Add regression tests verifying schema expansion occurs once across repeated messages, and document the fix.
| File | Description |
|---|---|
tests/schema_registry/_sync/test_avro_serdes.py |
Adds synchronous regression tests for schema-expansion caching. |
tests/schema_registry/_async/test_avro_serdes.py |
Adds asynchronous regression tests for schema-expansion caching. |
src/confluent_kafka/schema_registry/_sync/avro.py |
Adds schema-derived caches and conditional deserializer preprocessing. |
src/confluent_kafka/schema_registry/_async/avro.py |
Implements matching caching behavior for asynchronous Avro operations. |
CHANGELOG.md |
Documents the Avro performance fix. |
Review effort: Balanced
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.
What
Since 2.14.2 (#2255),
AvroDeserializer.__call__runsfastavro.expand_schema(reader_schema)on every message. It runs whether or not the subject has rules, andget_inline_tags(reader_schema)walks the whole schema on every message too.AvroSerializerdoes the same withuse.latest.version. Both results depend only on the schema, so this work is repeated on every message.With the 61-field schema in the reproducer below,
AvroDeserializertakes about 2.4 times as long per message as it does without these per-message calls. The cost grows with schema size.git bisectbetween v2.14.0 and v2.14.2 points to 3fd7408 (#2255).This PR:
expand_schema(...)andget_inline_tags(...)perSchemainAvroSerializerandAvroDeserializer, next to the existing parsed-schema cache;ctx is not None and subject is not Noneblock, which is the only place they are used.Behavior does not change.
_get_parsed_schemareturns the same parsed schema for a givenSchema,transformdoes not modify the schema it reads, andRuleContextonly reads the inline tags.Reproducer
MockSchemaRegistryClient, one schema, the same message deserialized 50,001 times:Python 3.13, fastavro 1.13.1, x86_64 Linux:
expand_schemacallsChecklist
test_avro_deserializer_expands_reader_schema_onceandtest_avro_serializer_expands_latest_schema_once(async, plus the sync versions generated bytools/unasync.py). Both fail on master and pass with this change.References
Test & Review
pyteston these tests:tests/schema_registry/{_sync,_async}/test_avro.py,test_avro_serdes.pyandtest_avro_serdes_rules.py, andtests/schema_registry/_sync/test_cel_avro_message_transform.py,test_dlq_serdes.pyandtest_validation_serdes.py. Result: 341 passed (337 existing, 4 new).AvroDeserializercall made twice, and both results must be equal. Result: 192 passed, the same as on master.tools/unasync.py --check, flake8, black, isort, and mypy on the changed modules all pass.Open questions / Follow-ups
expand_schema(self._parsed_schema)on every message. It is not cached here because its parsed schema does not come from_get_parsed_schema.