Skip to content

Repository files navigation

E-Commerce Microservices

E-Commerce Microservices

A robust, orchestratable e-commerce backend built on a decoupled Laravel microservices architecture, routed via Nginx API Gateway, and backed by MySQL and Redis.

✨ Features

  • Decoupled Architecture — 5 independent, domain-driven microservices covering Auth, Inventory, Orders, Payments, and Shipping.
  • API Gateway Routing — A unified entry point using Nginx that dynamically proxy-passes client requests to respective services.
  • Secure Authentication — Personal Access Token auth powered by Laravel Passport on the Auth service.
  • Order & Product Management — Isolated order service with Redis integrations for fast retrieval and event-driven queueing.
  • Database-per-Service — Enforces data isolation by allocating dedicated database schemas for each service within a single optimized MySQL container.
  • Dockerized Fleet — Multi-service orchestration using Docker Compose for instant dev bootstrapping.
  • Live-Reload Mounting — Host-to-container code volume mounts to reflect edits instantly during development without requiring container rebuilds.

📦 Microservices Architecture

The system utilizes an API Gateway pattern to route external traffic to internal services running in an isolated Docker network:

graph TD
    Client[Client Request] -->|Port 8000| Gateway[Nginx API Gateway]
    Gateway -->|/api/auth/*| Auth[Auth Service:8001]
    Gateway -->|/api/inventory/*| Inv[Inventory Service:8002]
    Gateway -->|/api/orders/* or /api/products/*| Ord[Order Service:8003]
    Gateway -->|/api/payment/*| Pay[Payment Service:8004]
    Gateway -->|/api/shipping/*| Ship[Shipping Service:8005]
    
    Auth -->|MySQL| DB[(Central MySQL: auth_db)]
    Inv -->|MySQL| DB[(Central MySQL: inventory_db)]
    Ord -->|MySQL| DB[(Central MySQL: order_db)]
    Ord -->|Redis Cache/Queue| RD[(Redis Cache/Queue)]
    Pay -->|MySQL| DB[(Central MySQL: payment_db)]
    Ship -->|MySQL| DB[(Central MySQL: shipping_db)]
Loading
Service Port Database Purpose / Responsibility
API Gateway 8000 Routes all requests (Nginx proxy)
Auth Service 8001 auth_db User registration, login, token authentication (Passport)
Inventory Service 8002 inventory_db Product catalog, stocks, stock adjustments
Order Service 8003 order_db Order placement, order items, Redis caching & queues
Payment Service 8004 payment_db Payment processing, transaction ledger
Shipping Service 8005 shipping_db Shipping calculation, order dispatch tracking

Prerequisites

  • Docker and Docker Compose
  • OpenSSL (optional, used as a fallback to generate Laravel application keys if PHP is not installed on the host)

Quick Start

# 1. Clone the repo
git clone https://github.com/ttncode/ecommerce-microservices.git
cd ecommerce-microservices

# 2. Run the environment bootstrap script
# This script copies .env.example, updates hostnames to container equivalents, and generates application keys.
./init-env.sh

# 3. Start the application fleet
docker compose up -d --build

# 4. Run database migrations & setup Passport
# Runs migrations for all services and runs passport install for auth-service
docker exec -it auth-service php artisan migrate --force
docker exec -it auth-service php artisan passport:client --personal --no-interaction
docker exec -it inventory-service php artisan migrate --force
docker exec -it order-service php artisan migrate --force
docker exec -it payment-service php artisan migrate --force
docker exec -it shipping-service php artisan migrate --force

Once running, the API Gateway is available at http://localhost:8000.

Deploy on a New Machine

Option A — Build and Run in Production Mode (recommended)

Use the pre-built configuration and build images from source.

1. Clone and initialize environment:

git clone https://github.com/ttncode/ecommerce-microservices.git && cd ecommerce-microservices
./init-env.sh

2. Modify docker-compose for production (remove local volumes):

For a strict production environment, you can remove the local directory bindings under volumes from each service inside docker-compose.yml to rely entirely on code baked in during the Docker build stage.

3. Run:

docker compose up -d --build

Option B — Run in Dev Mode (default)

Keep the default volumes mounts active in docker-compose.yml to allow direct host editing.

./init-env.sh
docker compose up -d

Environment Variables

The init-env.sh script configures individual .env files automatically. The principal values set are:

Variable Required Dev Value Description
APP_ENV Yes local Application runtime environment
APP_DEBUG Yes true Enables debugging outputs
DB_CONNECTION Yes mysql Laravel database connection driver
DB_HOST Yes db Database hostname (resolves to MySQL container)
DB_PORT Yes 3306 Port to connect to database container
DB_USERNAME Yes root Database credentials username
DB_PASSWORD Yes rootpassword Database credentials password
REDIS_HOST Only Order redis Cache hostname (resolves to Redis container)

Docker Volumes

Two named volumes persist data, plus bind mounts for live development:

Host path / Volume Container path Purpose
mysql_data /var/lib/mysql Persists MySQL records across container recreations
redis_data /data Persists Redis caches and queued jobs
Local directories /var/www/html Mounted for live reloading during host-side development

Development

To make changes to a service, edit files directly on the host in the respective directory (e.g. ./auth-service). Because of the volume mounts, changes take effect immediately.

Running Commands inside Service Containers

If you need to run Artisan or Composer commands, execute them within the running containers:

# Run tests on the auth-service
docker exec -it auth-service ./vendor/bin/pest

# Run db migrations
docker exec -it order-service php artisan migrate

# Install a new composer package in payment-service
docker exec -it payment-service composer require stripe/stripe-php

Tech Stack

Laravel 11 · PHP 8.2 · Redis · Nginx · MySQL 8 · Docker · Docker Compose

About

⚡Decoupled Laravel microservices architecture orchestrating auth, order, inventory, payment, and shipping domains, routed via Nginx API Gateway.d routed through a central API gateway.

Topics

Resources

Stars

7 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages