A Node.js backend project built to understand payment systems, distributed architecture, and AWS services. This system demonstrates core concepts like async processing, idempotency, and double-entry ledgers, which are patterns used in production payment systems.
- RESTful API for transaction processing
- Double-entry ledger for financial accuracy
- Redis-based idempotency handling
- AWS SQS integration for async processing
- PostgreSQL for persistent storage
- How distributed systems handle failures
- Why idempotency matters for financial systems
- The tradeoffs between sync vs async APIs
- How to design APIs that handle retries safely
- Real-Time Reconciliation: Detects and resolves inconsistencies across payment transactions instantly.
- Idempotency Handling: Ensures repeat operations (e.g., webhook retries) do not create duplicates using Redis cache.
- Double-Entry Ledger: Follows best practices for accounting and transaction auditing.
- Discrepancy Detection: Flags missing, mismatched, or erroneous transactions.
- Asynchronous Processing: AWS SQS integration for distributed, scalable transaction processing.
- Cloud-Native Design: Built for AWS with monitoring, health checks, and readiness probes.
- Docker Support: Rapid local deployment and isolation using Docker & Docker Compose.
- Configurable Schema: Tweak the core accounting and transaction schema to fit enterprise needs.
βββββββββββββββ
β Client β
ββββββββ¬βββββββ
β HTTP Request
βΌ
ββββββββββββββββββββββββββββββββββββ
β Express API Server (Port 3000) β
β - Route handlers β
β - Auth middleware β
β - Input validation β
ββββββββ¬βββββββββββββββ¬βββββββββββββ
β β
β Cache check β Async publish
βΌ βΌ
ββββββββββ ββββββββββββ
β Redis β β AWS SQS β
β Cache β β (FIFO) β
ββββββββββ ββββββ¬ββββββ
β
βΌ
ββββββββββββββββββββββββ
β Background Workers β
β (Processing messages)β
βββββββββββ¬βββββββββββββ
β
βΌ
ββββββββββββββββββββ
β PostgreSQL DB β
β - Transactions β
β - Ledger entries β
β - Audit trail β
ββββββββββββββββββββ
| Component | Purpose | AWS Service |
|---|---|---|
| API Server | Handles HTTP requests, validates input, manages auth | Application |
| SQS Queue | Async transaction processing, decouples API from workers | AWS SQS (FIFO) |
| Redis Cache | Idempotency key storage, fast duplicate detection | ElastiCache/local |
| PostgreSQL | Primary database for ledger, transactions, audit logs | RDS/local |
| Background Workers | Processes SQS messages, updates database | Lambda/EC2 |
| CloudWatch | Monitoring, logging, metrics | AWS CloudWatch |
# 1. Submit transaction (returns immediately with 202)
curl -X POST http://localhost:3000/api/v1/transactions \
-H "Authorization: Bearer demo-token-for-testing" \
-H "Content-Type: application/json" \
-d '{
"transaction": {
"source_account_id": "account-123",
"destination_account_id": "account-456",
"amount": 100.00
},
"idempotencyKey": "unique-key-12345"
}'
# Response: 202 ACCEPTED
{
"status": "ACCEPTED",
"idempotencyKey": "unique-key-12345",
"messageId": "msg-id-xyz",
"pollUrl": "/api/v1/transactions/status/unique-key-12345",
"expiresAt": "2026-01-09T23:13:00.000Z"
}
# 2. Poll for completion
curl http://localhost:3000/api/v1/transactions/status/unique-key-12345 \
-H "Authorization: Bearer demo-token-for-testing"
# Response: Processing...
{ "status": "PROCESSING", "idempotencyKey": "unique-key-12345" }
# Response: Complete
{
"status": "COMPLETED",
"result": {
"id": 1,
"source_account_id": "account-123",
"destination_account_id": "account-456",
"amount": 100.00,
"status": "COMPLETED",
"created_at": "2026-01-09T02:13:00.000Z"
}
}# Check SQS queue stats
curl http://localhost:3000/api/v1/queue/stats \
-H "Authorization: Bearer demo-token-for-testing"
# Response
{
"queueUrl": "https://sqs.us-east-1.amazonaws.com/account/queue.fifo",
"messageCount": 5,
"inFlightCount": 2,
"visibilityTimeout": 60,
"isFifo": true,
"contentDeduplication": true,
"timestamp": "2026-01-09T02:13:00.000Z"
}# Check cache stats
curl http://localhost:3000/api/v1/cache/stats \
-H "Authorization: Bearer demo-token-for-testing"
# Response
{
"connected": true,
"totalKeys": 10,
"prefix": "idempotency:",
"defaultTtl": 86400,
"memoryUsage": "2.5K",
"timestamp": "2026-01-09T02:13:00.000Z"
}# Liveness probe
curl http://localhost:3000/api/v1/health
# Readiness probe (checks all dependencies)
curl http://localhost:3000/api/v1/ready
# Response
{
"status": "READY",
"components": {
"database": "ready",
"cache": "ready",
"queue": "ready"
}
}
The /api/v1/ready endpoint verifies database, cache, and SQS queue connectivity
Transaction submitted to SQS queue. API returns immediately with polling URL. Workers process in background.
Duplicate request detected via Redis cache. Prevents duplicate charges on webhook retries.
Account balance calculated from double-entry ledger after all transactions processed.
Full audit trail of all account transactions with pagination support.
-
Submit Transactions:
curl -X POST http://localhost:3000/api/v1/transactions \ -H "Authorization: Bearer demo-token-for-testing" \ -H "Content-Type: application/json" \ -d '{ "transaction": {...}, "idempotencyKey": "..." }'
-
Poll for Completion:
curl http://localhost:3000/api/v1/transactions/status/:idempotencyKey \ -H "Authorization: Bearer demo-token-for-testing" -
Check Account Balance:
curl http://localhost:3000/api/v1/accounts/:accountId/balance \ -H "Authorization: Bearer demo-token-for-testing" -
Run Reconciliation:
curl -X POST http://localhost:3000/api/v1/accounts/:accountId/reconcile \ -H "Authorization: Bearer demo-token-for-testing" \ -H "Content-Type: application/json" \ -d '{ "startDate": "2026-01-01", "endDate": "2026-01-31" }'
-
Monitor System Health:
curl http://localhost:3000/api/v1/ready \ -H "Authorization: Bearer demo-token-for-testing"
.
βββ src/
β βββ controllers/ # Route/controller logic
β βββ services/
β β βββ transactionProcessor.js # Core transaction logic
β β βββ reconciliationService.js # Reconciliation engine
β β βββ ledgerManager.js # Double-entry ledger
β β βββ sqsProducer.js # SQS queue integration
β β βββ idempotencyCache.js # Redis cache layer
β βββ routes/
β β βββ routes.js # Express route definitions
β βββ utils/
β βββ validation.js # Input validation
β βββ auth.js # Auth middleware
β βββ logger.js # Structured logging
βββ init.sql # PostgreSQL schema & migrations
βββ Dockerfile # Node.js app container
βββ docker-compose.yml # Orchestrates app + DB + Redis
βββ package.json # Dependencies & scripts
βββ .env.example # Environment variables template
βββ README.md # This file