fix(teams): prevent concurrent slug collision with sequential retry (#499)#557
Open
Ridanshi wants to merge 4 commits into
Open
fix(teams): prevent concurrent slug collision with sequential retry (#499)#557Ridanshi wants to merge 4 commits into
Ridanshi wants to merge 4 commits into
Conversation
Signed-off-by: Prashantkumar Khatri <96608160+ShantKhatri@users.noreply.github.com>
Replace non-deterministic random suffix generation with sequential numeric candidates (my-team → my-team-1 → my-team-2, capped at 10). Wrap team creation in a bounded retry loop (5 attempts) so P2002 constraint violations from concurrent inserts trigger re-allocation rather than surfacing as a 409. The database-level @unique constraint on Team.slug remains the authoritative guard; application logic now recovers gracefully when it fires. Adds slug utility tests (createSlug, generateUniqueSlug determinism and bounds) and team route tests for retry-on-race-condition and retry-exhaustion paths. Closes Dev-Card#499
|
@Ridanshi is attempting to deploy a commit to the Prashantkumar Khatri's projects Team on Vercel. A member of the Team first needs to authorize it. |
|
Hi @Ridanshi, Thanks for opening this pull request. This PR has been automatically classified based on the files modified. Applied Labels
Primary Review Area
Reviewer@Harxhit has been identified as the primary reviewer for this pull request. If you have any questions regarding the affected area or implementation details, feel free to reach out to the assigned reviewer. Thank you for your contribution! |
CI — All Checks PassedBackend — SKIP
Mobile — SKIP
Web — SKIP
Last updated: |
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.
Summary
Math.random()suffix ingenerateUniqueSlugwith sequential numeric candidates (my-team→my-team-1→my-team-2, bounded at 10 attempts)@uniqueconstraint onTeam.slugis the authoritative guard; application layer now recovers when it fires under concurrent loadRoot cause
generateUniqueSlugchecked slug availability outside the transaction. Two concurrent requests could both observe the same slug as free, both enter$transaction, and the second would get a P2002 error that surfaced as an unrecoverable 409 — leaving one creation silently dead.Concurrency strategy
Optimistic with bounded retry (Option A): rely on the existing DB unique constraint as the true arbiter, detect the collision via P2002, and retry slug generation with the next sequential candidate. No locks, no serializable isolation, no infinite loops.
Retry strategy
generateUniqueSlug: deterministic sequential suffixes, max 10 candidates, throws if all takenFiles modified
apps/backend/src/utils/slug.ts— sequential suffix generation, bounded retries, typed signatureapps/backend/src/routes/team.ts— retry loop on P2002 in POST/, return type annotation, lint fixesapps/backend/src/__tests__/team.test.ts— concurrency retry tests, exhaustion test, import/lint fixesapps/backend/src/__tests__/slug.test.ts— new file: unit tests forcreateSlugandgenerateUniqueSlugSchema changes
None —
Team.slug @uniquealready existed inschema.prisma.Tests added
slug.test.ts: 10 tests coveringcreateSlugnormalization andgenerateUniqueSlugdeterminism, suffix increment, bounds, concurrencyteam.test.ts: retry-on-race-condition (201 after P2002 retry), retry-exhaustion (409 after 5 P2002s)Validation
eslint src/utils/slug.ts src/routes/team.ts src/__tests__/team.test.ts src/__tests__/slug.test.ts→ 0 errorsvitest run src/__tests__/slug.test.ts src/__tests__/team.test.ts→ 57/57 passFixes #499