# Flashcards
A fault tolerant, load balanced flashcard learning application

## Overview
The goal of this project was to implement a simple distributed system. The multi-container Docker application consists of a simple web-application with a persistent database and a load balancer.  
Create and run the containers: **docker compose --compatibility up**  
Application running on: **http://localhost:7070/**

## Technologies
- Web-application: Node.js, Express, AJAX
- Persistent storage: PostgreSQL
- Load balancer: Caddy
- Container solution: Docker

## Architecture

![Architecture Diagram](./architectureDiagram.png "Architecture Diagram")

**User Interface:**
HTML, CSS, and client-side JavaScript are used to create a responsive and interactive user interface.
AJAX is utilized for asynchronous communication between the client and server, enabling dynamic updates without page reloads.  
**Web Server:**
Node.js acts as the web server, providing the runtime environment for executing server-side JavaScript code.
Express, a lightweight web application framework for Node.js, handles routing, middleware, and API endpoints.  
**Application Logic:**
The server-side JavaScript code written using Express handles business logic, data processing, and interactions with the database.
This logic includes handling flashcard creation, retrieval, and updates.  
**Database:**
PostgreSQL, a robust and feature-rich relational database management system, is used for data storage.
The database stores flashcard sets and any other necessary application data.  
**Load Balancer:**
Caddy is employed as a load balancer, distributing incoming requests across multiple instances of the web application.
The load balancer ensures scalability, high availability, and fault tolerance by evenly distributing the workload.    
**Docker Containers:**
Docker is employed to containerize each component of the application, including Node.js, Express, Caddy, and PostgreSQL.
Containerization ensures consistent deployment, simplifies scalability, and enables easy management across different environments.  
**Communication Flow:**
Users interact with the application through the UI, making requests to the web server.
The load balancer receives incoming requests and distributes them across multiple instances of the web application.
The web server, running on Node.js with Express, handles the requests, executes the appropriate logic, and communicates with the database.
The application logic interacts with the PostgreSQL database for data retrieval and storage.
Responses from the server are sent back to the client, providing the requested data or performing the necessary actions.

## Application
The web application is a interactive platform designed for creating and studying flashcards. On the main page, they can choose between learning and creating flashcards. On the creation page, they can create new flashcards by specifying the content of the cards in the form of question and answer.
On the learning page, they can go through the flashcards, one by one. They can mark the learn success of the current card
All flashcards are stored in a persistent database, containing the question, answer and further information which can be used for further expansion of the application.  
The application is still a first draft because the focus was on the distributed system and has therefore room for improvement, like user login, multiple card sets, learn procedure and so on.

## Load balancer
The load balancer is responsible to distribute the traffic to the applications. If on host goes down it forwards the traffic to the other available upstream host.
