Skip to main content

Leaders and Followers

Implement Data Replication in Go - Part I

Recap

We have already discussed one of the Replication techniques i.e. Single Leader Replication where one of the nodes acts as a Leader and the rest of them as Followers.

In this chapter, we will try to implement our own Data Replication technique using Single Leader Replication with Synchronous flow of data to the Followers

Prerequisites

We will use Docker to run containers of PostgreSQL DB docker image. We will run two different containers of postgres on two different ports.

You need to have the below applications installed on your machine before moving on.

  • Docker (Docker Desktop)
  • Go
  • Postman or any other tool for testing your APIs
  • Any Code Editor (VS Code or Goland).
  • Terminal or Powershell

Setting Up PostgreSQL Containers

As a first step, please make sure that your Docker Desktop is running. Now open a terminal and fire the below command. This will download the latest image of postgres.

docker pull postgres

You will get the below output once the image is downloaded.

Using default tag: latest
latest: Pulling from library/postgres
2c6d21737d83: Pull complete 
cd877d542a82: Pull complete 
ef4dec12b244: Pull complete 
b4793e74422c: Pull complete 
12f6bc37ee29: Pull complete 
fd9c37dc95ed: Pull complete 
413b8cf9d665: Pull complete 
1c95e694bd99: Pull complete 
6b899778c1e5: Pull complete 
2627f21dbdce: Pull complete 
de237cbda09b: Pull complete 
f6db1b4908f2: Pull complete 
f5f0f93ae856: Pull complete 
259820b698b9: Pull complete 
Digest: sha256:ff37e66b0a03594086c3734d73e750f13480ca9bf64b53fafea18be4d5afb9ad
Status: Downloaded newer image for postgres:latest
docker.io/library/postgres:latest

Next, if you want to see all the docker images present in your machine, you can run the below command and look for postgres.

docker image ls

You will be able to see one of the docker images of postgres on your machine.

REPOSITORY             TAG                    IMAGE ID       CREATED         SIZE
postgres               latest                 b9d4095a181a   5 days ago      455MB

Once we have the docker image of postgres on our machine, we will run two different containers one as masterdb and another one as replicadb.

First, let's create masterdb container and map it to our local port 5445.

docker run -d --name masterdb -p 5445:5432 -e POSTGRES_PASSWORD=Master123 postgres

You will see that the masterdb is created and running on docker.

877ad2dea06b68b44cd4f8b499c447ae4ddfc91ba85aa54bc4670918576b69ee

Now, let's create replicadb container and map it to our local port 5446.

docker run -d --name replicadb -p 5446:5432 -e POSTGRES_PASSWORD=Replica123 postgres

You will see that the replicadb is created and running on docker.

11fe4a72d319cfde3a9cde4d3c3b07779b6c2aeb518affdac247677e1f02b9d5

Now, open two different windows of Terminal/PowerShell to access PostgreSQL CLI and create a database and table inside both containers.

In one of the terminals, run the below command to exec into masterdb container.

docker exec -it masterdb bash

After running the above command you will be now inside masterdb container running in your docker.

root@877ad2dea06b:/#

Similarly, in another terminal, you need to exec into replicadb container by running the below command.

docker exec -it replicadb bash

After running the above command you will be now inside replicadb container running in your docker.

root@11fe4a72d319:/#

Now, we will create a database and a table that would have the same data in both databases initially.

Now onwards, run each command in both terminals.

Firstly, fire the below command in both terminals.

root@11fe4a72d319:/# psql -h localhost -U postgres

After running the above command, you have launched PostgreSQL CLI and you will see something like this:

postgres=#

Create a database employee by running the below command in both terminals.

postgres=# CREATE DATABASE employee;

If you find the below output, then the database is created successfully.

CREATE DATABASE

Now, connect with the employee database by running the below command. It's the same as we run USE DATABASE in MySQL.

postgres=# \c employee;

You will see the below output. Make sure you are running each command in both terminals.

You are now connected to database "employee" as user "postgres".

Now, create a table emp.

employee=# CREATE TABLE emp(id INT PRIMARY KEY NOT NULL, name TEXT NOT NULL, salary INT NOT NULL);

If you find the below output, then the table is created successfully.

CREATE TABLE

Now add two dummy records.

employee=# INSERT INTO emp VALUES (101, 'John', 285000), (102, 'Sam', 255000);

If you find the below output, then both records are added successfully.

INSERT 0 2

Now, you can see the data by running the below command.

employee=# SELECT * FROM emp;

Output

 id  | name | salary 
-----+------+--------
 101 | John | 285000
 102 | Sam  | 255000
(2 rows)

Hurray! We have successfully set up two containers running on different ports on our local machine. In the next chapter, we will see the Go implementation.