Skip to content
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

Concurrency: Changes Synchronization & Deletion should be atomic #110

Open
Zhiyuan-Amos opened this issue Dec 5, 2021 · 0 comments
Open

Comments

@Zhiyuan-Amos
Copy link
Owner

Changes Synchronization & Deletion can technically be executed concurrently by the same user. This shouldn't be occurring as there is no use case for such a scenario. Also, this can lead to race conditions. For example, when executing Synchronization, which contains the following logic

Get all Changes by UserId
Process Changes
Delete Changes

If Process 1 is completes Delete Changes and Process 2 runs Process Changes, then double processing will be executed. Retrieval from external sources such as Images might fail as these resources might have been deleted already.

Let's add a new container Synchronizing, which contains {userId, state}. Both id & partition key will use userId.

  1. When Synchronization begins, add the userId with state isSynchronizing to Synchronizing. If an entry with the userId exists, the operation fails.
  2. Run Synchronization.
  3. On Synchronization complete, set existing state to pendingDeletion.
  4. When Deletion begins, set existing state to isDeleting. If an entry with the userId exists and the state is not pendingDeletion, or if an entry does not exist, the operation fails.
  5. On Deletion complete, delete entry.

To handle the scenario where either the Client or Server errors out, let's set ttl to 30s for auto-timeout. A race condition might occur if the ttl expires too quickly before the operation completes. To mitigate this, let's update ttl periodically while Synchronization is occurring.

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

No branches or pull requests

1 participant