Full stack voting app
Last updated
Last updated
In this tutorial, we'll deploy a containerized full-stack application to Release. Our example voting application will allow users to vote for their favorite category and then view the results of the votes. We'll use multiple stacks and frameworks for our app to illustrate the breadth and flexibility of deployments in Release.
Our codebase will comprise three services that will be containerized and managed by a docker-compose
file. Additionally, we will use Postgres as a database and Redis as a message broker to offload some of the computational load to the worker
service. Our services will be:
Vote: A frontend and some server-side code that will push the vote made by a user to Redis. This will be built in Python, using the Flask framework.
Result: A frontend that uses a websocket API to poll data from its server-side implementation to provide real-time updates of votes. This will be a Node.js application that uses Express to serve an Angular frontend. The frontend will use Socket.IO to manage the websocket connection.
Worker: The background task processor that reads from Redis and creates entries in our Postgres database to represent the results of the votes. The worker will be implemented using Java.
Our completed voting application will look like this:
And the result application will look like this:
You can find the completed code for the project here.
Create a fork of the repository on your version-control hosting provider (GitHub, GitLab, or Bitbucket). Ensure that the provider you fork the repository to is the provider you have integrated with Release.
Once you have forked the repository, you can clone it to your development machine to get started.
You do not need to have the repository clone to deploy the application to Release, but it is useful to be able to work through the code and understand the codebase.
Our vote application is a small Flask web app that accepts POST requests from the index.html
file it bundles and serves statically via a GET request to /
.
If you take a look at the vote/app.py
file, you should see the code below:
This code has a few responsibilities:
When an HTTP request is received, we assign a voter ID to the caller, if one is not already present as a cookie on the request.
If the HTTP request is a POST request, we connect to Redis, and push a JSON payload containing voter data onto a Redis queue called votes
.
If the HTTP request is a GET request, we return the index.html
template file with a few parameters.
The most important parameters provided to our template are option_a
and option_b
.
These are the categories that a user can vote for. If the environment variables for OPTION_A
and OPTION_B
aren’t set, the default options will be "Cats" and "Dogs".
The worker application is purely a backend service, written in Java.
On startup, it establishes a connection to Redis and the PostgreSQL database.
As part of the connection to our database, the worker application also creates the necessary database tables.
It then watches the Redis queue called votes
for new items.
When a new item is found, it calls a method called updateVote
, which handles writing the result of a vote to the PostgreSQL database.
The result application, in a similar fashion to the vote application, serves an index.html
file via its /
route.
More interestingly, it exposes a websocket API using Socket.IO.
On startup, the result application establishes a connection to the PostgreSQL database.
Once a connection has been successfully established, it calls a getVotes()
function using the database client.
This function reads the vote results from the database (which were written to it via the worker) and publishes them to a Socket.IO-managed channel called scores
.
Our client-side code (anchored at result/views/app.js
) reads from the scores
channel and updates the result application’s frontend accordingly.
Each of the applications described above is containerized using a dockerfile
in their respective directories. We can use docker-compose
to coordinate and run our applications together, as well as run containerized versions of Redis and PostgreSQL.
Below is the complete docker-compose.yml
file required to build and run our applications.
To run our project locally, ensure you have Docker installed, and run the following command in the root of the project:
The vote application will be accessible via port 5001
on localhost
and the Result application will be available via port 5002
.
Once we’ve created the applications and set up our docker-compose.yaml
file, we’re ready to deploy our app to Release.
Ensure you’ve forked the repository before we get started. The instructions to deploy our example voting app are here.
After deployment, we can click on the hostname URL for the vote application to tinker with making votes. You can share this URL with other people to vote, too.
To view the results in real-time, you can navigate to the hostname URL for the result application.
Our default environment variables for OPTION_A
and OPTION_B
were set to “Cats” and “Dogs”, but perhaps we’d like our users to choose between “Python” and “JavaScript”.
To modify this, we can navigate back to our Application Dashboard and click on our ephemeral environment.
From there click on the Settings tab and click the Edit button for the Environment Variables section.
From here, we can modify the environment variables for the environment. We will add two variables, for OPTION_A
and OPTION_B
respectively. Then click Save As New Version and then Apply.
This will apply the latest configuration changes to our live environment and redeploy it.
Once our deployment is complete, we should be able to navigate back to the vote application and see our new voting categories in action!
In this tutorial, we’ve learned how to set up and deploy a non-trivial project with multiple services using Release. We’ve also looked at how to configure databases using Docker on Release. Additionally, we learned how to modify environment configurations and redeploy afterward.
A good next step might be to create an environment specifically for a development
branch of the project so that you can iterate on your project without impacting a production deployment.