Skip to content

Commit adb66c3

Browse files
committed
DOC DB read-only replicas
1 parent 2142034 commit adb66c3

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

en/00_Getting_Started/03_Environment_Management.md

+2
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ $loader->loadFile($env);
9696

9797
Silverstripe core environment variables are listed here, though you're free to define any you need for your application.
9898

99+
If you want to configure environment variables for read-only replica databases, then view the [Read-only database replicas](/developer_guides/performance/db_read_only_replicas) documentation.
100+
99101
| Name | Description |
100102
| ---- | ----------- |
101103
| `SS_DATABASE_CLASS` | The database class to use. Only `MySQLDatabase` is included by default, but other values are available in optional modules such as [`PostgreSQLDatabase`](https://github.com/silverstripe/silverstripe-postgresql). Defaults to `MySQLDatabase`.|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
---
2+
title: Read-only database replicas
3+
summary: Using read-only database replicas to improve performance
4+
---
5+
6+
# Read-only database replicas
7+
8+
Read-only replicas are additional databases that are used to offload read queries from the primary database, which can improve performance by reducing the load on the primary database.
9+
10+
Read-only replicas are configured by adding environment variables that match the primary environment variable and suffixing `_REPLICA_<replica-number>` to the variable name, where `<replica_number>` is the replica number padding by a zero if it's less than 10, for example `SS_DATABASE_SERVER` becomes `SS_DATABASE_SERVER_REPLICA_01` for the first replica, or `SS_DATABASE_SERVER_REPLICA_12` for the 12th replica. Replias must be numbered sequentially starting from `01`.
11+
12+
```bash
13+
# Primary database
14+
SS_DATABASE_CLASS="MySQLDatabase"
15+
SS_DATABASE_SERVER="my-db-server"
16+
SS_DATABASE_PORT="3306"
17+
SS_DATABASE_USERNAME="my-user"
18+
SS_DATABASE_PASSWORD="my-password"
19+
SS_DATABASE_NAME="db"
20+
21+
# Read-only replica
22+
SS_DATABASE_SERVER_REPLICA_01="my-db-replica"
23+
SS_DATABASE_PORT_REPLICA_01="3306"
24+
SS_DATABASE_USERNAME_REPLICA_01="my-replica-user"
25+
SS_DATABASE_PASSWORD_REPLICA_01="my-replica-password"
26+
```
27+
28+
Replicas cannot define different configuration values for `SS_DATABASE_CLASS`, `SS_DATABASE_NAME`, or `SS_DATABASE_CHOOSE_NAME`. They are restricted to prevent strange issues that could arise from having inconsistent database configurations across replicas.
29+
30+
If one or more read-only replicas have been configured, then for each request one of the read-only replicas will be randomly 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:
31+
32+
- 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()).
33+
- The HTTP request matches a routing rule defined in [`Director.rule_patterns_must_use_primary_db`](api:SilverStripe\Control\Director->rule_patterns_must_use_primary_db). By default the URL paths `Security`, `dev`, and `admin` (if `silverstripe/admin` is installed) are covered by this by default.
34+
- A user with CMS access is logged in. This is done to ensure that logged in users will correctly see any CMS updates on the website frontend. Users without CMS access will still use a read-only replica.
35+
- For any query that goes through a call to [`DataQuery::execute()`](api:SilverStripe\ORM\DataQuery::execute()), the `DataObject` subclass being queried is configured with [`DataObject.must_use_primary_db`](api:SilverStripe\ORM\DataObject->must_use_primary_db) set to `true`. This includes most commonly used ORM methods such as [`DataObject::get()`](api:SilverStripe\ORM\DataObject::get()), and excludes [`SQLSelect`](api:SilverStripe\ORM\Queries\SQLSelect) methods. By default all core security related `DataObject` subclasses have `must_use_primary_db` set to `true`.
36+
- Any code wrapped in a call to [`DB::withPrimary()`](api:SilverStripe\ORM\DB::withPrimary()).
37+
- All queries that result from using the CLI.
38+
39+
## Forcing use of the primary database
40+
41+
When using database replicas you may need to force the use of the primary database to ensure there are no issues with the data being out of sync. The following methods are available to force the use of the primary database:
42+
43+
[`DB::setMustUsePrimaryDB()`](api:SilverStripe\ORM\DB::setMustUsePrimaryDB()) will force the use of the primary database for the rest of current request cycle. Once it has ben set it cannot be unset.
44+
45+
```php
46+
// Code here can use a replica
47+
48+
DB::setMustUsePrimaryDB();
49+
50+
// Code here will only use the primary database
51+
```
52+
53+
Code wrapped in a call to [`DB::withPrimary()`]((api:SilverStripe\ORM\DB::withPrimary())) will always use the primary database.
54+
55+
```php
56+
// Code here can use a replica
57+
58+
DB::withPrimary(function () {
59+
// Code here will only use the primary database
60+
});
61+
62+
// Code here can use a replica
63+
```

en/08_Changelogs/5.4.0.md

+13
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,26 @@ title: 5.4.0 (unreleased)
77
## Overview
88

99
- [Features and enhancements](#features-and-enhancements)
10+
- [Read-only replica database support](#db-read-only-replicas)
1011
- [Option to change `ClassName` column from enum to varchar](#classname-varchar)
1112
- [Other new features](#other-new-features)
1213
- [API changes](#api-changes)
1314
- [Bug fixes](#bug-fixes)
1415

1516
## Features and enhancements
1617

18+
### Read-only replica database support {#db-read-only-replicas}
19+
20+
Read-only replicas are additional databases that are used to offload read queries from the primary database, which can improve performance by reducing the load on the primary database.
21+
22+
Read-only replicas are configured by adding environment variables that match the primary environment variable and suffixing `_REPLICA_<replica-number>` to the variable name, where `<replica_number>` is the replica number padding by a zero if it's less than 10, for example `SS_DATABASE_SERVER` becomes `SS_DATABASE_SERVER_REPLICA_01` for the first replica, or `SS_DATABASE_SERVER_REPLICA_12` for the 12th replica. Replias must be numbered sequentially starting from `01`.
23+
24+
Replicas cannot define different configuration values for `SS_DATABASE_CLASS`, `SS_DATABASE_NAME`, or `SS_DATABASE_CHOOSE_NAME`. They are restricted to prevent strange issues that could arise from having inconsistent database configurations across replicas.
25+
26+
If one or more read-only replicas have been configured, then for each request one of the read-only replicas will be randomly 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:
27+
28+
See [read-only database replicas](/developer_guides/performance/read_only_database_replicas/) for more details.
29+
1730
### Option to change `ClassName` column from enum to varchar {#classname-varchar}
1831

1932
On websites with very large database tables it can take a long time to run `dev/build`, which can be a problem when deploying changes to production. This is because the `ClassName` column is an `enum` type which requires an a `ALTER TABLE` query to be run affecting every row whenever there is a new valid value for the column. For a very rough benchmark, running an `ALTER TABLE` query on a database table of 10 million records took 28.52 seconds on a mid-range 2023 laptop, though this time will vary depending on the database and hardware being used.

0 commit comments

Comments
 (0)