Skip to content

A distributed Fibonacci calculator built with microservice architecture, featuring React frontend, ExpressJS API, background worker, Redis pub/sub, PostgreSQL, Docker containerization, and automated CI/CD deployment to Azure Container Instances

Notifications You must be signed in to change notification settings

Dimuthu7/fibonacci-calculator-microservices

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Fibonacci Calculator - Distributed Application

A full-stack distributed application that calculates Fibonacci numbers using a microservices architecture with Docker containerization and automated CI/CD deployment to Azure Container Instances.

🎯 Basic Functionality

This application allows users to:

  1. Submit Fibonacci Index: Users enter a number (index) between 0-40 to calculate the corresponding Fibonacci number
  2. Real-time Calculation: The system processes the request asynchronously using a worker pattern
  3. View Results: Users can see:
    • All previously submitted indices (stored in PostgreSQL)
    • Calculated Fibonacci values (stored in Redis)
  4. Automatic Updates: The UI polls for new results every 2 seconds and refreshes the list of indices every 5 seconds

How It Works

  1. User submits an index through the React frontend
  2. Server receives the request and:
    • Stores the index in PostgreSQL (permanent storage)
    • Places a placeholder value in Redis ("Nothing yet!")
    • Publishes the index to a Redis channel (insert)
  3. Worker process:
    • Subscribes to the Redis insert channel
    • Receives the index and calculates the Fibonacci number
    • Updates Redis with the calculated value
  4. Frontend polls Redis and displays the result when ready

🔄 Why Worker Pattern?

1. Separation of Concerns

  • Server: Handles HTTP requests, data validation, and user interactions
  • Worker: Handles CPU-intensive calculations without blocking the server

2. Scalability

  • The server can handle multiple requests simultaneously
  • Workers can be scaled independently based on calculation load (using Kubernetes)
  • Multiple worker instances can process calculations in parallel

3. Performance

  • Non-blocking: Server responds immediately to users without waiting for calculations
  • Asynchronous Processing: Calculations happen in the background
  • Better Resource Utilization: CPU-intensive tasks don't block I/O operations

4. Resilience

  • If a worker crashes, the server continues to accept requests
  • Failed calculations can be retried by other workers
  • Server remains responsive even during heavy computational loads

💻 Local Development

Prerequisites

  • Docker and Docker Compose installed
  • Node.js 20+ (for local development without Docker)

Running with Docker Compose

# Start all services
docker-compose -f docker-compose-dev.yml up

# Or in detached mode
docker-compose -f docker-compose-dev.yml up -d

# View logs
docker-compose -f docker-compose-dev.yml logs -f

# Stop all services
docker-compose -f docker-compose-dev.yml down

Accessing the Application

📝 Environment Variables

Server

  • REDIS_HOST: Redis server hostname
  • REDIS_PORT: Redis server port (default: 6379)
  • PGUSER: PostgreSQL username
  • PGHOST: PostgreSQL hostname
  • PGDATABASE: PostgreSQL database name
  • PGPASSWORD: PostgreSQL password
  • PGPORT: PostgreSQL port (default: 5432)
  • PORT: Server port (default: 5000)

Worker

  • REDIS_HOST: Redis server hostname
  • REDIS_PORT: Redis server port (default: 6379)

📚 Technology Stack

  • Frontend: React 19, TypeScript, Vite, TailwindCSS, TanStack Query
  • Backend: Express.js, TypeScript, Node.js
  • Worker: TypeScript, Node.js, ioredis
  • Database: PostgreSQL 16
  • Cache/Message Broker: Redis 7
  • Reverse Proxy: Nginx 1.27
  • Containerization: Docker

🤝 Contributing

This is a learning project. Feel free to fork and experiment!


Note: This application is designed for educational purposes to demonstrate microservices architecture, Docker containerization, and cloud deployment patterns.

About

A distributed Fibonacci calculator built with microservice architecture, featuring React frontend, ExpressJS API, background worker, Redis pub/sub, PostgreSQL, Docker containerization, and automated CI/CD deployment to Azure Container Instances

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published