Competency Mastery Concurrency ADR#657
Conversation
|
Thanks for the pull request, @jesperhodge! 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:
🔘 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. |
Rejected-alternative items 1 and 2 nested Pros/Cons bullet lists directly under a continuation paragraph at a different indent with no blank line separator, and item 3's closing paragraph was indented to neither the list body nor its sub-bullets. docutils flagged these as errors under -W, failing the readthedocs build.
mgwozdz-unicon
left a comment
There was a problem hiding this comment.
Following up on feedback I'd shared earlier outside this PR favoring the batch-lock approach over keyed partitioning, given the Kafka/Redis dependency and the accuracy-over-latency tradeoff for this use case: this revision reflects that direction, and the Kafka/Redis coupling concern is resolved.
I'm wondering if we should also consider a per-learner lock instead of the single deployment-wide lock. A DB advisory lock keyed on a hash of user_id would give the same same-learner serialization the chosen approach relies on, but would let different learners' batches run in parallel, which the current design gives up entirely. It also sidesteps everything Alternative 1 was rejected for: no event-transport dependency, no partition-key contract with openedx-platform, no reconciliation backstop.
Rejected Alternative 2's argument doesn't quite cover this: it treats "per-learner isolation" as equivalent to "keyed partitioning" ("Making parallel evaluation correct requires... per-learner isolation, i.e. the keyed-partitioning alternative above"), but a per-learner DB lock gets you per-learner isolation without touching the transport at all. If there's a reason this doesn't hold up here (lock-management overhead across many concurrent per-learner locks, contention patterns, something else), that reasoning is worth adding to the doc.
Requesting changes to get this addressed, either by adding the analysis to Rejected Alternatives or by folding it into the Decision.
| - **Same-learner correctness.** Grade-change events arrive asynchronously and can be delivered out | ||
| of order and processed on more than one worker. Because writes are append-only, two evaluations | ||
| for the same learner that overlap can each read a stale snapshot of the sibling leaf statuses and | ||
| each append a derived roll-up computed from an incomplete picture (a write-skew). Leaf rows are | ||
| always correct, since each leaf is a pure function of its own grade; only the derived | ||
| group/competency rows can be left wrong. Nothing crashes and no constraint is violated, but a | ||
| learner's stored competency status can be silently incorrect. |
There was a problem hiding this comment.
Maybe this is a silly question, but can't we mitigate this by making sure to commit the transaction of the leaf statuses, and then re-read the latest commited state of the leaves before doing the roll-up?
There was a problem hiding this comment.
Another thing we can do (if it's necessary) is to arrange it so that the roll-ups are a log that have an incrementing version number, and create a composite unique index that would cause a conflict that would reject concurrent writes. The losing write would have to catch the error, increment the version number again, and re-read the database state.
There was a problem hiding this comment.
I think this question is not relevant anymore with the current version, right? Since now we only process on one worker at a time.
@mgwozdz-unicon I'll improve the description in the ADR. Here's why this solution is not good: It fights the batching that the performance depends on. The chosen design's throughput comes from batching across learners: one bulk read of current statuses, evaluate the whole batch in memory, one bulk write — a few round-trips regardless of how many learners/events are in the batch. A per-learner lock is naturally per-learner (in the earlier design, per-event): acquire lock → read that learner → evaluate → write → release, one learner at a time. That reintroduces per-learner (or per-event) transaction/commit overhead plus lock acquire/release churn, which is exactly the overhead batching was collapsing. Under bursty grading across many learners, that per-unit overhead dominates. Lock-lifecycle machinery would be multiplied across millions of keys. One deployment-wide lock has exactly one lifecycle to run: acquisition, timeout, stale-lock recovery on a crashed worker. A per-learner lock needs a per-learner lock table (or keyed advisory locks) and that same lifecycle replicated across potentially millions of learner keys, held and waited on concurrently, plus a connection+worker tied up per contended lock. |
| 2. ``competency_criteria_id``: logical foreign key to ``CompetencyCriterion.id`` (no database-level constraint) | ||
| 3. ``user_id``: logical foreign key to the learner's user id (no database-level constraint) | ||
| 4. ``status_id``: foreign key to ``CompetencyMasteryStatuses.id`` | ||
| 5. ``effective_source_timestamp``: the timestamp of the grade change this status was computed from, used for the out-of-order defense in :ref:`openedx-learning-adr-0004` |
There was a problem hiding this comment.
How is effective_source_timestamp different from created?
There was a problem hiding this comment.
This is premised on how ADR 0004 is now - it very well might change when we have incorporated your latest feedback; for example, if we integrate everything in a grade update transaction, then we should remove this new field.
I updated the description - it tracks when the grade was changed, not the competency, for async defensiveness per ADR 0004 as it is right now, but this is subject to change.
ormsbee
left a comment
There was a problem hiding this comment.
It seems like this entire ADR's premise is that per-event processing is prohibitively expensive, and we must therefore batch event processing under various locking mechanisms. It's not clear to me why that is. The subsection grade update is already a somewhat expensive, async task. We could plausibly wrap any updates we're doing to competency mastery data in the same transaction that writes the subsection grade update, so that those things are always kept in sync.
Approximately much work are we expecting to happen in order to do the mastery recalculation?
@ormsbee You're right, and it changed my thinking. I changed the ADR according to this: Per-event mastery work is actually small, a handful of reads and writes for one learner; the big numbers in the ADR (~200 leaves/course, billions of rows) are platform totals, not the cost of one recalculation. Throughput was never really the issue. The thing we were actually protecting against is write-skew: two concurrent updates for the same learner can each roll up a status from a stale view of sibling data. But mastery already only moves forward from non-completion to completion, and we can lean on that harder: if every write is "take the higher of stored and computed," concurrent updates converge on their own, no coordination needed. That's the piece that lets us drop the lock, and the throughput ceiling it created goes with it, because now updates can run in parallel. Which brings us back to your suggestion. It works now, and I don't have a strong preference yet between two shapes:
Curious which of the two shapes feels right to you, or if there's a third way we're not seeing. |
I don't think we should go with Option A because we do not intend for mastery to always be tied to a subsection grade. We've actually proposed tying mastery to several other things (course completion, unit grade (a concept that doesn't exist in the platform currently), problem grade, and rubric criterion grade (also a concept that doesn't exist in the platform currently)). So I would go with option B out of these two. |
| @@ -0,0 +1,150 @@ | |||
| # Competency data model (updated) | |||
There was a problem hiding this comment.
Unless others disagree, I think I'd recommend updating the existing PNG or trading it out for the mermaid diagram in the existing ADR 0002, as well as making it clearer that we already supported the active vs history dynamic and how the mastery levels fit in and anything else needed, rather than adding this extra file. We want to try to keep the ADRs light and concise and only change what really needs to be changed. We've also been asked to try to keep the implementation detail in ADRs to a minimum, which may also be relevant here.
| 6. ``created``: on ACTIVE, when the row was first written; on HISTORY, when this advance was appended | ||
|
|
||
| 3. Add a new database table for ``StudentCompetencyCriteriaGroupStatus`` with these columns: | ||
| The ACTIVE table additionally has a ``modified`` timestamp (last in-place update) and is unique |
There was a problem hiding this comment.
I feel like these modified fields should get enumerated like the rest of the fields.
There was a problem hiding this comment.
Is it better now?
|
Quick thoughts:
|
I could have sworn that there was a reason that we chose to use openedx-events for this process when we last discussed it in January, but I'm having a hard time remembering what it was. My first thought was dependency direction, but that doesn't seem right. I wonder if it was just that we were thinking that CBE would be taking more of a plugin shape so we wanted to keep it decoupled from openedx-platform. Outside of the other cons that Jesper raised for the single transaction (increased latency for the transaction and shared database), I don't have an issue with the single transaction approach if we want to accept those tradeoffs. Also as a side note: I'm pretty sure openedx-platform and openedx-core share a database already, so the "shared database" point in ADR 4 (line 220) probably needs some clarification. |
I'll remove it, it's a Claude remnant not needed. |
|
@ormsbee @mgwozdz-unicon sounds like we're in alignment, so I'll update the ADR to follow Dave's recommendation. |
|
Yeah, I was thinking that there's still value in using openedx-events to decouple from platform, without the message bus aspect of it. |
| Foreign keys that would cross the alias boundary (a learner-status row referencing a | ||
| criteria-definition row, and the reference to the user) are declared without database-level | ||
| constraints, as edx-platform does for ``PersistentSubsectionGrade.user_id``, so the tables can | ||
| live in different databases; the delete protection of :ref:`openedx-learning-adr-0002` is | ||
| therefore enforced in application code, not by a database constraint. |
There was a problem hiding this comment.
As mentioned in our discussion on #661 , not having foreign keys is definitely a downside of this approach, and making the deletion protection more complicated and fragile.
as edx-platform does for
PersistentSubsectionGrade.user_id, so the tables can live in different databases
Is there an ADR or documentation around PersistentSubsectionGrade.user_id ? I can see that it indeed is not a foreign key, but I can't find any explanation of that in the code nor ADRs, so I'm wondering if it is actually meant to be deployed on a separate database or not. As far as I can tell, the only table that has ever been designed to run on a separate database is the StudentModuleHistoryExtended table (it's the only one with a router).
UPDATE: ADRs in this PR have been completely rewritten as of July 17, 2026.
Strong involvement from Claude (AI).
I haven't fully reviewed changes to diagrams and ADRs 2 and 3 yet, made with Claude, need my own review still.
Summary
These are some significant changes to the data model and approach. They will warrant a good amount of discussion.
A main concern is that the leaf node table and leaf node history table could possibly contain billions of rows, at the scale of the
PersistentSubsectionGradetable of edx-platform. The current solution accepts this, but is this acceptable for OpenEdx? If not, there is also a rejected alternative to not store leaf nodes and just compute them at read time, but that would make it harder to keep subsection statuses frozen so that later competency criteria changes don't change the status.ADR 4: How learner competency mastery is recorded
correctly under concurrent, out-of-order grade-change events without
paying a per-event serialization cost at scale.
ADR 5: How competency criteria statuses for learners are stored at scale, given the tables
can be massive (billions of rows).