Skip to content

Commit db11743

Browse files
committed
Merge laster master into branch
2 parents dcfaf16 + 216b444 commit db11743

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

README.md

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,16 @@ Or install it yourself as:
2222
$ gem install mysql2-aurora
2323

2424
## Usage
25-
This gem extends Mysql2::Client. You can use `aurora_max_retry` option.
25+
This gem extends Mysql2::Client. You can use `aurora_max_retry` and `aurora_disconnect_on_readonly` options.
2626

2727
```ruby
2828
Mysql2::Client.new(
2929
host: 'localhost',
3030
username: 'root',
3131
password: 'change_me',
3232
reconnect: true,
33-
aurora_max_retry: 5
33+
aurora_max_retry: 5,
34+
aurora_disconnect_on_readonly: true
3435
)
3536
```
3637

@@ -44,8 +45,39 @@ development:
4445
password: change_me
4546
reconnect: true
4647
aurora_max_retry: 5
48+
aurora_disconnect_on_readonly: true
49+
4750
```
4851

52+
There are essentially two methods for handling and RDS Aurora failover. When there is an Aurora RDS failover event the primary writable server can change it's role to become a read_only replica. This can happen without active database connections droppping.
53+
This leaves the connection in a state where writes will fail, but the application belives it's connected to a writeable server. Writes will now perpetually fail until the database connection is closed and re-established connecting back to the new primary.
54+
55+
To provide automatic recovery from this method you can use either a graceful retry, or an immediate disconnection option.
56+
57+
### Retry
58+
59+
Setting aurora_max_retry, mysql2 will not disconnect and automatically attempt re-connection to the database when a read_only error message is encountered.
60+
This has the benefit that to the application the error is transparent and the query will be re-run against the new primary when the connection succeeds.
61+
62+
It is however not safe to use with transactions
63+
64+
Consider:
65+
66+
* Transaction is started on the primary server A
67+
* Failover event occurs, A is now readonly
68+
* Application issues a write statement, read_only exception is thrown
69+
* mysql2-aurora gem handles this by reconnecting transparently to the new primary B
70+
* Aplication continues issuing writes however on a new connection in auto-commit mode, no new transaction was started
71+
72+
The application remains un-aware it is now operating outside of a transaction, this can leave data in an inconcistent state, and issuing a ROLLBACK, or COMMIT will not have the expected outcome.
73+
74+
### Immediate disconnect
75+
76+
Setting aurora_disconnect_on_readonly to true, will cause mysql2 to close the connection to the database on read_only exception. The original exception will be thrown up the stack to the application.
77+
With the database connection disconnected, the next statement will hit the disconnected error and the application can handle this as it would normally when been disconnected from the database.
78+
79+
This is safe with transactions.
80+
4981
## Contributing
5082

5183
Bug reports and pull requests are welcome on GitHub at https://github.com/alfa-jpn/mysql2-aurora.

0 commit comments

Comments
 (0)