Skip to content

feat: bookmarks rm user foreign key db constraint#38811

Open
johanseto wants to merge 1 commit into
openedx:masterfrom
nelc:jlc/bookmarg-rm-db-user-constraint
Open

feat: bookmarks rm user foreign key db constraint#38811
johanseto wants to merge 1 commit into
openedx:masterfrom
nelc:jlc/bookmarg-rm-db-user-constraint

Conversation

@johanseto

Copy link
Copy Markdown
Contributor

Description

Title: Refactor: Remove Database Foreign Key Constraint on User Table in Bookmarks to Optimize Write Performance

Inspired by the courseware student module's good performance.

student = models.ForeignKey(User, db_index=True, db_constraint=False, on_delete=models.CASCADE)

1. Context & Architecture

The Bookmarks service allows students to save specific course components (e.g., videos, text blocks, or assignments) for quick reference later. Historically, the Bookmark model mapped directly to the central User table using a strict, database-enforced Foreign Key (FK) constraint. This ensured rigid referential integrity at the database layer.

2. The Problem: Write Bottlenecks and Lock Contention

In a high-traffic environment where thousands of students are interacting with the platform simultaneously, the act of creating or deleting a bookmark becomes a micro-transaction that can disproportionately impact the wider database ecosystem.

Because of the hard Foreign Key constraint, every time a new bookmark is inserted or updated, the database engine must acquire a shared lock on the parent User table row to verify the user exists and maintain referential integrity.

  • The User table is a "hot" table, concurrently accessed and modified by almost every other model in the system (e.g., CourseEnrollment, Completion, tracking logs).
  • Stacking these shared locks for frequent, low-priority write operations like bookmarking causes unnecessary transaction queueing.
  • This lock contention drags down the performance of the User table globally, resulting in latency spikes and reducing overall write throughput across the platform.

3. The Solution

This PR drops the explicit database-level Foreign Key constraint linking Bookmarks to the User table (utilizing db_constraint=False in the model definition).

The application will continue to store the user_id as a conceptual relationship to track ownership of the bookmark, but the database will no longer execute strict referential integrity checks or place shared locks on the User table during INSERT or UPDATE operations on bookmarks.

4. Impact & Benefits

  • Optimized Write Performance: INSERT operations for bookmarks are now completely decoupled from the User table's lock state, executing immediately without queueing.
  • Reduced Database Contention: Eliminating shared locks on the core User table during bookmarking events heavily reduces transaction overhead, improving the latency and responsiveness of the platform during high-traffic spikes.
  • Better Scalability for Low-Priority Actions: Bookmarking is a high-frequency, low-criticality action. Isolating its database impact protects core services like authentication, enrollment, and grade calculation from being slowed down by users interacting with course materials.

5. Trade-offs & Considerations

  • Application-Level Responsibility: We are trading strict database referential integrity for speed and scale. The database will no longer automatically block user deletions if dependent bookmarks exist.
  • Orphaned Record Management: Any cleanup of orphaned bookmarks (e.g., when a user is permanently deleted) must now be handled at the application layer or offloaded to an asynchronous background cleanup job (e.g., via Celery tasks).

Test

run migrations

2026-06-11_09-08

Inspired also by PR nelc#78

similar approach in openedx/completion#443

@openedx-webhooks openedx-webhooks added the open-source-contribution PR author is not from Axim or 2U label Jun 24, 2026
@openedx-webhooks

Copy link
Copy Markdown

Thanks for the pull request, @johanseto!

This repository is currently maintained by @openedx/wg-maintenance-openedx-platform.

Once you've gone through the following steps feel free to tag them in a comment and let them know that your changes are ready for engineering review.

🔘 Get product approval

If you haven't already, check this list to see if your contribution needs to go through the product review process.

  • If it does, you'll need to submit a product proposal for your contribution, and have it reviewed by the Product Working Group.
    • This process (including the steps you'll need to take) is documented here.
  • If it doesn't, simply proceed with the next step.
🔘 Provide context

To help your reviewers and other members of the community understand the purpose and larger context of your changes, feel free to add as much of the following information to the PR description as you can:

  • Dependencies

    This PR must be merged before / after / at the same time as ...

  • Blockers

    This PR is waiting for OEP-1234 to be accepted.

  • Timeline information

    This PR must be merged by XX date because ...

  • Partner information

    This is for a course on edx.org.

  • Supporting documentation
  • Relevant Open edX discussion forum threads
🔘 Submit a signed contributor agreement (CLA)

⚠️ We ask all contributors to the Open edX project to submit a signed contributor agreement or indicate their institutional affiliation.
Please see the CONTRIBUTING file for more information.

If you've signed an agreement in the past, you may need to re-sign.
See The New Home of the Open edX Codebase for details.

Once you've signed the CLA, please allow 1 business day for it to be processed.
After this time, you can re-run the CLA check by adding a comment below that you have signed it.
If the CLA check continues to fail, you can tag the @openedx/cla-problems team in a comment for further assistance.

🔘 Get a green build

If one or more checks are failing, continue working on your changes until this is no longer the case and your build turns green.

Details
Where can I find more information?

If you'd like to get more details on all aspects of the review process for open source pull requests (OSPRs), check out the following resources:

When can I expect my changes to be merged?

Our goal is to get community contributions seen and reviewed as efficiently as possible.

However, the amount of time that it takes to review and merge a PR can vary significantly based on factors such as:

  • The size and impact of the changes that it introduces
  • The need for product review
  • Maintenance status of the parent repository

💡 As a result it may take up to several weeks or months to complete a review and merge your PR.

@github-project-automation github-project-automation Bot moved this to Needs Triage in Contributions Jun 24, 2026
@johanseto johanseto changed the title feat: rm user foreign key db constraint feat: bookmarks rm user foreign key db constraint Jun 24, 2026
@mphilbrick211 mphilbrick211 moved this from Needs Triage to Needs Tests Run or CLA Signed in Contributions Jun 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

open-source-contribution PR author is not from Axim or 2U

Projects

Status: Needs Tests Run or CLA Signed

Development

Successfully merging this pull request may close these issues.

3 participants