Rollback, Followers, and Forks - Learning Heroku Postgres (2015)

Learning Heroku Postgres (2015)

Chapter 6. Rollback, Followers, and Forks

In this chapter, you will learn important concepts related to security, stability, and experiments in the PostgreSQL database on the Heroku platform. We will discuss topics related to rollback, follower databases, and fork databases. These functionalities offered by Heroku Postgres are very useful to ensure the secure storage of your data and the scalability of your Postgres database.

Basically, rollback is related to recovering a prior version of your database and restoring the data and structure of your database. Rollback is a useful feature when your database is corrupted.

A follower database is an essential feature when your concern is scalability and security of your data. A follower database is a replica of your main database in read-only mode; this replica is synchronized automatically on the Heroku infrastructure. You can have as many follower databases as necessary for your application.

Finally, the fork database is useful to test and simulate your application with different versions of the database. The fork database is a copy of the data and the structure of the main database at a specific time.

Throughout this chapter, you will learn how to use each of these features and how to benefit from their functionalities.

In this chapter, we will be cover:

· Heroku Postgres rollback

· Heroku Postgres follower databases

· Heroku Postgres forking databases

Heroku Postgres rollback

The most common use case of rollback is when your database breaks down with a problem after a new push to production. Through the rollback feature, you can roll your database back to before the issue occurred. Another use case is when data loss occurs accidentally in the database. This feature allows database data to be recovered for any given time period.

In Chapter 4, PG Backups you learned about PG Backups. It is a great solution for ensuring the security and integrity of your database. Sometimes, people get confused about PG Backups and Heroku Postgres rollback; in fact, they are complementary solutions. Through Heroku Postgres rollback, you have another way to recover your data and maintain the integrity of your database; you can specify the exact period to the rollback.

Checking the rollback feature

The first step for performing rollback is to check whether this feature is available in your plan. Currently, the rollback functionality is available only in standard, premium, and enterprise plans.

You can check whether the rollback is enabled in two different ways. The first one is through the Heroku client with the heroku pg:info command:

$ heroku pg:info --app your-app-name

=== HEROKU_POSTGRESQL_CHARCOAL_URL (DATABASE_URL)

...

Rollback: earliest from 2014-10-04 22:56 UTC

...

The preceding command displays the rollback information; this tells you whether the feature is available for your database. If the line displays a date, as the one in the preceding command, this means that the feature is available. If the line displays "Unsupported", this means that it is unavailable, as shown here:

=== HEROKU_POSTGRESQL_ORANGE_URL (DATABASE_URL)

...

Rollback: Unsupported

...

The second way to check whether rollback is active is through the Heroku Dashboard. Just visit https://postgres.heroku.com, select your database, and take a look at the Statistics section to check whether the rollback feature is enabled, as shown in the following image:

Checking the rollback feature

The rollback feature is enabled

Available period

The period available for rollback varies according to your database plan. It is important because it allows you to perform the rollback within the available range. This table shows the periods for each plan:

Heroku Postgres tier

Rollback

Hobby

No

Standard

1 hour

Premium

1 week

Enterprise

1 month

Creating a rollback database

The rollback process consists of the creation of another database with the desired rollback period. Thus, when the rollback is done, it doesn't affect your main database. If you want to change the main database, you need to promote the rollback database. You will see this a little later in the chapter.

To perform a rollback, it is necessary to use the Heroku client. The command consists of the addition of a new database and the --rollback flag with the database color name for which the rollback should be done, and you must use the --to flag to specify the period of the rollback.

You can specify the time period in two different ways, either using a 2014-10-05 15:02:00+00 timestamp or using the time with the time zone as 2014-10-05 15:02 US/Pacific.

Thus, the basic command to rollback is as follows:

$ heroku addons:add heroku-postgresql:standard-0 --rollback database_color_name --to '2014-10-05 15:02:00+00' --app your-app-name

Adding heroku-postgresql:standard-0 on your-app-name... done, v240 ($50/mo)

Attached as HEROKU_POSTGRESQL_RED_URL

Database will become available after it completes rolling back to 2014-10-05 15:02:00 +0000 (00:59:43 ago)

