Practice Exercise 1:
Objectives
Run a Flask app that uses Distributed Databases
Scenario
You need a Flask app on web1 that can run using Distributed Databases (db1 & db2)
<name>-a1t-inf-ds-web1.acad.opswerks.net<name>-a1t-inf-ds-db1.acad.opswerks.net<name>-a1t-inf-ds-db2.acad.opswerks.net
Tasks
- On the
web1instance, make a directory namedch8and navigate to it - Create a virtual environment
python -m venv chapter8 - Activate your virtual environment
source chapter8/bin/activate - Download the file below and save it as
requirements.txtin the current directorya1t-inf-ds-files.acad.opswerks.net/requirements.txt - Run the command to install the dependencies for the modified Flask app
pip install -r requirements.txt - Download the file below and save it as
flask_v2.pya1t-inf-ds-files.acad.opswerks.net/flask_v2.py - Create a folder named
templatesin your current directory and save the file there astable.html:a1t-inf-ds-files.acad.opswerks.net/table.html - Set the environment variable
DB_USERexport DB_USER=<NAME> -
On the
db1instance run the command:Your username is the same as your Linux username and the password ismysql -u <USERNAME> -p -e "SHOW MASTER STATUS"Acad_student-<USERNAME>. Take note of thePositionfrom the output -
On the
db2instance run the command:mysql -u <USERNAME> -p -e "CHANGE MASTER TO MASTER_HOST='<DB1_HOSTNAME>', MASTER_USER='<YOUR_USERNAME>', MASTER_PASSWORD='<YOUR_PASSWORD>', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=<LOG_POSITION>; START SLAVE;"
Fill in the command with the proper values, the MASTER_LOG_POSITION is taken from the output of SHOW MASTER STATUS
- Run your app
python flask_v2.py
- Visit your application at
<YOUR_NAME>-a1t-inf-ds-lb.acad.opswerks.net, your application should display a table - Stop the
mysqldservice on thedb1instance and revisit your application's website, it should be still running despitedb1being down.
Conclusion
One of the most significant benefits of distributed databases is fault tolerance. To illustrate this, we deliberately stopped the mysqld service on the db1 instance. Despite this disruption to a part of the database infrastructure, our application remained functional.
In the world of distributed systems and databases, understanding fault tolerance, scalability, and consistency is crucial. This activity showcased the power of distributed databases in maintaining service availability, even in challenging situations.