spring-projects / spring-session

Spring Session
https://spring.io/projects/spring-session
Apache License 2.0
1.86k stars 1.11k forks source link

Reduce allocations from Redis operations #3207

Open theigl opened 1 month ago

theigl commented 1 month ago

RedisIndexedSessionRepository and RedisSessionExpirationPolicy create a lot of single-use proxies by relying on BoundHashOperations.

For instance in this case:

https://github.com/spring-projects/spring-session/blob/f6f4c8652e03257420bc35f44492c5dc5d4121a9/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisIndexedSessionRepository.java#L898-L903

https://github.com/spring-projects/spring-session/blob/f6f4c8652e03257420bc35f44492c5dc5d4121a9/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisIndexedSessionRepository.java#L733-L735

This showed up in our production profiler because of hundreds of MB allocated in java.lang.reflect.Method:

image

If I understand the code correctly, this is done purely for convenience and could simply be replaced by the following code:

String key = getSessionKey(sessionId);
sessionRedisOperations.opsForHash().putAll(key, this.delta);

Or am I overlooking something?

theigl commented 2 weeks ago

@marcusdacoregio: Hi and sorry for pinging you directly.

I'd be willing to provide a PR for this if I get a preliminary OK on my suggestion. I think the performance optimization would be worth it.

marcusdacoregio commented 2 weeks ago

Hi @theigl, thanks for reaching out.

Unfortunately, I'm not in the project anymore so I won't be able to approve/merge your PR if submitted. I'll ping @rwinch here who might be able to guide you.

I'd say that the best thing to do is to create a copy implementation of RedisIndexedSessionRepository, apply your changes and measure it again, that will help the team analyze your suggestion better.