feat(api): Add concurrent subscription update conflict resolution wit…#685
Open
Tekprecious wants to merge 3 commits into
Open
feat(api): Add concurrent subscription update conflict resolution wit…#685Tekprecious wants to merge 3 commits into
Tekprecious wants to merge 3 commits into
Conversation
…h OCC Implements an optimistic concurrency control (OCC) mechanism using version numbers to prevent silent data loss during concurrent updates. - Adds a 'version' field to key entities. - Introduces a backend OptimisticLockService for version checking. - Returns 409 Conflict on version mismatch. - Implements a client-side service with automatic retries and exponential backoff. - Prepares for manual conflict resolution via a Zustand store if retries fail. Closes Smartdevs17#613
Clears out multiple workspace errors: - Fixes Prettier layout/syntax issues in crdt.ts and .storybook/main.js. - Resolves import/export name collisions in the design-system barrel file. - Corrects broken relative import paths in test files. - Removes numerous unused variables and imports flagged by the linter.
|
@Tekprecious Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
…h OCC
feat(occ): Implement optimistic concurrency control
Closes #613
Description
This pull request introduces an Optimistic Concurrency Control (OCC) mechanism to prevent silent data loss when multiple users or automated processes perform simultaneous updates on the same resource.
Previously, the system was vulnerable to the "last-writer-wins" problem, where concurrent modifications could overwrite each other without warning. For example, a support agent updating subscription notes could have their changes silently discarded if a billing process updated the same subscription's status at the same time.
This implementation resolves the issue by adding a
versionfield to key entities (subscriptions,invoices,plans). Each update operation must now specify the version of the data it intends to modify. If the version on the server is newer, the update is rejected with a409 Conflicterror, forcing the client to resolve the conflict before retrying.Implementation Details
The solution is implemented across the full stack:
Database (
db/migrations/):003_add_occ_version_column.sql) adds an integerversioncolumn to thesubscriptions,invoices, andplanstables. The column defaults to1for new records.Backend (
backend/shared/):apiResponse.ts: The shared API response has been updated with a newCONFLICT_VERSION_MISMATCHerror code. The error payload now includes the currentversionof the resource on the server, enabling clients to fetch the latest data for retries.occ/OptimisticLockService.ts: This new, reusable service is the core of the backend implementation. It provides acheckVersionhelper to validate entity versions before an update.?force=trueadmin override parameter.Client-Side (
mobile/app/services/):conflictResolutionService.ts: A new client-side service introduceswithConflictResolution, a higher-order function designed to wrap any data mutation function.409 CONFLICT_VERSION_MISMATCHerrors. Upon conflict, it fetches the latest version of the entity, re-applies the user's intended changes, and retries the mutation up to 3 times with exponential backoff.useConflictStore) with the conflicting local and remote states. This enables a UI component to subscribe to the store and present a diff to the user for manual resolution.withConflictResolutionservice.How to Test
N), change the subscription's status and save.409 Conflicterror because the server is now at versionN+1. ThewithConflictResolutionservice will automatically:N+1).Pull Request Checklist
Quality Gates (All must pass before merge)
Additional Requirements
Reviewers
This PR will not be mergeable until all quality gates pass.