LoginSignup
11

More than 5 years have passed since last update.

Follower DatabasesでHerokuのPostgreSQLを冗長化

Posted at

前提条件

  • Herokuに課金利用していること
  • HerokuのPostgreSQL Add-onであるHeroku Postgresを利用していること

目的

Follower databaseを設定することで、スレーブのDBをでホットスタンバイにする
HerokuのPostgreで読み取り専用で本番DBに安全にアクセスできる

マスターのDBのプラランは、以下のようにスタンダードプラン以上だと使うことが出来る

Followers are only supported on Standard, Premium, and Enterprise tier database plans. Follow these steps to upgrade from a Hobby tier (dev or basic) plan to a production plan.

また副作用的に読み取り専用のDBができるので、問い合わせ調査などSelectするだけの場合はこちらを利用すると良いだろう

A database follower is a read-only copy of the master database that stays up-to-date with the master database data. As writes and other data modifications are committed in the master database, the changes are streamed, in real-time, to the follower databases.

heroku pg:info -a アプリ名
=== HEROKU_POSTGRESQL_PURPLE_URL (DATABASE_URL)
...
Fork/Follow: Available

同じプランである必要な無い。パフォーマンスに影響が無いのかはよくわからなかった

heroku addons:create heroku-postgresql:standard-0 --follow HEROKU_POSTGRESQL_CHARCOAL_URL -a アプリ名

Adding postgresql-rigid-xxxx to app_name... done
Setting HEROKU_POSTGRESQL_COLOR_URL and restarting app_name... done, vXXX
Follower will become available for read-only queries when up-to-date.
Use `heroku pg:wait` to track status
Use `heroku addons:docs heroku-postgresql` to view documentation.

まさかの時のフェイルオーバー

マスタのDBがStandardプランの場合はマニュアルでフェイルオーバー

プランがStandardの場合はマニュアルでフェイルオーバーする必要がある。
マニュアルで実行する手順は以下の通り
https://devcenter.heroku.com/articles/upgrading-heroku-postgres-databases#upgrade-with-follower-changeovers-2-prevent-new-database-updates

マスタのDBがPremiumプランの場合は自動でフェイルオーバー

Heroku Postgres premium plans have the HA feature with automated failover. R

ref: https://devcenter.heroku.com/articles/heroku-postgres-follower-databases

スレーブを読み取り専用のDBとして接続する手順

Herokuの環境変数から、DBの接続上を情報を取得しPostgresのクライアントで接続する

heroku config -s -a アプリ名| grep HEROKU_POSTGRESQL_COLOR_URL

MacだとPsequelが一番使いやすい気がする。MySQLのSequel Proと似た使い心地
http://www.psequel.com/

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11