← All Articles

Automatic database replication for MySQL, PostgerSQL, MongoDB and Redis

Kasia HoffmanKasia Hoffman
Dec 23rd 14Updated Jul 26th 17

We are happy to talk to you about how Cloud 66 Database Replication is now even more powerful than before.

Database Replication is designed to help you to scale your database and improve your service availability simply by setting up slave/master replication.

You can set up database replication in a single stack or between two separate stacks.

Why do I need replication?

Setting up replication for a single stack can improve your database performance as it allows you to split your read and write operations between two different databases. In this scenario the master is used for read and write operations while the slave is read only. High read/low write apps benefit most from this setup.

However replicating databases between two stacks (or Cross-stack replications) allows you to achieve minimal downtime when you need to move your application from the first stack to the second one. Cross-Stack replication stacks can reside in different data centers, resulting in a failover setup for your application, improving availability and resilience. Move your stacks with minimal downtime, but also you can keep the failover stack in different region.

How does it work?

To get started, first you need to make sure you have Managed backups available on your stack. Here you can find more information about managed backups. Once your managed backups are configured, you can setup your database replication with ease.

Let's see what happens behind the scenes:

  1. We take a full backup of the master database server in your source stack
    Single stack : we create a secondary database server in your cloud and restore your backup on it.
    Between two stacks : we restore your backup on the secondary database server.
  2. The secondary database is configured to be a slave of the source database
  3. The source database is configured to be a master of the secondary database
  4. The relevant environment variables are updated for use in your code and scripts
  5. Your replication will be monitored by Cloud 66. You will get alerts when there is an issue with the replication.

Database replication on the single stack

Click on your database group in your stack (MySQL, PostgerSQL, MongoDB or Redis) and then click on the green button scale up (you need to have Managed Backup setup to see this button).

This would begin the replication process.

And all done!

Wasp has become a master and Badger is now a slave.

Database replication between two stacks

Note: This feature is available for MySQL, PostgreSQL and Redis.

Imagine you have two stacks. One in Netherlands (let's call it Elephant) and second in the USA (let's call it Mouse). Now you decided to replicated the database from the stack in Netherlands to the stack in the USA. This is what you do:

Note that the source stack (Elephant) would need to have managed backups.

Click on your database server under the database group in your secondary (Mouse) stack:


Next, on the right hand side, click on the Configure Data Replication button.

When you click on it an orange box would appear. Select the source stack Elephant in the drop box and click OK.

Now the process of databse replication between the stacks begins.

A few minutes later all is done. The database on Mouse is a slave of the database on the Elephant stack and is read only.

The database on the Elephant stack is read/write.

Promoting DB slave to DB master using the Toolbelt

Let's take it one step further: You have replicated your database between the two stacks and now want to switch your slave database to a master (for example as part of disaster recorvery).

How can we do that?

The Manual way

  1. Manually change the configuration files.
  2. Point your environment variables.
  3. Restore the database.

Using Cloud 66 Toolbelt

Simply promote database on the Mouse server (in the USA) and use it as a new master.

How to use it?

To promot a slave database to become a standalone database server you can use the Toolbelt (cx). Here is how it works:

Assuming your primary stack (Elephant) has database replication configured we can use the Toolbelt like this:

$ cx databases promote-slave [-s <stack>] [--db-type <database type>] <slave server name>

To change the database on the Mouse stack use the following command.

$ cx databases promote-slave -s Mouse shark

If you have more than one database with replication, you can specify which one you want to promot:

$ cx databases promote-slave -s Mouse --db-type postgresql shark

The valid db-types are mysql, postgresql and redis.

Resyncing replicated databases

Sometimes it is possible for replicatied databases to go out of sync. You can manually sync them with the following command:

$ cx databases resync-slave [-s <stack>] [--db-type <database type>] <slave server name>

For example:

$ cx databases resync-slave -s Mouse --db-type postgresql my-slave-server-name

Toolbelt allows you to automate the process and help you save time.

Tip: We suggest to perform this action during a non-busy time as it could result in application downtime. You can place your stack in maintenance mode to ensure a better experience for your visitors.

Check out our help page with the explanation on Toolbelt and learn more about Database Replication.

Try Cloud 66 for Free, No credit card required