|
| 1 | +# Utopia Replication |
| 2 | + |
| 3 | +> [!IMPORTANT] |
| 4 | +> This repository is a read-only mirror of the [utopia-php monorepo](https://github.com/utopia-php/monorepo). Development happens in [`packages/replication`](https://github.com/utopia-php/monorepo/tree/main/packages/replication) — please open issues and pull requests there. |
| 5 | +
|
| 6 | +[](https://github.com/utopia-php/replication/actions/workflows/tests.yml) |
| 7 | + |
| 8 | +[](https://appwrite.io/discord) |
| 9 | + |
| 10 | +Utopia Replication is a Swoole-native MySQL binlog reader. It streams row-level changes (change data capture) from a MySQL server over the replication protocol, so a consumer can react to writes — invalidate caches, build projections, fan out events — by tailing a server locally instead of relying on application-level messaging. This library is maintained by the [Appwrite team](https://appwrite.io). |
| 11 | + |
| 12 | +Although this library is part of the [Utopia Framework](https://github.com/utopia-php/framework) project it is dependency free and can be used as standalone with any other PHP project or framework. |
| 13 | + |
| 14 | +## Getting Started |
| 15 | + |
| 16 | +Install using composer: |
| 17 | +```bash |
| 18 | +composer require utopia-php/replication |
| 19 | +``` |
| 20 | + |
| 21 | +## System Requirements |
| 22 | + |
| 23 | +Utopia Replication requires PHP 8.3 or later, and the [Swoole](https://github.com/swoole/swoole-src) extension (>=6.0) for coroutine socket I/O. The [OpenSSL](https://www.php.net/manual/en/book.openssl.php) extension is required for `caching_sha2_password` full authentication and TLS. |
| 24 | + |
| 25 | +### Source server |
| 26 | + |
| 27 | +The MySQL source must be configured for GTID-based row replication: |
| 28 | + |
| 29 | +```ini |
| 30 | +binlog_format = ROW |
| 31 | +gtid_mode = ON |
| 32 | +enforce_gtid_consistency = ON |
| 33 | +binlog_row_metadata = FULL # so column names arrive in the stream |
| 34 | +``` |
| 35 | + |
| 36 | +The connecting user needs the `REPLICATION SLAVE` and `REPLICATION CLIENT` privileges. |
| 37 | + |
| 38 | +## Usage |
| 39 | + |
| 40 | +```php |
| 41 | +use Utopia\Replication\Adapter; |
| 42 | +use Utopia\Replication\Adapter\MySQL; |
| 43 | + |
| 44 | +// Adapter is the polymorphic interface; MySQL is the binlog implementation. |
| 45 | +$replication = new MySQL( |
| 46 | + host: '127.0.0.1', |
| 47 | + port: 3306, |
| 48 | + username: 'replicator', |
| 49 | + password: 'secret', |
| 50 | + serverId: 1001, // unique among replicas |
| 51 | + schema: 'appwrite', // only emit changes for this database |
| 52 | + ssl: false, // when true, TLS verifies the server cert by default |
| 53 | +); |
| 54 | + |
| 55 | +$replication->start($checkpoint ?? null); // resume from a GTID set, or null for "now" |
| 56 | + |
| 57 | +foreach ($replication->getChanges() as $change) { |
| 58 | + // $change->action — Change::INSERT | UPDATE | DELETE |
| 59 | + // $change->table — physical table name |
| 60 | + // $change->rows — list of column => value maps (after-image for updates) |
| 61 | + // $change->gtid — executed-GTID-set checkpoint (advance on commit) |
| 62 | + |
| 63 | + foreach ($change->rows as $row) { |
| 64 | + // react to the change ... |
| 65 | + } |
| 66 | + |
| 67 | + $checkpoint = $change->gtid; // persist to resume after a restart |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +The reader runs inside a Swoole coroutine; `getChanges()` blocks (yielding the |
| 72 | +coroutine) while waiting for events. The GTID checkpoint advances on transaction |
| 73 | +commit, so a crash mid-transaction re-streams it — treat changes as idempotent. |
| 74 | + |
| 75 | +## Scope |
| 76 | + |
| 77 | +Deliberately minimal: MySQL 8, ROW-format binlog, and the event types needed for |
| 78 | +CDC (TABLE_MAP, WRITE/UPDATE/DELETE_ROWS, GTID, ROTATE, XID). JSON column values |
| 79 | +are skipped (bytes advanced, not decoded). Column names come from FULL row |
| 80 | +metadata, so no `INFORMATION_SCHEMA` lookups are required. |
| 81 | + |
| 82 | +## Tests |
| 83 | + |
| 84 | +```bash |
| 85 | +composer test # unit (pure decoders — no server needed) |
| 86 | +docker compose up -d --wait |
| 87 | +composer test:e2e # against a real MySQL 8 (basic CRUD, >16MiB rows, |
| 88 | + # caching_sha2 full-auth, TLS) |
| 89 | +``` |
| 90 | + |
| 91 | +## Copyright and license |
| 92 | + |
| 93 | +The MIT License (MIT) [http://www.opensource.org/licenses/mit-license.php](http://www.opensource.org/licenses/mit-license.php) |
0 commit comments