Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOC DB read-only replicas #587

Open
wants to merge 1 commit into
base: 6
Choose a base branch
from

Conversation

emteknetnz
Copy link
Member

@emteknetnz emteknetnz marked this pull request as draft September 16, 2024 08:10
@emteknetnz emteknetnz changed the title DOC DB read-only replicas DB DOC DB read-only replicas Sep 16, 2024
@emteknetnz emteknetnz marked this pull request as ready for review September 16, 2024 08:21
@emteknetnz emteknetnz marked this pull request as draft September 16, 2024 09:50
@emteknetnz emteknetnz force-pushed the pulls/5/db-replica branch 4 times, most recently from 07d3b24 to 3e95db2 Compare September 18, 2024 23:12
@emteknetnz emteknetnz marked this pull request as ready for review September 18, 2024 23:53
If one or more read-only replicas have been configured, then one of the read-only replicas will be selected from the pool of available replicas to handle queries for the rest of the request cycle. However the primary database will be used instead if one of the follow criteria has been met:

- The current query includes any mutable SQL such as `INSERT` or `DELETE`. The primary database will be used for the current query, as well as any future queries, including read queries, for the rest of the current request cycle. Mutable SQL is defined on [`DBConnector::isQueryMutable()`](api:SilverStripe\ORM\Connect\DBConnector::isQueryMutable()).
- The HTTP request that matched a rule defined in [`Director.must_use_primary_db_rules`](api:SilverStripe\Control\Director->must_use_primary_db_rules). By default the URL paths `Security`, `dev`, and `admin` (if `silverstripe/admin` is installed) are covered by this by default.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
- The HTTP request that matched a rule defined in [`Director.must_use_primary_db_rules`](api:SilverStripe\Control\Director->must_use_primary_db_rules). By default the URL paths `Security`, `dev`, and `admin` (if `silverstripe/admin` is installed) are covered by this by default.
- The HTTP request matches a routing rule defined in [`Director.must_use_primary_db_rules`](api:SilverStripe\Control\Director->must_use_primary_db_rules). By default the URL paths `Security/*`, `dev/*`, and `admin/*` (if `silverstripe/admin` is installed) are covered by this by default.

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated

Copy link
Member

Choose a reason for hiding this comment

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

Still need to add the /* so it's clear it's any path starting with those - i.e. dev/build is included, not just dev on its own.

en/08_Changelogs/5.4.0.md Outdated Show resolved Hide resolved
en/08_Changelogs/5.4.0.md Outdated Show resolved Hide resolved
@emteknetnz emteknetnz force-pushed the pulls/5/db-replica branch 6 times, most recently from 45647f1 to adb66c3 Compare September 25, 2024 23:21
Copy link
Member

Choose a reason for hiding this comment

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

As per silverstripe/silverstripe-framework#11379 (comment) $dataQuery->execute() now works a little differently to $dataQuery->query()->execute() - that needs to be called out.

Copy link
Member

@GuySartorelli GuySartorelli Sep 26, 2024

Choose a reason for hiding this comment

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

Also need to explicitly point out that DB::get_conn() will give you a replica by default if you have one configured, unless you explicitly pass in DB::CONN_PRIMARY.

Copy link
Member Author

@emteknetnz emteknetnz Sep 26, 2024

Choose a reason for hiding this comment

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

Re the first point, it's a pretty nuanced detail which is already mentioned in the docs, though not the changelog (which does refer to the docs for more detail)

Note that the withCorrectDatabase() call which $dataQuery->execute() makes which $dataQuery->query()->execute() doesn't simply means it respects DataObject.must_use_primary_db config

From docs:
For any query that goes through a call to DataQuery::execute(), the DataObject subclass being queried is configured with DataObject.must_use_primary_db set to true. This includes most commonly used ORM methods such as DataObject::get(), and excludes SQLSelect methods. By default all core security related DataObject subclasses have must_use_primary_db set to true.

A number of DataQuery methods now use withCorrectDatabase() e.g. DataQuery::count()

Copy link
Member

Choose a reason for hiding this comment

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

Re the first point, it's a pretty nuanced detail which is already mentioned in the docs, though not the changelog (which does refer to the docs for more detail)

I think it should be explicitly called out in the changelog, since it's a change in behaviour that some people may need to update their code to account for.

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated

If one or more read-only replicas have been configured, then one of the read-only replicas will be selected from the pool of available replicas to handle queries for the rest of the request cycle. However the primary database will be used instead if one of the follow criteria has been met:

- The current query includes any mutable SQL such as `INSERT` or `DELETE`. The primary database will be used for the current query, as well as any future queries, including read queries, for the rest of the current request cycle. Mutable SQL is defined on [`DBConnector::isQueryMutable()`](api:SilverStripe\ORM\Connect\DBConnector::isQueryMutable()).
- The HTTP request that matched a rule defined in [`Director.must_use_primary_db_rules`](api:SilverStripe\Control\Director->must_use_primary_db_rules). By default the URL paths `Security`, `dev`, and `admin` (if `silverstripe/admin` is installed) are covered by this by default.
Copy link
Member

Choose a reason for hiding this comment

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

Still need to add the /* so it's clear it's any path starting with those - i.e. dev/build is included, not just dev on its own.

Copy link
Member

@GuySartorelli GuySartorelli left a comment

Choose a reason for hiding this comment

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

@emteknetnz emteknetnz force-pushed the pulls/5/db-replica branch 2 times, most recently from e98eeb8 to b286048 Compare September 27, 2024 00:56
@emteknetnz emteknetnz changed the base branch from 5 to 6 September 27, 2024 02:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants