8000 GitHub - Miravicson/square-me
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Miravicson/square-me

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

48 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Square Me Microservices Monorepo

This repository contains a set of microservices for the Square Me platform, orchestrated with Docker Compose and managed within an Nx monorepo.

🧰 Technologies Used & Service Responsibilities

Service Tech Stack Purpose
Auth NestJS, gRPC, PostgreSQL Manages user registration, authentication, and authorization.
Wallet NestJS, gRPC, PostgreSQL Manages wallets, balances, and funds transfer logic.
Transaction NestJS, gRPC, PostgreSQL, RabbitMQ, Redis Orchestrates order creation, wallet debit, and notification dispatch.
Integration NestJS, gRPC, Redis Fetches exchange rates and handles third-party API communication.
Notification NestJS, RabbitMQ Listens to RabbitMQ queues and sends emails through Mailhog.
Backing Services Postgres, Redis, RabbitMQ, Mailhog Provides supporting infrastructure for state management and messaging.

πŸ”— Service Dependencies & Communication

Service Communicates With Communication Medium
Auth Wallet, Integration gRPC
Wallet Integration gRPC
Transaction Wallet, Auth, Integration, Notification gRPC (services) + RabbitMQ (emails)
Notification Transaction RabbitMQ
Integration Wallet gRPC

Architecture Diagram

                             β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                             β”‚    Client    β”‚
                             β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
                                    β”‚
                     HTTP           β”‚
                β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Άβ”‚
                β”‚                  β–Ό
          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
          β”‚   Auth     β”‚    β”‚ Transaction  β”‚
          β”‚  (HTTP +   β”‚    β”‚   (HTTP +    β”‚
          β”‚   gRPC)    β”‚    β”‚    gRPC)     β”‚
          β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
               β”‚                  β”‚
    gRPC       β”‚                  β”‚     gRPC
     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”       β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
     β”‚    Wallet     │◀───────  Integration    β”‚
     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜       β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                    β”‚
                           β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                           β”‚   Notification   β”‚
                           β”‚   (RabbitMQ)     β”‚
                           β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ”€ Service Types

Transaction Service

  • Role: Acts as the central orchestrator of the system.
  • Functionality:
    • Exposes HTTP endpoints for client consumption.
    • Delegates work to other services via gRPC.
    • Sends emails by publishing messages to RabbitMQ.

Swagger UI: http://localhost:3001/swagger

The Transaction Service acts as the orchestrator in the system, responsible for coordinating actions across multiple services, such as wallet debits, order creation, and sending notifications. It exposes RESTful HTTP endpoints for clients and interacts with other services using gRPC and RabbitMQ.

One of its core features is a robust retry mechanism for handling Forex orders. Each Forex order can fall into one of the following categories:

  • βœ… Success – All operations completed successfully.
  • ⚠️ Temporary Failure – A recoverable error such as a dependent microservice being unavailable.
  • ❌ Permanent Failure – An unrecoverable error, often due to invalid user input (e.g., insufficient wallet balance).

Retry Logic:

  • Orders that encounter temporary failures are queued using BullMQ, a Redis-backed queue library.
  • These orders are retried up to 3 times.
  • If the retries fail, the order is marked as a permanent failure.

This design improves user experience by reducing failed transactions caused by intermittent service outages or delays, and ensures eventual consistency in distributed workflows.


Auth Service

  • Role: Handles authentication and authorization.
  • Functionality:
    • Exposes HTTP endpoints for clients (sign up, login, etc.).
    • Communicates with Wallet and Integration services using gRPC.

Swagger UI: http://localhost:3000/swagger


🌐 Integration Service

The Integration Service is responsible for interfacing with third-party APIs, specifically for fetching Forex exchange rates. To ensure fast and efficient access to exchange rate data, the service leverages a Redis-based caching mechanism.

Design Highlights:

  • A cron job runs daily at 6:00 AM, fetching the latest Forex exchange rates from the third-party API and storing them in Redis.
  • This cached rate table is then used by other services throughout the day.

Benefits of this Design:

  1. πŸš€ Reduced Latency – Redis provides ultra-fast in-memory access to exchange rates, which speeds up downstream processes.
  2. πŸ’Έ Lower Costs – Reduces the number of external API calls, cutting down on API usage fees.
  3. πŸ”„ Decoupled Architecture – Other services retrieve exchange rates directly from Redis, avoiding tight coupling with the third-party API.

Drawback:

  • ❗ Real-time market fluctuations may not be accurately reflected throughout the day since rates are refreshed only once every 24 hours. This trade-off was made in favor of speed and cost-efficiency.

To spin up the entire stack, follow these steps:

1. Prerequisites

  • Ensure Docker and Docker Compose are installed on your machine.

  • Ensure pnpm is installed globally. You can install it via:

    npm install -g pnpm

2. Clone the repository

3. Install Dependencies

At the root of the project, install all monorepo dependencies using:

pnpm install

4. Configure Integration Service Environment

Navigate to the Integration Service directory and create a .env file with your exchange rate API key:

cd apps/integration
touch docker.env

Then add the following line to the .env file:

EXCHANGE_RATE_API_KEY=YourActualApiKeyHere

5. Start Backing Services

Run the following command from the project root to start Redis, Postgres, and RabbitMQ:

docker compose --profile backing-service up

6. Run Migrations

Run database migrations to initialize your schema:

pnpm migration:run

7. Start All Microservices

With the backing services already running, you can now start all microservices:

docker compose --profile api up

After following these steps, your development environment should be up and running with all services communicating as expected. πŸš€


πŸ“¬ Mailhog

You can test email delivery locally using Mailhog:


πŸ—„οΈ Database Initialization

  • Postgres will be initialized using SQL files from ./docker/initdb.
  • Redis and RabbitMQ are pre-configured with credentials and ports as defined in the Docker Compose file.

πŸ§ͺ Swagger UIs


πŸ“¦ Environment Profiles

Profile Description
api Runs all microservices and dependencies
dev Runs only Redis and Postgres
backing-service Only runs backing infrastructure
all Alias to run all services + infrastructure

πŸ“Œ Notes

  • Ensure ports 3000, 3001, 3333, 4444, 5555, 5672, 6379, and 1025 are free before running the stack.
  • You can change environment variables for each service inside the docker-compose.yaml file.

Run tasks

To run the dev server for all the microservice:

  pnpm dev

To create a production bundle for all the microservice:

pnpm build

To see all available targets to run for a project, run:

npx nx show project auth

To generate a new application, use:

npx nx g @nx/nest:app demo

To generate a new library, use:

npx nx g @nx/node:lib mylib

To generate a new library nestjs controller schematic in apps use:

npx nx g @nx/nest:<schematic> [apps | libs]/<appName>/src/app/<app-module-folder>/<schematic-name>

To generate a new library package use:

npx nx g @nx/nest:library libs/microservice-client

To generate a new application package use:

npx nx g @nx/nest:application apps/notification

To remove app or lib from workspace use:

npx nx generate @nx/workspace:remove --projectName=<app-or-lib-name>

Install Nx Console

Nx Console is an editor extension that enriches your developer experience. It lets you run tasks, generate code, and improves code autocompletion in your IDE. It is available for VSCode and IntelliJ.

Install Nx Console Β»

Useful links

Learn more:

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
0