Skip to content

rmoreira/cassandra-project

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Cassandra with Python (and Flask) in docker-composer:

This repository shows how to stand a three-node Cassandra cluster and Flask webserver to display the data from the database. The scripts in the init-scripts folder creates a keyspace called "sampledata", sets replication to 2, and initializes the database with some data, in this case, the index number and name of the months.

Video

if you want to follow along by wachting the video below:

demo video

Steps:

  • docker-composer build

    • This step will take a few minutes to build.
  • docker-compose up

    • This step will take a few seconds to come up because the Cassandra cluster has to initialize.

Verify that the webapp is up:

{
  "1": "January",
  "2": "February",
  "3": "March",
  "4": "April",
  "5": "May",
  "6": "June",
  "7": "July",
  "8": "August",
  "9": "September",
  "10": "October",
  "11": "November",
  "12": "December"
}

Verify that replication is working:

  • Open another terminal window.
  • See the running containers:
docker-compose ps

Output:

               Name                              Command               State           Ports
-----------------------------------------------------------------------------------------------------
cassandra-project_cassandra-node1_1   /opt/bitnami/scripts/cassa ...   Up      7000/tcp, 9042/tcp
cassandra-project_cassandra-node2_1   /opt/bitnami/scripts/cassa ...   Up      7000/tcp, 9042/tcp
cassandra-project_cassandra-node3_1   /opt/bitnami/scripts/cassa ...   Up      7000/tcp, 9042/tcp
cassandra-project_web-app_1           python /app/app.py               Up      0.0.0.0:5555->5555/tcp

Stop one container:

docker stop cassandra-project_cassandra-node3_1

Once it stops, go to to http://localhost:5555/ again. With replication set to 2, Cassandra's QUORUM allows the database to successfully return the request since all nodes have two thirds of the data.

Stop another container:

docker stop cassandra-project_cassandra-node3_1

And go to to http://localhost:5555/ again. The application will thrown an exception.

Increasing replication from 2 to 3:

By increasing replication from 2 to 3, reads will be faster, but writes will be slower. Press Control+C to stop the running cluster, and clean up the existing containers:

docker-compose rm -f

Then start the cluster again:

docker-compose up

Wait a few seconds and go to http://localhost:5555/ again. You should get a successful response.

Now try to stop 2 Cassandra containers:

docker stop cassandra-project_cassandra-node2_1
docker stop cassandra-project_cassandra-node3_1

Wait a few seconds and go to http://localhost:5555/ again. You should get a successful response because replication was set to 3, and all nodes have all the data.

References:

About

This repository shows how to stand a three-node Cassandra cluster and Flask webserver to display the data in the Cassandra database. The scripts in the init-scripts folder creates a keyspace called "sampledata", sets replication to 2, and initializes the database with some data, in this case, the index number and name of the months.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors