Skip to content

Activate Hibernate second-level cache#474

Merged
mmwinther merged 13 commits intomainfrom
experiment/hibernate-cache
Jan 5, 2026
Merged

Activate Hibernate second-level cache#474
mmwinther merged 13 commits intomainfrom
experiment/hibernate-cache

Conversation

@mmwinther
Copy link
Member

@mmwinther mmwinther commented Dec 29, 2025

Summary

Some requests which trigger many database queries have been running very slowly. To help with this we activate the Hibernate "second-level" cache which persists between database transactions and is shared between threads. The database entities chosen to be cached are CorrespondenceMap since a high number of these are accessed in the longest-running requests.

Advantages

This change has shown to increase the speed of fully cached requests by over 3x. For example a request to https://klass-dev-cache.intern.test.ssb.no/api/klass/v1/classifications/131/changes\?from\=0000-01-01 now takes 22s where it takes 1m44s for instances without caching enabled.

Disadvantages

Due to the caching being quite aggressive, changes to the cached objects in the database may not be shown to users. To mitigate this we have been selective about which DB entities are cached. The CorrespondenceMap should primarily be for historical data which won't receive updates, but we need to run a thorough User Acceptance Test to verify this.

Other changes

  • Remove the H2 database dependency
  • Change remaining tests to use embedded Postgres DB
  • Reorder dependency specification to be logical

Testing

Have run unit tests and all data integrity tests, as well as manual testing on the endpoints which have had performance problems.

Ref: DPMETA-1204

Some requests which trigger many database queries have been running very slowly.
To help with this we activate the Hibernate "second-level" cache which persists
between database transactions and is shared between threads.

In simple local experiments this can increase the speed of fully cached requests
by over 3x.
@mmwinther mmwinther requested a review from a team as a code owner December 29, 2025 14:34
@sonarqubecloud
Copy link

sonarqubecloud bot commented Jan 2, 2026

Copy link
Contributor

@Jorgen-5 Jorgen-5 left a comment

Choose a reason for hiding this comment

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

Nice!

@mmwinther mmwinther merged commit a7bbffb into main Jan 5, 2026
10 checks passed
@mmwinther mmwinther deleted the experiment/hibernate-cache branch March 2, 2026 13:06
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