Building a Distributed Voting App with Docker: A Hands-On Guide

Hritik RajHritik Raj
3 min read

Ever wondered how modern applications handle high traffic and multiple interacting services? Microservices, containerization, and message queues are key players. Today, I want to walk you through a simple, yet powerful, voting application that showcases these concepts in action, all orchestrated beautifully with Docker.

Why Docker for a Simple Voting App?

You might think, "It's just a voting app, why all the complexity?" And you'd be right if it were a single script. But imagine our simple poll going viral! We need:

  • Scalability: What if millions vote? We can't have one server doing everything.

  • Reliability: If one part crashes, the whole system shouldn't go down.

  • Maintainability: Different teams might work on different parts, using different technologies.

This is where Docker shines. By encapsulating each part of our application into its own isolated container, we gain the ability to easily deploy, scale, and manage them independently.

Unpacking Our Voting App's Architecture

Our voting application isn't just one big program; it's a team of specialized services working together. Here's a quick look at who does what:

  1. The Vote App (Python/Flask): This is what you see and interact with! It's a simple web interface where you cast your vote for your favorite option (say, "Cats" or "Dogs"). When you click "Vote," it doesn't immediately update a database.

  2. Redis (The Messenger): Instead, your vote is quickly sent to Redis, an incredibly fast, in-memory data store. Think of Redis here as our super-efficient message queue. The Vote App simply drops the vote into a "to-do" list in Redis and moves on.

  3. The Worker (.NET): This is the unsung hero working behind the scenes. Our .NET worker constantly watches Redis's "to-do" list. As soon as a vote appears, the worker grabs it, processes it, and then...

  4. PostgreSQL (The Record Keeper): ...the worker updates the official vote count in our persistent PostgreSQL database. This ensures that even if other services restart, our vote totals are safe and sound.

  5. The Result App (Node.js/React): Finally, we have the Result App. This is another web interface, but its job is to fetch the latest vote counts directly from PostgreSQL and display them to you in near real-time. It's how you see which option is winning!

Docker: The Conductor of Our Orchestra

So, how do all these pieces, written in different languages (Python, .NET, Node.js), talk to each other and run seamlessly? Docker containers and Docker Compose.

Each service (Vote App, Redis, Worker, PostgreSQL, Result App) lives in its own Docker container. This means each has its own isolated environment, dependencies, and configurations. Docker ensures they don't interfere with each other and are portable across different machines.

docker-compose.yml is the magic file that brings it all together. With a single command (docker compose up --build), Docker Compose reads this file and:

  • Builds the custom images for our Vote App, Worker, and Result App.

  • Pulls official images for Redis and PostgreSQL.

  • Starts all five containers.

  • Sets up a dedicated network so they can find and communicate with each other using their service names (e.g., the Worker knows to connect to redis and db).

  • Maps necessary ports so you can access the Vote and Result apps from your browser.

This entire setup, from database to user interface, can be brought to life in moments, thanks to Docker.

Try It Yourself!

Want to see this in action? Head over to the https://github.com/dockersamples/example-voting-app.git . You'll find all the Dockerfiles, application code, and the docker-compose.yml file. Just clone the repo, run docker compose up --build, and watch a distributed, containerized voting app come to life right on your machine!

This simple application is a fantastic starting point for understanding how microservices can be isolated, communicate via message queues, and achieve persistence, all powered by the robust ecosystem of Docker. Happy containerizing!

0
Subscribe to my newsletter

Read articles from Hritik Raj directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Hritik Raj
Hritik Raj