Skip to content

Conversation

@RealOrangeOne
Copy link
Contributor

@RealOrangeOne RealOrangeOne commented Jan 26, 2026

What is the context of this PR?

In #425, a race condition was fixed where data created would not be immediately available. This is likely because the replica instances had not synced the relevant data yet. The fix is to force the write instance to be used for the subsequent query.

Unfortunately, there's little which can be done about this lag. In the case of #425, the queries are already using bulk_create / bulk_update, so they'll be done in a single operation, and should be synchronised at once. Therefore, trying to batch them in a transaction is unlikely to help.

Since this pattern is likely to come up semi-often, I've written a small force_write_db_for_queryset helper to make it clearer why .using is being called. I've modified the dataset view to use the force_write_db_for_queryset helper, but only when necessary, so that most cases can still use the replica instances. I've also added force_write_db to work for an entire block, and added it to the publishing management commands

The docs now reference these methods exclusively, rather than working around it with explicit .using calls (which can be unclear) or using a transaction (which has a performance overhead). DEFAULT_DB_ALIAS is the default for transaction.atomic, so that can be removed.

I've also added a test around nested transactions (ie savepoints), to confirm that definitely works.

How to review

Note that this PR contains both CMS-1000 and CMS-1001, due to the huge overlap in functionality.

Follow-up Actions

N/A

@RealOrangeOne RealOrangeOne requested a review from a team as a code owner January 26, 2026 17:20
@RealOrangeOne RealOrangeOne marked this pull request as draft January 27, 2026 15:16
@RealOrangeOne
Copy link
Contributor Author

Converting back to draft to include CMS-1000.

@RealOrangeOne RealOrangeOne changed the title CMS-1001 - Add force_write_db util to work around database replication lag CMS-1000 / CMS-1001 - Better support forcing the write database instance Jan 27, 2026
Copy link
Contributor

@MaciekBaron MaciekBaron left a comment

Choose a reason for hiding this comment

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

Just two nit-picks, otherwise LGTM!

@RealOrangeOne RealOrangeOne marked this pull request as ready for review January 27, 2026 17:23
@MebinAbraham MebinAbraham changed the title CMS-1000 / CMS-1001 - Better support forcing the write database instance Better support forcing the write database instance Jan 29, 2026
@MebinAbraham MebinAbraham added Documentation Improvements or additions to documentation DX Developer eXperience labels Jan 29, 2026
Copy link
Contributor

@sanjeevz3009 sanjeevz3009 left a comment

Choose a reason for hiding this comment

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

Apart from the comments left already. LGTM!

Copy link
Contributor

@MaciekBaron MaciekBaron left a comment

Choose a reason for hiding this comment

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

Marking as "changes requested" as we still have those two nit-picks I left (sorry!)

@RealOrangeOne
Copy link
Contributor Author

@MaciekBaron Both should be fixed - I've just commented the relevant commits

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Documentation Improvements or additions to documentation DX Developer eXperience

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants