Skip to content

GH-3879: Add cache to optimize header match performance. #3934

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

chickenchickenlove
Copy link
Contributor

Fixes: #3879
Issue link: #3879

In previous discussion, we decided to use LinkedHashMap for LruCache Implementation.
However, I think that we missed concurrency of KafkaListenerContainer.
We can set concurrency when we create KafkaListener like @KafkaListener(concurrency = 10, ....).

But, LinkedHashMap is not thread-safe.
So, I use ConcurrentLruCache provided by spring-framework to ensure thread-safe.

Fixes: spring-projects#3879
Issue link: spring-projects#3879

What
Add a LRU cache for pattern match of KafkaHeaderMapper.

Why?
To improve CPU usage used by pattern match of KafkaHeaderMapper.
Commonly, many Kafka records in the same topic will have the same header name.
Currently, Pattern Match has O(M*N) time complexity, where M is pattern
length, N is String length.

If results of patterns match are cached and KafkaHeaderMapper uses it,
KafkaHeaderMapper can expect improvement in terms of CPU usage.

Signed-off-by: Sanghyeok An <[email protected]>
Signed-off-by: Sanghyeok An <[email protected]>
@chickenchickenlove chickenchickenlove changed the title spring-projectsGH-3879: Add cache to optimize header match performance. GH-3879: Add cache to optimize header match performance. May 30, 2025
Copy link
Member

@artembilan artembilan left a comment

Choose a reason for hiding this comment

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

Just one nit-pick.
I love everything else.
Thank you again!

/**
* A Cache that remembers whether a header name matches the multi-value pattern.
*/
class HeaderPatternMatchCache {
Copy link
Member

Choose a reason for hiding this comment

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

This is cool solution!
No days without learning something new.
Thank you! 😄

So, this has to private static to optimize memory consumption.
And all the method not public.

Signed-off-by: Sanghyeok An <[email protected]>

private final ConcurrentLruCache<String, Boolean> multiValueHeaderPatternMatchCache = new ConcurrentLruCache<>(MAX_SIZE, key -> Boolean.TRUE);

private final ConcurrentLruCache<String, Boolean> singleValueHeaderPatternMatchCache = new ConcurrentLruCache<>(MAX_SIZE, key -> Boolean.TRUE);
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for the update .
Now I have a question: why no logic like “if not multi-value , than it is single”?
I just said something here about memory , and realized that we do waste it with this map for single-value headers. While it feels like just worry about caching multi is enough.
Looks like we need to dedicate more engineering to this algorithm to come up with better utilization.
Does it make sense what I’m asking?
Thanks.

Copy link
Contributor Author

@chickenchickenlove chickenchickenlove May 31, 2025

Choose a reason for hiding this comment

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

The current implementation is designed to cache the result (either single-value or multi-value) for any header name that has undergone pattern matching at least once.

If we only store the result for multi-value headers, then even for header names that were already evaluated as single-value, the pattern matching logic would still be executed repeatedly each time.

Initially, I intended to use a single ConcurrentLruCache to store false for single-value and true for multi-value headers, but that behavior wasn’t supported🥲, which is why the current structure was chosen.

IMHO, for an effective pattern match cache, both outcomes—single-value and multi-value—should be stored. Currently, each ConcurrentLruCache is configured with a size of 1000. What do you think about reducing it to 500 to optimize memory usage?

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.

Add cache to optimize header match performance.
2 participants