-
Notifications
You must be signed in to change notification settings - Fork 2
mosu-105 refactor: 학교 등록 할 때 레디스에도 등록하도록 수정 #107
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,6 +39,8 @@ public AvailableSchoolResponse getAvailableSchools() { | |
| @Transactional | ||
| public void registerSchool(SchoolRegistrationRequest request) { | ||
| schoolJpaRepository.save(request.toEntity()); | ||
| schoolQuotaCacheManager.addSchoolMaxCapacity(request.schoolName(), request.capacity()); | ||
| schoolQuotaCacheManager.addSchoolCurrentApplicationCount(request.schoolName(), 0L); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The value Consider defining a constant within the private static final long INITIAL_APPLICATION_COUNT = 0L; |
||
| } | ||
|
|
||
| @Transactional | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The database save operation and these two Redis cache updates are not atomic. The
@Transactionalannotation only covers the JPA operation. If the database transaction commits successfully but one of the subsequent Redis operations fails (e.g., if the Redis server is temporarily unavailable), the application's data will be in an inconsistent state. The new school will exist in the database but will be missing from the cache, which could lead to runtime errors or incorrect behavior in parts of the application that rely on this cache data.To ensure data consistency, it's better to couple the cache update to the transaction's lifecycle. A common pattern in Spring is to use
TransactionSynchronizationManagerto execute the cache updates only after the database transaction has successfully committed.Here's an example of how you could apply this pattern:
This ensures that the cache is only modified if the database write is successful, preventing data inconsistency.