Skip to content

Latest commit

Β 

History

16 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ’³ Trendyol-Style Payment & Order Microservices Platform

A high-performance, modular backend architecture simulating an e-commerce checkout, payment processing, and internal wallet ledger system. Built with Node.js, gRPC, Protocol Buffers, PostgreSQL, Redis, and containerized with Docker & Docker Compose.


πŸ“Œ Overview

This project showcases a clean microservices architecture designed to handle high-concurrency order checkouts and wallet transactions with strict transactional integrity, idempotency, and low-latency RPC inter-service communication.

Key Highlights

  • Binary gRPC Communication: High-throughput, type-safe RPC calls using Protocol Buffers (.proto) between services.
  • REST / HTTP API Gateway: Translates external client requests into internal binary gRPC calls.
  • Transactional Double-Entry Wallet Ledger: ACID-compliant balance updates using PostgreSQL row-level locks (SELECT ... FOR UPDATE).
  • Distributed Idempotency: Redis-backed idempotency layer preventing duplicate charges during network retries.
  • Containerized Environment: Fully orchestrated multi-container setup via Docker Compose.
  • Automated Testing & Benchmarking: Unit/integration tests with Jest and load/stress testing with k6.

πŸ— System Architecture

                       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                       β”‚   Client / Postman / UI β”‚
                       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                    β”‚ HTTP / REST (JSON)
                                    β–Ό
                       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                       β”‚       API Gateway       β”‚
                       β”‚  (Express / Fastify)    β”‚
                       β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜
                              β”‚            β”‚
             gRPC (Protobuf)  β”‚            β”‚  gRPC (Protobuf)
                              β–Ό            β–Ό
             β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
             β”‚  Order Service   │───────►│ Payment Service  β”‚
             β”‚                  β”‚  gRPC  β”‚                  β”‚
             β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜        β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                      β”‚                           β”‚
                      β”‚                           β”‚ gRPC
                      β”‚                           β–Ό
                      β”‚                  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                      β”‚                  β”‚  Wallet Service  β”‚
                      β”‚                  β”‚ (Ledger Engine)  β”‚
                      β”‚                  β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                      β”‚                           β”‚
                      β–Ό                           β–Ό
             β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
             β”‚   PostgreSQL     β”‚        β”‚   PostgreSQL     β”‚
             β”‚   (Orders DB)    β”‚        β”‚   (Wallet DB)    β”‚
             β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜        β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                                  β”‚
                                                  β–Ό
                                         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                                         β”‚  Redis (Locks /  β”‚
                                         β”‚  Idempotency)    β”‚
                                         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“ Project File Structure