Use `heroku pg:wait` to track status.

Use `heroku addons:docs heroku-postgresql` to view documentation.

The time to create the rollback database can vary according to the size of your database. You can use the heroku pg:wait command to see the progress:

$ heroku pg:wait --app your-app-name

Waiting for database HEROKU_POSTGRESQL_RED_URL... \ preparing (5% completed)

There is another way to perform the rollback. It is through the --by flag. You can specify a time interval, and it should be of the X days X hours X minutes format. Here is an example:

$ heroku addons:add heroku-postgresql:standard-0 --rollback database_color_name --by '2 days 5 hours 10 minutes' --app your-app-name

Tip

You cannot specify both the --by and --to options for rollback. You should specify one or the other.

Promote a rollback database

In some cases, you may want to promote the rollback database to the primary database. You must use the heroku pg:promote command with the database color URL to be promoted:

$ heroku pg:promote HEROKU_POSTGRESQL_[COLOR]_URL --app your-app-name

Deprovisioning a rollback database

At any time, it's possible to remove the rollback database through the heroku addons:remove command by passing as parameter the database color URL that you want to remove:

$ heroku addons:remove HEROKU_POSTGRESQL_[COLOR]_URL --app your-app-name

Heroku Postgres follower databases

The follower database is a feature that helps the scalability of your Heroku applications.

Through the follower database, Heroku provides a way to increase the efficiency of reading your database data by creating a master-slave structure. Each slave is one follower in read-only mode, and the data is updated from master to slave in real time.

The most common use of a follower database is when you want to increase the performance to access your data; for example, imagine that you have a web application with a huge number of users. If all users access the same database, you will probably have performance issues. A good solution to this is to create copies of your main database, and when each user needs any data, the system redirects the request for a copy of the database. This way, you have a database distributed system. Through Heroku Postgres follower databases, you can add many follower databases for your application.

Creating and managing follower databases

The follower feature is enabled for the plans in the standard, premium, and enterprise tiers. You can check whether your database is enabled to use followers through the heroku pg:info command:

$ heroku pg:info --app your-app-name

=== HEROKU_POSTGRESQL_CHARCOAL_URL (DATABASE_URL)

Fork/Follow: Available

A follower database must be created from a database that allows us to read and write. Usually, the follower database is created from DATABASE_URL. You cannot create followers in cascade from another follower database.

The creation of a follower database consists of adding a new database through Heroku add-ons and the use of the --follow flag and the database URL from which the follower must be created:

$ heroku addons:add heroku-postgresql:standard-2 --follow HEROKU_POSTGRESQL_[COLOR]_URL --app your-app-name

Adding heroku-postgresql:standard-2 on your-app-name... done, v245 ($200/mo)

Attached as HEROKU_POSTGRESQL_BLACK_URL

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.

In the preceding command, heroku-postgresql:standard-2 informs us about the type of plan that will be used for the follower database. You can select the plan that best suits you, and through https://addons.heroku.com/heroku-postgresql, you can see all the available plans.

Tip

It's recommended that you either choose a higher plan or one that is able to create a new follower database.

Through the heroku pg:wait command, you can check the creation process of a new follower database:

$ heroku pg:wait --app your-app-name

Waiting for database HEROKU_POSTGRESQL_BLACK_URL... | preparing (5% completed)

The creation time of a new follower database depends on the size of your database and the use of the database at that time.

Unfollow the main database

You can stop following the main database at anytime. This action does not remove the follower database; it just stops the synchronization. In this case, after unfollowing the main database, you can read and write data in the follower database.

The command expects to be informed of the follower database URL. Here is an example:

$ heroku pg:unfollow HEROKU_POSTGRESQL_[COLOR]_URL --app your-app-name

! HEROKU_POSTGRESQL_[COLOR]_URL will become writable and no longer

! follow HEROKU_POSTGRESQL_[COLOR]. This cannot be undone.

! WARNING: Destructive Action

! This command will affect the app: your-app-name

! To proceed, type "your-app-name" or re-run this command with --confirm your-app-name

> your-app-name

Unfollowing HEROKU_POSTGRESQL_[COLOR]_URL... done

Tip

An important detail when you perform the unfollow action is that you still continue to be charged by the follower database. This is because the database has not been removed; it still remains active to read and write data.

If you want to delete the database, it is necessary to use the add-on remove command. Here is an example:

$ heroku addons:remove HEROKU_POSTGRESQL_[COLOR]_URL --app your-app-name

! WARNING: Destructive Action

! This command will affect the app: your-app-name

! To proceed, type "your-app-name" or re-run this command with --confirm your-app-name

> your-app-name

Removing HEROKU_POSTGRESQL_[COLOR] on your-app-name... done, v246 ($200/mo)

Upgrade the database plan with follower

A follower database is also an interesting feature to upgrade the database plan with a small amount of downtime.

This operation consists of adding a new follower database with the desired plan and then promoting it to the main database of your application. The advantage of this solution is that the downtime is very low, around 1 minute.

The first step is to create a new follower with the new plan to upgrade:

$ heroku addons:add heroku-postgresql:standard-2 --follow DATABASE_URL --app your-app-name

Adding heroku-postgresql:standard-2 on your-app-name... done, v247 ($200/mo)

Attached as HEROKU_POSTGRESQL_GOLD_URL

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.

The second step is about preventing new updates to the main database so that you can be sure that both databases will be synchronized. This action consists of putting your application in maintenance mode and disabling its processes:

$ heroku maintenance:on --app your-app-name

Enabling maintenance mode for your-app-name... done

Now, you should also disable your works to prevent any update of data from them:

$ heroku ps:scale worker=0 --app your-app-name

Scaling dynos... done, now running worker at 0:2X.

After that, you can check whether the follower database is already built through the heroku pg:wait command:

$ heroku pg:wait --app your-app-name

If the command doesn't return anything, it means that the follower database has been created and synchronized with your main database.

Next, you need to promote the follower database to the main database. For this, you first need to perform the unfollow action and after that, promote the database. Through the heroku pg:info command, you can see the information on your main database and follower database:

$ heroku pg:info --app your-app-name

=== HEROKU_POSTGRESQL_CHARCOAL_URL (DATABASE_URL)

Plan: Standard Yanari

Status: Available

...

Followers: HEROKU_POSTGRESQL_GOLD

...

=== HEROKU_POSTGRESQL_GOLD_URL

Plan: Standard 2

Status: Available

...

Following: HEROKU_POSTGRESQL_CHARCOAL

Behind By: 0 commits

...

Tip

The Behind By field informs us that the follower database is outdated from the main database by zero commits.

Now, you need to unfollow the main database using the following command:

$ heroku pg:unfollow HEROKU_POSTGRESQL_GOLD_URL --app your-app-name

The following command promotes it as the main database:

$ heroku pg:promote HEROKU_POSTGRESQL_GOLD_URL --app your-app-name

Promoting HEROKU_POSTGRESQL_GOLD_URL to DATABASE_URL... done

Finally, activate your works, remove your application from maintenance mode, and delete the old database:

$ heroku ps:scale worker=1 --app your-app-name

$ heroku maintenance:off --app your-app-name

$ heroku addons:remove HEROKU_POSTGRESQL_CHARCOAL_URL --app your-app-name

High availability with followers

By default, the follower databases are created in a geographical area that is different from that of the main database. In addition, all databases in the premium and enterprise plans have high availability enabled. If your database fails, it is automatically replaced by another main database with the same plan. If there are follower databases, they are destroyed and recreated again after the failure event.

Heroku Postgres forking databases

A fork of your database is the structure and data copied at a certain point in time. This consists of creating a new database from another. The fork database is not synchronized after your creation.

The most common use is for analysis and experiments with a copy of the database. You can also use the fork database to experiment with different versions of database plans. This allows you to test the behavior of the database.

A fork database is a copy of another database, that allows us to read and write.

Forking your database

The creation of a fork database works in a manner similar to the creation of a follower database. You must add a new database in the desired plan using the --fork flag; this flag advises that there will be a fork from another database.

As it happens with the followers, you don't need to create a fork in the same plan of the database. You can choose any other plan, and the forks allow you to, experiment with different database plans. The forks represent a useful tool to optimize the configuration of your application during these experiments.

The following command illustrates the creation of a new fork database:

$ heroku addons:add heroku-postgresql:standard-0 --fork DATABASE_URL --app your-app-name

Adding heroku-postgresql:standard-0 on your-app-name... done, v249 ($50/mo)

Attached as HEROKU_POSTGRESQL_ONYX_URL

Database will become available after it completes forking

Use `heroku pg:wait` to track status.

Use `heroku addons:docs heroku-postgresql` to view documentation.

Through the heroku pg:wait command, you can see the creation of the new database:

$ heroku pg:wait --app your-app-name

Waiting for database HEROKU_POSTGRESQL_ONYX_URL... / preparing (5% completed)

Forking databases with the fast option

There is another option to create a fork database, and it is through the use of the --fast flag. This mode of creation is faster because the fork is made 30 hours ago; this is the maximum period. It could be a useful option if your database doesn't have big changes over this period. This option is presented here as an alternative if you need to create a fork quickly:

$ heroku addons:add heroku-postgresql:standard-2 --fork DATABASE_URL --fast --app your-app-name

Adding heroku-postgresql:standard-2 on your-app-name... done, v250 ($200/mo)

Attached as HEROKU_POSTGRESQL_ROSE_URL

Fork will contain data from October 14, 2014 3:10PM UTC (about 22 hours ago)

To create a fork with up-to-date data, exclude the `--fast` flag.

Database will become available after it completes forking

Use `heroku pg:wait` to track status.

Use `heroku addons:docs heroku-postgresql` to view documentation.

View your fork databases

You can use the heroku pg:info command to view information on your fork database:

$ heroku pg:info --app your-app-name

=== HEROKU_POSTGRESQL_ROSE_URL

Plan: Standard 2

Status: Available

Data Size: 56.0 MB

Tables: 38

PG Version: 9.3.5

Connections: 1/400

Fork/Follow: Available

Rollback: earliest from 2014-10-15 13:15 UTC

Created: 2014-10-15 13:10 UTC

Forked From: HEROKU_POSTGRESQL_CHARCOAL

Maintenance: not required

Deprovisioning a fork database

You can remove a fork database at any time. This action deletes the database via the heroku addons:remove command using the database color URL as a parameter:

$ heroku addons:remove HEROKU_POSTGRESQL_[COLOR]_URL --app your-app-name

! WARNING: Destructive Action

! This command will affect the app: rsvp-production

! To proceed, type "your-app-name" or re-run this command with --confirm your-app-name

> your-app-name

Removing HEROKU_POSTGRESQL_[COLOR] on your-app-name... done, v251 ($200/mo)

Self-test questions

Answer true or false.

1. Is rollback a feature that allows you to create a database in the previous state in time?

2. Is the --by flag mandatory to create a rollback database?

3. After creating a rollback database, is it necessary to promote a main database to active it in your application?

4. Is a follower database an important resource for the horizontal growth of your database as a master and slave?

5. Are follower databases synchronized automatically?

6. Can follower databases be created with different database plans?

7. Are fork databases an interesting resource for experiments with a copy of the main database?

8. Do fork databases allow us to create a new follower and fork database with your content?

9. Is a fork database a database that is constantly synchronized with the main database?

10. Is heroku pg:wait a useful command to monitor the creation of new databases?

Summary

In this chapter, you learned important concepts and tools that you will probably use as your applications grow.

First, you saw the Heroku Postgres rollback. This is very useful when data loss occurs accidentally. With one command, you can create a new database with the old state of your main database.

You learned about working with follower databases. The followers probably will be one of the features that you will use as your application grows up. Through followers, Heroku created a master-slave solution for database reading.

Finally, you learned about fork databases. A fork is a copy of the database, and it isn't synchronized with the main database. You also learned that forks are interesting to experiment and test with different database plans.

In the next chapter, you will learn how logs work and how to extract information that helps you identify problems and understand the most common errors.