Skip to content

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 web1 instance, make a directory named ch8 and 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.txt in the current directory
    a1t-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.py
    a1t-inf-ds-files.acad.opswerks.net/flask_v2.py
    
  • Create a folder named templates in your current directory and save the file there as table.html:
    a1t-inf-ds-files.acad.opswerks.net/table.html
    
  • Set the environment variable DB_USER
    export DB_USER=<NAME>
    
  • On the db1 instance run the command:

    mysql -u <USERNAME> -p -e "SHOW MASTER STATUS"
    
    Your username is the same as your Linux username and the password is Acad_student-<USERNAME>. Take note of the Position from the output

  • On the db2 instance 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 mysqld service on the db1 instance and revisit your application's website, it should be still running despite db1 being 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.