Skip to content

Fix race condition in analytics increment #22

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 1 commit into
base: main
Choose a base branch
from

Conversation

gul-ahmad
Copy link

Problem

The current implementation of the analytics increment method is susceptible to race conditions. In high-concurrency situations, this could lead to duplicate rows being created instead of proper increments.

Solution

Added database row-level locking using lockForUpdate() within a transaction to ensure atomic operations:

  • Used lockForUpdate() to prevent concurrent access to the same analytics row
  • Wrapped operations in a transaction for atomicity
  • Added proper locking when checking max analytics limit

Changes

  • Modified increment() method in DatabaseAnalyticsRepository to use transactions and row-level locking
  • Ensures only one row is created even with concurrent requests
  • Maintains existing functionality while fixing the race condition

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.

None yet

1 participant