ちょっと話題の記事

【新機能】Amazon Aurora で読み込みエンドポイントが利用可能になりました

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

ウィスキー、シガー、パイプをこよなく愛する大栗です。

本日、Amazon Auroraで読み込みエンドポイント(Reader Endpoint)が発表されました。今までは、インスタンスエンドポイント(Instance Endpoint)とクラスターエンドポイント(Cluster Endpoint)が利用できましたが、新しいエンドポイントについて試してみました。

読み込みエンドポイントとは

現在Auroraには、以下の3種類のエンドポイントがあります。クラスターエンドポイントはWriterを指すエンドポイントで、インスタンスエンドポイントは各インスタンスを指しエンドポイントです。今回追加された読み込みエンドポイントはReaderを指すエンドポイントです。

  • クラスターエンドポイント(Cluster endpoint)
  • インスタンスエンドポイント(Instance endpoint)
  • 読み込みエンドポイント(Reader endpoint) ← New

各々のエンドポイントを図示すると以下のようになります。読み込みエンドポイントはReaderをラウンドロビンで負荷分散します。

Untitled (3)

読み込みエンドポイントを試す

Auroraを以下の様な3台構成にして検証してみます。

  • aurora-a1:Writer
  • aurora-a2:Reader
  • aurora-c1:Reader

RDS_·_AWS_Console

クラスタの情報を確認すると読み込みエンドポイントが増えています。

RDS_·_AWS_Console

実際にログインしてみます。innodb_read_only変数を確認するとONになっており、Readerにアクセスしていることが分かります。

$ mysql -uawsuser -pmypassword -h cluster1.cluster-ro-abcdefghijkl.ap-northeast-1.rds.amazonaws.com
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 20
Server version: 5.6.10-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show variables like 'hostname';
+---------------+---------------+
| Variable_name | Value         |
+---------------+---------------+
| hostname      | ip-10-7-2-178 |
+---------------+---------------+
1 row in set (0.00 sec)

mysql> show variables like 'innodb_read_only';
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| innodb_read_only | ON    |
+------------------+-------+
1 row in set (0.00 sec)

mysql>

少し間を置いてから、もう一度アクセスしてみます。innodb_read_only変数はONのままですが、hostname変数が変わっており、別のインスタンスへアクセスしていることが分かります。

$ mysql -uawsuser -pmypassword -h cluster1.cluster-ro-abcdefghijkl.ap-northeast-1.rds.amazonaws.com
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.6.10-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show variables like 'hostname';
+---------------+---------------+
| Variable_name | Value         |
+---------------+---------------+
| hostname      | ip-10-7-1-227 |
+---------------+---------------+
1 row in set (0.00 sec)

mysql> show variables like 'innodb_read_only';
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| innodb_read_only | ON    |
+------------------+-------+
1 row in set (0.00 sec)

mysql>

次はdigでDNSの状況を確認します。cluster1.cluster-ro-abcdefghijkl.ap-northeast-1.rds.amazonaws.comのTTLは1秒になっているようです。

[ec2-user@ip-192-0-2-1 ~]$ dig cluster1.cluster-ro-abcdefghijkl.ap-northeast-1.rds.amazonaws.com

; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.37.rc1.45.amzn1 <<>> cluster1.cluster-ro-abcdefghijkl.ap-northeast-1.rds.amazonaws.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 56918
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;cluster1.cluster-ro-abcdefghijkl.ap-northeast-1.rds.amazonaws.com. IN A

;; ANSWER SECTION:
cluster1.cluster-ro-abcdefghijkl.ap-northeast-1.rds.amazonaws.com. 1 IN	CNAME aurora-c1.abcdefghijkl.ap-northeast-1.rds.amazonaws.com.
aurora-c1.abcdefghijkl.ap-northeast-1.rds.amazonaws.com. 5 IN A	172.31.29.133

;; Query time: 27 msec
;; SERVER: 172.31.0.2#53(172.31.0.2)
;; WHEN: Thu Sep  8 23:21:28 2016
;; MSG SIZE  rcvd: 136

[ec2-user@ip-192-0-2-1 ~]$ dig cluster1.cluster-ro-abcdefghijkl.ap-northeast-1.rds.amazonaws.com

; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.37.rc1.45.amzn1 <<>> cluster1.cluster-ro-abcdefghijkl.ap-northeast-1.rds.amazonaws.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 13573
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;cluster1.cluster-ro-abcdefghijkl.ap-northeast-1.rds.amazonaws.com. IN A

;; ANSWER SECTION:
cluster1.cluster-ro-abcdefghijkl.ap-northeast-1.rds.amazonaws.com. 1 IN	CNAME aurora-a2.abcdefghijkl.ap-northeast-1.rds.amazonaws.com.
aurora-a2.abcdefghijkl.ap-northeast-1.rds.amazonaws.com. 5 IN A	172.31.6.104

;; Query time: 27 msec
;; SERVER: 172.31.0.2#53(172.31.0.2)
;; WHEN: Thu Sep  8 23:21:31 2016
;; MSG SIZE  rcvd: 136

フェイルオーバーをしてから確認してみます。先程はaurora-a2.abcdefghijkl.ap-northeast-1.rds.amazonaws.comaurora-c1.abcdefghijkl.ap-northeast-1.rds.amazonaws.comへアクセスできていましたが、aurora-a1.abcdefghijkl.ap-northeast-1.rds.amazonaws.comaurora-c1.abcdefghijkl.ap-northeast-1.rds.amazonaws.comに変わっています。

[ec2-user@ip-192-0-2-1 ~]$ dig cluster1.cluster-ro-abcdefghijkl.ap-northeast-1.rds.amazonaws.com

; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.37.rc1.45.amzn1 <<>> cluster1.cluster-ro-abcdefghijkl.ap-northeast-1.rds.amazonaws.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 32184
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;cluster1.cluster-ro-abcdefghijkl.ap-northeast-1.rds.amazonaws.com. IN A

;; ANSWER SECTION:
cluster1.cluster-ro-abcdefghijkl.ap-northeast-1.rds.amazonaws.com. 1 IN	CNAME aurora-a1.abcdefghijkl.ap-northeast-1.rds.amazonaws.com.
aurora-a1.abcdefghijkl.ap-northeast-1.rds.amazonaws.com. 5 IN A	172.31.0.232

;; Query time: 33 msec
;; SERVER: 172.31.0.2#53(172.31.0.2)
;; WHEN: Thu Sep  8 23:39:35 2016
;; MSG SIZE  rcvd: 136

[ec2-user@ip-192-0-2-1 ~]$ dig cluster1.cluster-ro-abcdefghijkl.ap-northeast-1.rds.amazonaws.com

; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.37.rc1.45.amzn1 <<>> cluster1.cluster-ro-abcdefghijkl.ap-northeast-1.rds.amazonaws.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 45675
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;cluster1.cluster-ro-abcdefghijkl.ap-northeast-1.rds.amazonaws.com. IN A

;; ANSWER SECTION:
cluster1.cluster-ro-abcdefghijkl.ap-northeast-1.rds.amazonaws.com. 1 IN	CNAME aurora-c1.abcdefghijkl.ap-northeast-1.rds.amazonaws.com.
aurora-c1.abcdefghijkl.ap-northeast-1.rds.amazonaws.com. 5 IN A	172.31.29.133

;; Query time: 17 msec
;; SERVER: 172.31.0.2#53(172.31.0.2)
;; WHEN: Thu Sep  8 23:39:36 2016
;; MSG SIZE  rcvd: 136

さいごに

Reader Endpointが無い時にはWriterがフェイルオーバーすると、同じインスタンスに元々Readerとしてアクセスしていた負荷とWriterとしての負荷の両方がかかり過負荷になることがありました。ReaderとWriterの負荷の両方をかけないためには、innodb_read_only変数をチェックしたり、クラスターエンドポイントの変化時にReaderへアクセスするエンドポイントを更新する処理を独自に追加する必要がありました。読み込みエンドポイントができたため、ユーザー側ではフェイルオーバーしてインスタンスが変わることをあまり意識せずにAuroraを利用することができます。 ただしコネクションプーリングを利用している環境の場合は注意が必要です。Writerへ昇格するインスタンスのIPアドレスはそのままなのでvalidationQueryを設定していてもコネクションが切断されずに昇格したWriterへアクセスし続けてしまいます。コネクションを生成後一定期間で破棄するか、コネクションプーリングを利用しない方が良いかもしれません。