Skip to content

perf(issues): stop holding the team row for the whole create - #240

Merged
imshashank merged 2 commits into
mainfrom
perf/issue-number-allocation
Aug 8, 2026
Merged

imshashank merged 2 commits into
mainfrom
perf/issue-number-allocation

Conversation

@imshashank

@imshashank imshashank commented Aug 8, 2026 •

Copy link
Copy Markdown
Contributor

What

allocateIssueNumber took a row lock on the team inside the create transaction and held it until commit. Everything after it, labels, activity, notifications, the sync id, ran with the team row still locked, so two people creating issues on the same team queued behind each other for the whole write rather than just the increment.

In production this was the single worst statement by total time:

Mean Calls Total Statement
103ms 471 48.6s update team set issue_counter = issue_counter + $3

Nothing else came close.

The change

Hoist requireTeam and allocateIssueNumber out of the transaction. The allocation now commits on its own, before the rest of the write starts.

The tradeoff, stated plainly

A refused write now leaves a gap in the numbering rather than reusing the number. Identifiers already tolerate gaps, and this is the same tradeoff a Postgres sequence makes. The alternative, keeping the allocation inside the transaction but moving it last, keeps numbering dense but reintroduces the lock on the retry path.

This reasoning is specific to issue numbers. It does not transfer to sync_id, where a gap is a delivery bug rather than a cosmetic one.

Measured

12 concurrent creates, 3 rounds, against local Postgres:

Total Per issue
Before 616ms 51.3ms
After 214ms 17.8ms

2.9x faster.

Tests

Four new tests in packages/core/tests/work/issue-service.test.ts. Honest note on what they prove: the three concurrency tests pass against the old code too, because the row lock also guaranteed distinct numbers. Only leaves a gap rather than reusing a number when the write is refused discriminates between the two implementations, verified by mutation testing.

Full core suite: 729 pass, 0 fail.

Greptile Summary

This PR shortens contention during issue creation by allocating and committing the team’s next issue number before starting the main creation transaction.

  • Moves team lookup and issue-number allocation outside the issue-write transaction.
  • Retains a transactional team lookup for the remaining create operation.
  • Adds concurrency, authorization, independent-counter-commit, and numbering-gap tests.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

Filename Overview
packages/core/src/work/issue-service.ts Moves issue-number allocation before the main transaction while retaining transactional validation and issue creation.
packages/core/tests/work/issue-service.test.ts Adds coverage for concurrent uniqueness, independent counter commits, team authorization, and intentional numbering gaps.

Sequence Diagram

sequenceDiagram
    participant C as Caller
    participant DB as Database
    participant TX as Create transaction
    C->>DB: requireTeam(teamId)
    C->>DB: increment issueCounter
    DB-->>C: allocated number (committed)
    C->>TX: begin
    TX->>DB: requireTeam(teamId)
    TX->>DB: validate and insert issue
    alt creation succeeds
        TX->>DB: commit issue and related writes
        TX-->>C: created issue
    else creation is refused
        TX->>DB: rollback issue writes
        Note over DB: Allocated number remains consumed
        TX-->>C: error
    end
Loading

Reviews (2): Last reviewed commit: "fix(issues): revalidate the team inside ..." | Re-trigger Greptile

allocateIssueNumber took a row lock on the team inside the create
transaction and held it until commit, so labels, activity, notifications
and the sync id all ran with the team row locked. Two people creating
issues on the same team queued behind each other for the full write,
which showed up as the slowest statement in production: 103ms mean over
471 calls, 48.6s total.

Hoist the team lookup and the number allocation out of the transaction.
The allocation commits on its own, so a refused write now leaves a gap
in the numbering rather than reusing a number, which identifiers already
tolerate.

12 concurrent creates: 616ms before, 214ms after.
@imshashank
imshashank requested a review from pulkitxm as a code owner August 8, 2026 14:59
@vercel

vercel Bot commented Aug 8, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
orbit Ready Ready Preview Aug 8, 2026 3:55pm

Request Review

@github-actions github-actions Bot added the tests Test coverage and test infrastructure label Aug 8, 2026
@coderabbitai

coderabbitai Bot commented Aug 8, 2026 •

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Issue creation now loads the team and allocates the issue number before starting its transaction. New tests cover concurrent uniqueness, increasing issue numbers, and number consumption when creation fails.

Changes

Issue Number Allocation

Layer / File(s) Summary
Pre-transaction allocation and validation
packages/core/src/work/issue-service.ts, packages/core/tests/work/issue-service.test.ts
createIssue allocates the team and issue number before the transaction. The transaction reuses those values. Tests cover concurrent creates, later issue creation, and failed creation after allocation.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

  • Noveum/orbit#31: Updates the same issue-number allocation flow and related concurrency behavior.

Suggested labels: area: issues

Suggested reviewers: pulkitxm

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly summarizes the main performance change by indicating that issue creation no longer holds the team row for the entire operation.
Description check ✅ Passed The description directly explains the transaction, locking, performance, numbering, tradeoffs, and tests changed by the pull request.
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch perf/issue-number-allocation

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/core/src/work/issue-service.ts`:
- Around line 718-720: Update createIssue so the target team is revalidated
inside the db.transaction immediately before the issue insert, and use that
transaction-scoped team for the issue’s team id/key and issueIdentifier
generation. Avoid relying on the pre-transaction team object or key after
allocateIssueNumber.

In `@packages/core/tests/work/issue-service.test.ts`:
- Around line 1243-1259: Update the test case “does not hold the team row while
the rest of the write runs” to create a deterministic pause after the first
issue’s number allocation, start the second create while that transaction
remains paused, then release the barrier and assert the second allocation
completes successfully. Ensure the test would fail if the allocation still held
the team row for the remainder of the transaction.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 189ade56-8d7f-4a87-aae9-1c6967597f57

📥 Commits

Reviewing files that changed from the base of the PR and between 2da40e0 and f164516.

📒 Files selected for processing (2)
  • packages/core/src/work/issue-service.ts
  • packages/core/tests/work/issue-service.test.ts

Comment thread packages/core/src/work/issue-service.ts Resolved
Moving the allocation out of the transaction also moved the team check
out, so a membership change, an archive or a key rename between the
check and the insert could produce an issue with stale authorization or
the wrong identifier.

Re-require the team inside the transaction and build the identifier from
that row. requireTeam is a plain select with no row lock, so this costs
one round trip and reintroduces none of the contention the change was
about.

Also replaces a test that did not discriminate. It created issues in
sequence and asserted the numbers rose, which the old transaction scoped
implementation satisfied too. It now asserts the counter advances even
when the write that follows is refused, which only holds when the
allocation commits on its own, plus a case covering the team check for
a principal who may not write to that team.
@imshashank
imshashank merged commit 3f55e0b into main Aug 8, 2026
12 checks passed
@imshashank
imshashank deleted the perf/issue-number-allocation branch August 8, 2026 16:03

This branch was successfully deployed

1 active deployment
Preview — 505337e5 Deployed Aug 8, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tests Test coverage and test infrastructure

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant