Skip to content

Fix race condition in notification status upsert#2166

Closed
Copilot wants to merge 1 commit intofix/2164-notification-duplicationfrom
copilot/sub-pr-2165
Closed

Fix race condition in notification status upsert#2166
Copilot wants to merge 1 commit intofix/2164-notification-duplicationfrom
copilot/sub-pr-2165

Conversation

Copy link

Copilot AI commented Mar 3, 2026

The createNotifiedArticleStatus method used a find-then-insert/update pattern that was vulnerable to a race condition: two concurrent transactions could both observe no existing row and both attempt an insert, causing a unique constraint violation on user_id.

Changes

  • Atomic upsert via native SQL: Replaced the find-then-insert/update logic with a single INSERT ... ON DUPLICATE KEY UPDATE query in UserNotificationStatusRepository, eliminating the race condition at the DB level.
  • Service simplification: createNotifiedArticleStatus now delegates entirely to upsertLastNotifiedArticleId — no JPA findByUserId call, no branching.
// Before
userNotificationStatusRepository.findByUserId(userId)
    .ifPresentOrElse(
        status -> status.updateNotifiedArticleId(articleId),
        () -> userNotificationStatusRepository.save(new UserNotificationStatus(userId, articleId))
    );

// After
userNotificationStatusRepository.upsertLastNotifiedArticleId(userId, articleId);
INSERT INTO user_notification_status (user_id, last_notified_article_id)
VALUES (:userId, :notifiedArticleId)
ON DUPLICATE KEY UPDATE last_notified_article_id = VALUES(last_notified_article_id)

🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

Copilot AI mentioned this pull request Mar 3, 2026
4 tasks
Copilot AI changed the title [WIP] Address feedback on duplicate notification error fix Fix race condition in notification status upsert Mar 3, 2026
@taejinn taejinn closed this Mar 3, 2026
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