|
| 1 | +--- |
| 2 | +title: "MariaDB read-only replication" |
| 3 | +sidebarTitle: "MariaDB read-only replication" |
| 4 | +description: Configure and access read-only MariaDB replicas to ease the load on a primary database. |
| 5 | +--- |
| 6 | + |
| 7 | +You can improve the performance of read-heavy applications by defining read-only replicas of your MariaDB database and then connecting your application to those replicas. |
| 8 | + |
| 9 | +Examples of read-heavy applications include: |
| 10 | +- Listing pages or dashboards |
| 11 | +- Reporting or analytics jobs |
| 12 | +- Background jobs that frequently query data |
| 13 | + |
| 14 | +{{< note theme="info" title="Note" >}} |
| 15 | +- **To prevent data loss or interruptions** during replication, you must configure the disk size for each replica. The replica service does not inherit the disk size of the primary database. The replica disk size must at least match the primary service's disk capacity. See the example below. |
| 16 | +- **Replication is asynchronous**: Delays of a few milliseconds might occur between writes on the primary database and reads on the replica database. |
| 17 | +- **Replicas are read-only**: This restriction ensures data consistency and integrity. Attempts to modify data will result in an SQL error. |
| 18 | +{{< /note >}} |
| 19 | + |
| 20 | +### Read-only vs. external replicas |
| 21 | +Read-only replicas are used primarily to improve application performance by distributing database read requests from read-heavy applications. |
| 22 | + |
| 23 | +Other common use cases for read-only replicas include: |
| 24 | +- Cross-region backup: Replicating data to different geographical regions |
| 25 | +- Data warehousing: Extracting data from production to analytics projects |
| 26 | + |
| 27 | +[External replicas](/add-services/mysql/mysql-replication.md) reside on remote servers and have different use cases, including disaster recovery. |
| 28 | + |
| 29 | +### Replica scope and sharing services {#replica-scope-sharing-services} |
| 30 | +MariaDB services (which provide access to databases and replicas) defined in a project cannot be accessed by or shared with applications in other projects. |
| 31 | + |
| 32 | +To share the MariaDB service among applications in different projects, you must complete the following high-level steps: |
| 33 | +1. Migrate the {{% vendor/name %}} projects to {{% vendor/company_name %}} Flex. |
| 34 | +1. Migrate the applications that are in those projects. |
| 35 | +1. Move the desired applications into one Flex project, rather than separate Flex projects. In {{% vendor/company_name %}} Flex, multiple applications in one project can share the same services. |
| 36 | + |
| 37 | +For details, see [Converting from {{% vendor/name %}}](https://docs.upsun.com/learn/tutorials/migrating/from-fixed.html) in the {{% vendor/company_name %}} Flex product docs. |
| 38 | + |
| 39 | +## 1. Configure the primary and replica services |
| 40 | + |
| 41 | +The following code fragment defines two MariaDB services: a primary and a replica. You can use this fragment as a template by copying it into your `services.yaml` or `application.yaml` file. |
| 42 | + |
| 43 | +Be sure to: |
| 44 | +- Replace `<VERSION>` with the [supported MariaDB version](/add-services/mysql/_index.md#supported-versions) that you need. Use the same version number for the primary and replica services. |
| 45 | +- Replace `<SIZE>` with a `disk` size in (MB) that is sufficient for the primary database's disk capacity. |
| 46 | +- **Important:** Use `replicator` as the endpoint name when you define the replica service. This is a special endpoint name that the replica service uses to connect to the database. |
| 47 | + |
| 48 | +```yaml {configFile="services"} |
| 49 | +services: |
| 50 | + db: |
| 51 | + type: mariadb:<VERSION> |
| 52 | + disk: <SIZE> |
| 53 | + configuration: |
| 54 | + schemas: |
| 55 | + - main |
| 56 | + endpoints: |
| 57 | + main: |
| 58 | + default_schema: main |
| 59 | + privileges: |
| 60 | + main: admin |
| 61 | + replicator: |
| 62 | + privileges: |
| 63 | + main: replication |
| 64 | + |
| 65 | + db-replica1: |
| 66 | + type: mariadb-replica:<VERSION> |
| 67 | + disk: <SIZE> |
| 68 | + configuration: |
| 69 | + schemas: |
| 70 | + - main |
| 71 | + endpoints: |
| 72 | + main: |
| 73 | + default_schema: main |
| 74 | + privileges: |
| 75 | + main: admin |
| 76 | + relationships: |
| 77 | + primary: db:replicator # Do not change the name `primary`. The service expects to receive this name. |
| 78 | + |
| 79 | + db-replica2: |
| 80 | + type: mariadb-replica:<VERSION> |
| 81 | + disk: <SIZE> |
| 82 | + configuration: |
| 83 | + schemas: |
| 84 | + - main |
| 85 | + endpoints: |
| 86 | + main: |
| 87 | + default_schema: main |
| 88 | + privileges: |
| 89 | + main: admin |
| 90 | + relationships: |
| 91 | + primary: db:replicator # Do not change the name `primary`. The service expects to receive this name. |
| 92 | +``` |
| 93 | +
|
| 94 | +### How it works |
| 95 | +
|
| 96 | +Using the sample code fragment above: |
| 97 | +
|
| 98 | +1. The primary service (`db`) defines an additional `replicator` endpoint, granting the `replication` privilege. This enables a replica to connect and continuously replicate data from the primary database. |
| 99 | + |
| 100 | + ```yaml |
| 101 | + replicator: |
| 102 | + privileges: |
| 103 | + main: replication |
| 104 | + ``` |
| 105 | + |
| 106 | +2. The replica services (`db-replica1` and `db-relica2`) use the `mariadb-replica` image type and connect back to the primary database service through the primary relationship. This establishes a replication link from `db` (the source) to `db-replica` (the target). |
| 107 | + |
| 108 | + ```yaml |
| 109 | + relationships: |
| 110 | + primary: db:replicator |
| 111 | + ``` |
| 112 | + |
| 113 | +The `db-replica1` and `db-replica2` replica services continuously stream data from the primary endpoint, maintaining a read-only copy of the primary database content. Write operations are not permitted on the replicas. |
| 114 | + |
| 115 | + |
| 116 | +## 2. Define the relationship between the application and the replicas |
| 117 | + |
| 118 | +Even if your application won't access the replication endpoint, you must expose the endpoint to an application as a relationship so that you can connect to it over SSH. |
| 119 | + |
| 120 | +Add a new relationship to your application container, as shown below: |
| 121 | + |
| 122 | +```yaml {configFile="app"} |
| 123 | +name: myapp |
| 124 | +
|
| 125 | +[...] |
| 126 | +
|
| 127 | +# Relationships enable an app container's access to a service. |
| 128 | +relationships: |
| 129 | + # More information: https://fixed.docs.upsun.com/anchors/fixed/app/reference/relationships/ |
| 130 | + database: |
| 131 | + service: db |
| 132 | + endpoint: main |
| 133 | + database-readonly: |
| 134 | + service: db-replica |
| 135 | +``` |
| 136 | + |
| 137 | + |
0 commit comments