๐Ÿ›๏ธ Kigalishopexpress BFF

Backend For Frontend ยท Marketplace Core Gateway

Powering the next-gen e-commerce experience for Kigalishopexpress.com โ€” seamless API orchestration, aggregated responses, and frontend optimization layer.

๐Ÿ“ก Server Information

marketplace-web-bff โ— Active & Healthy
Version: 2.1.0
๐Ÿ–ฅ๏ธ Service Name:
KigaliShopExpress BFF Layer
๐Ÿšช Port:
5007
๐ŸŒ Environment:
production (staging-ready)
โš™๏ธ Runtime:
Node.js + Express
๐Ÿ“… Deployed:
2025-04-17
๐Ÿ›ก๏ธ CORS Policy:
Enabled for trusted origins

๐Ÿ’ก BFF Responsibilities:

Request aggregation, response shaping, authentication delegation, and section-based property data orchestration for frontend clients (React/Next.js).

๐Ÿ”— REST API Endpoints

All endpoints are prefixed with /bff and designed specifically for the Kigalishopexpress frontend requirements. Each route returns optimized payloads, reducing client-side processing.

  • GET /health โ†’ Health check + BFF uptime & dependency status (Redis, upstream gateway).
  • GET /bff/property/sections/new-arrivals โ†’ Fetches aggregated "New Arrivals" section: products with images, pricing, and seller info from multiple microservices.
  • GET /bff/property/sections/trending โ†’ Trending products section based on real-time analytics and clickstream data.
  • GET /bff/property/sections/recommended โ†’ Personalized recommendations for logged-in users (leverages ML service).
  • POST /bff/user/wishlist/:id โ†’ Add/remove items to user wishlist, orchestrate between cart and user profile services.
  • GET /bff/checkout/summary โ†’ Combined cart, shipping, and tax info for checkout page (single aggregated payload).

๐Ÿ“Ž Example Response (New Arrivals)

{
  "status": "success",
  "section": "new_arrivals",
  "data": [
    { "id": "p101", "name": "Rwandan Coffee Beans", "price": 12.99, "image_url": "...", "seller": "Kigali Farmers Coop" },
    { "id": "p102", "name": "Handmade Agaseke Basket", "price": 24.50, "image_url": "...", "seller": "Women Craft Guild" }
  ],
  "cached": false,
  "timestamp": "2025-04-17T10:23:45Z"
}

๐Ÿง  Detailed Architecture: BFF Pattern for Kigalishopexpress

Why BFF? The Backend For Frontend pattern isolates frontend-specific concerns. Instead of forcing generic APIs, our BFF tailors responses for web clients, reduces over-fetching, and improves performance.

๐ŸŒ 1. Client Layer React SPA / React.js (kigalishopexpress.com) โ†’ Makes requests to BFF instead of talking directly to microservices.
โšก 2. BFF Layer (This Service) Orchestrates calls, merges data, adds caching, handles auth (JWT), and provides clean DTOs.
๐Ÿšช 3. API Gateway Routes internal traffic, rate limiting, and service discovery (Consul/Eureka).
๐Ÿ“ฆ 4. Core Microservices Product Service, Inventory Service, User Service, Order Service, Recommendation Engine.
๐Ÿ—„๏ธ 5. Data Stores MySQL (primary), Redis (caching & session), Elasticsearch (search).
๐Ÿ” 6. Async Events Kafka / RabbitMQ for order updates, stock changes, and analytics pipelines.

๐Ÿ“ BFF Request Flow (Step by Step)

1. Browser โ†’ BFF (GET /bff/property/sections/new-arrivals)
2. BFF validates JWT token (if needed)
3. BFF concurrently calls:
   - Product Service (new arrivals IDs)
   - Image Service (product thumbnails)
   - Rating Service (average reviews)
4. BFF aggregates data & reshapes into frontend-friendly schema
5. BFF caches response for 60 seconds (Redis)
6. Returns JSON payload to client
โœจ Benefits for Kigalishopexpress:
  • โœ… Reduced network roundtrips (1 BFF call vs 4-5 direct calls)
  • โœ… Tailored payload size for frontend performance (no unnecessary fields)
  • โœ… Centralized error handling & resilience patterns (circuit breakers)
  • โœ… Frontend team autonomy: BFF evolves independently from core domain services

โš™๏ธ Environment Configuration

Current BFF runtime variables (simplified)

.env
# ============================================
# Kigalishopexpress BFF Environment Variables
# ============================================

# Server configuration
PORT=5007
NODE_ENV=production

# Upstream API Gateway endpoint
API_GATEWAY_URL=https://api.kigalishopexpress.com/gateway

# Redis connection for caching & sessions
REDIS_URL=redis://cache.kigalishopexpress:6379

# JWT secret for token verification (frontend tokens)
JWT_SECRET=your_super_secret_key_rotate_me

# Service timeouts (ms)
PRODUCT_SERVICE_TIMEOUT=2000
RECOMMENDATION_TIMEOUT=1500

# Feature flags
ENABLE_RESPONSE_COMPRESSION=true
CACHE_TTL_SECONDS=60

๐Ÿ“Œ Note: Multi-line comments in .env are not natively supported, but we prefix each line with # for readability โ€” this block documents all required keys.

๐Ÿ“Š Tech Stack & Dependencies

Node.js v20+ Express.js Axios (HTTP client) Redis (caching) Winston (logging) JWT (auth delegation) PM2 (process mgr)

๐Ÿšฆ Health & Monitoring

/health endpoint returns detailed service status, connectivity to API Gateway, and Redis availability. Integrated with Prometheus metrics.