Test New Database System for Clustering Database on Containers

## Why New Database is Considered
This post is about CockroachDB https://github.com/cockroachdb. I just a little wondered why they named this Database like that. So I googled it then the reason returned. (Thanks Google as always!)

> I was annoyed. Why wasn't there a scalable, survivable, consistent database with transactions? I was even willing to drop transactions as a requirement – a terrible sacrifice. The frustration led me to write a manifesto. What would the "right" database look like?
> 
>I imagined it would be composed of symmetric nodes, require no external dependencies, spread itself naturally across availability zones for survival. Each node would autonomously replicate and repair data. These were the capabilities that led me to the name "cockroach", because they'll colonize the available resources and are nearly impossible to kill.
>https://forum.cockroachlabs.com/t/why-did-you-name-it-cockroachdb/38/2

As he explains the origin of the name, the features of CockroachDB "a scalable, survivable, consistent database with transactions" is the reason which led me to the database.
On its top page, it says:

> CockroachDB is a cloud-native SQL database for building global, scalable cloud services that survive disasters.
> https://www.cockroachlabs.com/

Currently PostgreSQL on a container is used in the system and data gathered from infrastructures are stored in the Postgres DB. We need to consinder the increase of the data volume in the future. However, Postgres architecture seems not to be suitable for scalable architecture. As a result, Postgres DB could be the bottleneck of the system performance.    

## Try New Database
Because of that reason, I need to research another database for the future scenario. And CockroachDB was found as a result. 
As I am now developing the new system based on the architecure with Postgres DB, I don't want to change the code as long as possible. Fortunately, according to the link below, CockroachDB supports PostgreSQL client drivers for Java. 

> CockroachDB supports the PostgreSQL wire protocol, so you can use any available PostgreSQL client drivers. We've tested it from the following languages:
> Go
Python
Ruby
Java
JavaScript (node.js)
C++/C
Clojure
PHP
Rust
https://www.cockroachlabs.com/docs/stable/frequently-asked-questions.html#what-languages-can-i-use-to-work-with-cockroachdb

Let's try CockroachDB on the current system. 
Note that I tested the compatibility between Postgres and CockroachDB but not how CockroachDB's multi nodes clustering artchetecure works.

##### 1. Start CockroachDB on a Container
Pull the image https://hub.docker.com/r/cockroachdb/cockroach/.

```
root@ubuntu:~# docker pull cockroachdb/cockroach:v1.0.5 
```

##### 2. Set up Single Node CockroachDB Cluster
Create a bridge newtork for containers.

```
root@ubuntu:~# docker network create -d bridge roachnet
```

Then, start the first node.

```
root@ubuntu:~# docker run -d \
--name=roach1 \
--hostname=roach1 \
--net=roachnet \
-p 26257:26257 -p 8080:8080  \
-v "${PWD}/cockroach-data/roach1:/cockroach/cockroach-data"  \
cockroachdb/cockroach:v1.0.5 start --insecure
```

##### 3. Set up SQL Database on CockroachDB
Create a new database and user, then grant privilege.
Note that the reason the new user is named "postgres" is I just wanted to reduce the amount of code modification.

```
root@ubuntu:~# docker exec -it roach1 ./cockroach sql --insecure
# Welcome to the cockroach SQL interface.
# All statements must be terminated by a semicolon.
# To exit: CTRL + D.
root@:26257/> CREATE USER postgres WITH PASSWORD 'password';
CREATE USER
root@:26257/> CREATE DATABASE test_db;
CREATE DATABASE
root@:26257/> grant all on database test_db to postgres;
GRANT
```

##### 4. Run an existing Jar Application
Run an existing Jar Application with just small code modification such as database endpoint. That's all I needed to change.

Confirm new data is inserted into the database after the execution.

```
root@:26257/> set database=test_db;
SET
root@:26257/test_db> show tables ;
+---------+
|  Table  |
+---------+
| test_table |
+---------+
(1 row)
root@:26257/test_db>  SELECT COUNT(*) FROM test_table;
 count 
-------
   100
(1 row)
```

As the result of this test, I learned that CockrochDB can be implemented onto the current system with small code modification. I will consider to use this database in the future for scalable database architecture.