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?

@artembilan
Copy link
Member

the pattern matching logic would still be executed repeatedly each time.

Right, my bad.
The problem is not about multi-value header names as caching concern was raised originally.
And that made my brain to think wrong direction.

The real problem that we always go against pattern matching when we could just cache the matching outcome to not go there any more if we got the same header name in the next record.

So, it sounds like we got a 3 states: single-value, multi-value and don't map.
I think we can use a LinkedHashMap as cache implementation and Boolean object as a value for header name.
So, every single time when we got the value from the cache (or better to say a method backed by cache) we would get a respective state for that header name.

Does this makes sense?
Thanks

@chickenchickenlove
Copy link
Contributor Author

@artembilan
Thanks for your comments! 🙇‍♂️

Don’t map refers to patterns that have not yet been evaluated, right?
If so, the current implementation already covers what you’re describing.

// 1.
if (this.headerMatchedCache.isMultiValuePattern(headerName)) {
    return true;
}

// 2.
if (this.headerMatchedCache.isSingleValuePattern(headerName)) {
    return false;
}

// In this step, it is 'dont map' state.
// In case the pattern matches multi-value
// 3.
this.headerMatchedCache.cacheAsMultiValueHeader(headerName);
...

// In case the pattern doesn't match multi-value
// 4.
this.headerMatchedCache.cacheAsSingleValueHeader(headerName);

// After this, it is not in 'dont map' state.

The don’t map state will be resolved to either multi-value or single-value as a result of the pattern match.
So, after step 4, the state will be determined as either multi-value or single-value.

Also, ConcurrentMessageListenerContainer shares KafkaHeaderMapper instance between child containers.
So, they can cause race condition when LinkedHashMap is full and removeOldestEntry() is called.

This is the reason why I used two separate ConcurrentLruCache instances.

IMHO, the race condition that might occur in removeEldestEntry() could be a minor issue.
If you agree that the race condition in removeEldestEntry() is negligible, I'll replace ConcurrentLruCache with LinkedHashMap instead.

What do you think?
Please let me know your opinion 🙇‍♂️ .

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