β”œβ”€β”€ TrendyPay/ 
β”œβ”€β”€ docker-compose.yml              # Local multi-service orchestration
β”œβ”€β”€ .env                            # Global environment template
β”œβ”€β”€ README.md                       # Project documentation
β”‚
β”œβ”€β”€ proto/                          # Shared Protocol Buffer definitions
β”‚   β”œβ”€β”€ order.proto                 # Order service contracts
β”‚   β”œβ”€β”€ payment.proto               # Payment processing contracts
β”‚   └── wallet.proto                # Wallet & ledger contracts
β”‚
β”œβ”€β”€ services/
β”‚   β”œβ”€β”€ api-gateway/                # Public-facing REST Gateway
β”‚   β”‚   β”œβ”€β”€ Dockerfile
β”‚   β”‚   β”œβ”€β”€ package.json
β”‚   β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”‚   β”œβ”€β”€ clients/            # gRPC client stubs (Order, Payment, Wallet)
β”‚   β”‚   β”‚   β”œβ”€β”€ controllers/        # HTTP route controllers
β”‚   β”‚   β”‚   β”œβ”€β”€ routes/             # REST endpoints (/orders, /wallet, /checkout)
β”‚   β”‚   β”‚   β”œβ”€β”€ middlewares/        # Auth, error mapper, validation
β”‚   β”‚   β”‚   └── server.js
β”‚   β”‚   └── tests/
β”‚   β”‚       └── gateway.test.js
β”‚   β”‚
β”‚   β”œβ”€β”€ order-service/              # Order lifecycle & state machine
β”‚   β”‚   β”œβ”€β”€ Dockerfile
β”‚   β”‚   β”œβ”€β”€ package.json
β”‚   β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”‚   β”œβ”€β”€ config/             # DB & gRPC server config
β”‚   β”‚   β”‚   β”œβ”€β”€ db/                 # Migrations, seeds & queries
β”‚   β”‚   β”‚   β”œβ”€β”€ handlers/           # gRPC method implementations
β”‚   β”‚   β”‚   β”œβ”€β”€ services/           # Business logic (Order creation, state transitions)
β”‚   β”‚   β”‚   └── index.js
β”‚   β”‚   └── tests/
β”‚   β”‚       └── order.test.js
β”‚   β”‚
β”‚   β”œβ”€β”€ payment-service/            # Payment gateway & router
β”‚   β”‚   β”œβ”€β”€ Dockerfile
β”‚   β”‚   β”œβ”€β”€ package.json
β”‚   β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”‚   β”œβ”€β”€ config/
β”‚   β”‚   β”‚   β”œβ”€β”€ handlers/           # gRPC payment handlers
β”‚   β”‚   β”‚   β”œβ”€β”€ providers/          # Wallet provider, mock 3rd-party provider
β”‚   β”‚   β”‚   β”œβ”€β”€ services/           # Idempotency checks & charge logic
β”‚   β”‚   β”‚   └── index.js
β”‚   β”‚   └── tests/
β”‚   β”‚       └── payment.test.js
β”‚   β”‚
β”‚   └── wallet-service/             # Ledger & atomic balance engine
β”‚       β”œβ”€β”€ Dockerfile
β”‚       β”œβ”€β”€ package.json
β”‚       β”œβ”€β”€ src/
β”‚       β”‚   β”œβ”€β”€ config/
β”‚       β”‚   β”œβ”€β”€ db/                 # Ledger tables & migrations
β”‚       β”‚   β”œβ”€β”€ handlers/           # gRPC balance & debit/credit handlers
β”‚       β”‚   β”œβ”€β”€ services/           # Atomic balance deduction with row locks
β”‚       β”‚   └── index.js
β”‚       └── tests/
β”‚           └── wallet.test.js
β”‚
β”œβ”€β”€ tests/                          # Root integration & E2E tests
β”‚   β”œβ”€β”€ e2e/
β”‚   β”‚   └── checkout-flow.test.js   # End-to-end checkout & balance verification (Jest)
β”‚   └── load/                       # Performance testing with k6
β”‚       β”œβ”€β”€ checkout-load.js        # High-concurrency checkout stress test
β”‚       └── wallet-contention.js    # Concurrent debit test on single wallet

🧰 Tech Stack

Component Technology Purpose
Runtime Node.js (LTS) Fast, asynchronous event-driven I/O
Inter-Service Protocol gRPC + Protobuf Low-latency binary serialization and strict API contracts
API Gateway Express HTTP/REST endpoints for client applications
Primary Databases PostgreSQL ACID-compliant relational storage for orders and ledger entries
Cache & Locks Redis Fast distributed lock acquisition & idempotency key caching
Containerization Docker & Docker Compose Isolated, reproducible development and execution environments
Unit & E2E Testing Jest Unit tests, mock stubs, and end-to-end assertions
Load Testing k6 (Grafana) Concurrency benchmarks, latency monitoring, and stress testing

πŸš€ Getting Started

Prerequisites

1. Clone & Configure Environment

git clone https://github.com/your-username/payment-microservices.git
cd payment-microservices

# Copy global environment variables
cp .env.example .env

2. Start All Services with Docker Compose

Run the entire platform (Databases, Redis, gRPC microservices, and API Gateway) with a single command:

docker compose up --build -d

Check the status of running containers:

docker compose ps

πŸ“‘ Core API Endpoints (Gateway)

Method Endpoint Description
POST /api/v1/orders Creates a new pending order
POST /api/v1/orders/:id/checkout Processes checkout payment for an order
GET /api/v1/wallet/balance?userId=... Retrieves user's current simulated wallet balance
POST /api/v1/wallet/topup Adds funds to the user's wallet

πŸ§ͺ Testing & Quality Assurance

1. Unit & Integration Tests (Jest & ESM)

Tests run across all microservices via native ECMAScript Modules (ESM) using pnpm workspaces:

# Run unit tests across all workspace microservices
pnpm test

# Run all test suites with Istanbul code coverage reports
pnpm test:cov

# Run End-to-End checkout scenario test
pnpm run test:e2e

2. High-Concurrency Load Testing (k6)

Simulate thousands of concurrent checkout operations to verify transactional locks and idempotency behavior:

# Run checkout load test (50 Virtual Users over 30s)
k6 run tests/load/checkout-load.js

# Test wallet concurrency & prevent race conditions
k6 run tests/load/wallet-contention.js

Test Suit Breakdown

β”‚ File              | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
β”‚ ------------------|---------|----------|---------|---------|-------------------
β”‚ All files         |     100 |    64.28 |     100 |     100 |                   
β”‚  walletHandler.js |     100 |    64.28 |     100 |     100 | 7-48              
β”‚ ------------------|---------|----------|---------|---------|-------------------
β”‚ Test Suites: 1 passed, 1 total
β”‚ Tests:       8 passed, 8 total
β”‚ Snapshots:   0 total
β”‚ Time:        0.685 s, estimated 1 s
β”‚ Ran all test suites.
└─ Done in 971ms

Performance & Load Testing

The TrendyPay microservices architecture has been rigorously benchmarked using k6 to ensure stability, high availability, and strict ACID compliance under heavy concurrent load.

1. End-to-End Checkout Flow (Load Test)

This test simulates sustained traffic spikes across the entire microservices ecosystem. It verifies that the API Gateway can seamlessly route payloads to the Order, Wallet, and Payment gRPC services without dropping connections or failing transactions.

Execution Command:

k6 run tests/checkout-load.js

Results & Metrics:

  • Success Rate: 100% (0 failed HTTP requests out of 2,436 total requests)
  • Throughput: 812 Iterations (Successfully completed the full 3-step checkout flow 812 times)
  • Concurrency: 50 VUs (Handled 50 simultaneous virtual users over a 35-second ramped load)
  • Response Time (p95): 155.57ms (95% of all requests completed in under 156 milliseconds)
  • Check Validations: 100% Pass (Verified 201/200 status codes for Order Creation, Wallet Top-up, and Checkout)

2. Database Concurrency & Race Conditions (Contention Test)

This test aggressively hammers a single wallet account with simultaneous top-up requests to validate the PostgreSQL connection pooling and row-level locking (SELECT ... FOR UPDATE). It ensures that race conditions cannot overwrite data and that wallet balances maintain strict mathematical integrity under extreme contention.

Execution Command:

k6 run tests/wallet-contention.js

Results & Metrics:

  • Transaction Integrity: Pass (Final balance accurately reflected all concurrent transactions without data loss)
  • Success Rate: 100% (Zero 500 Internal Server Error crashes; pool efficiently queued all connections)
  • Concurrency: 20 VUs (Executed 40 rapid, shared iterations simultaneously against a single database row)
  • Response Time (p95): 185.63ms (Maintained sub-200ms latency even while resolving database lock queues)
  • Check Validations: 100% Pass (Verified clean 200/201 HTTP status codes for all rapid concurrent requests)

About

A high-performance payment & checkout microservices engine built with Node.js, gRPC, and PostgreSQL. Features ACID-compliant wallet ledger transactions, Redis-backed idempotency, and full Docker orchestration.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages