feat: bookmarks rm user foreign key db constraint#38811
Conversation
(cherry picked from commit 797d0be)
|
Thanks for the pull request, @johanseto! This repository is currently maintained by 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 approvalIf you haven't already, check this list to see if your contribution needs to go through the product review process.
🔘 Provide contextTo 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:
🔘 Submit a signed contributor agreement (CLA)
If you've signed an agreement in the past, you may need to re-sign. Once you've signed the CLA, please allow 1 business day for it to be processed. 🔘 Get a green buildIf one or more checks are failing, continue working on your changes until this is no longer the case and your build turns green. DetailsWhere 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:
💡 As a result it may take up to several weeks or months to complete a review and merge your PR. |
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.
openedx-platform/lms/djangoapps/courseware/models.py
Line 95 in b1b59dd
1. Context & Architecture
The
Bookmarksservice allows students to save specific course components (e.g., videos, text blocks, or assignments) for quick reference later. Historically, theBookmarkmodel mapped directly to the centralUsertable 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
Usertable row to verify the user exists and maintain referential integrity.Usertable is a "hot" table, concurrently accessed and modified by almost every other model in the system (e.g., CourseEnrollment, Completion, tracking logs).Usertable 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
Bookmarksto theUsertable (utilizingdb_constraint=Falsein the model definition).The application will continue to store the
user_idas 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 theUsertable duringINSERTorUPDATEoperations on bookmarks.4. Impact & Benefits
INSERToperations for bookmarks are now completely decoupled from theUsertable's lock state, executing immediately without queueing.Usertable during bookmarking events heavily reduces transaction overhead, improving the latency and responsiveness of the platform during high-traffic spikes.5. Trade-offs & Considerations
Test
run migrations
Inspired also by PR nelc#78
similar approach in openedx/completion#443