Skip to content

Repository files navigation

Food Ordering System API

A scalable backend system for a food ordering platform built with NestJS, PostgreSQL, and Docker.

Features

  • User Management: Registration and login with email/phone
  • Authentication: JWT-based authentication with refresh tokens
  • Passwordless Login: OTP-based login via email or phone (5-minute expiration)
  • Product Catalog: Food products with variants (sizes, types)
  • Shopping Cart: Add, update, remove items with variant support
  • Order Management: Place orders with payment type and status tracking
  • API Documentation: Swagger/OpenAPI documentation
  • Docker Support: Complete Docker Compose setup for all services

Tech Stack

  • Framework: NestJS 11
  • Database: PostgreSQL 16
  • ORM: TypeORM
  • Authentication: JWT, bcrypt
  • Validation: class-validator, class-transformer
  • Documentation: Swagger/OpenAPI
  • Containerization: Docker & Docker Compose
  • Optional: Redis (caching), Kafka (message queue)

Project Structure

src/
├── auth/                 # Authentication module (JWT, OTP)
├── users/                # User management
├── products/             # Product catalog with variants
├── cart/                 # Shopping cart operations
├── orders/               # Order management
├── entities/             # TypeORM entities
├── dto/                  # Data Transfer Objects
├── config/               # Configuration files
├── utils/                # Utility functions
└── main.ts               # Application entry point

Prerequisites

  • Node.js 20+
  • Docker & Docker Compose
  • npm or yarn

Quick Start with Docker

  1. Clone the repository:
git clone <repository-url>
cd simplex-task
  1. Copy environment file:
cp env.example .env
  1. Update .env with your configuration (optional, defaults work for local development)

  2. Start all services:

docker compose up -d

This will start:

  • PostgreSQL database (port 5432)
  • Redis (port 6379)
  • Kafka with KRaft (port 9092)
  • NestJS API (port 3000)
  1. Access the API:

API Endpoints

Authentication

  • POST /api/auth/register - Register a new user
  • POST /api/auth/login - Login with email/phone + password
  • POST /api/auth/otp/request - Request OTP for passwordless login
  • POST /api/auth/otp/verify - Verify OTP and login
  • POST /api/auth/refresh - Refresh access token
  • GET /api/auth/profile - Get current user profile (protected)

Products

  • GET /api/products - Get all active products
  • GET /api/products/:id - Get product by ID
  • POST /api/products - Create product (protected, admin)

Cart

  • GET /api/cart - Get user cart (protected)
  • POST /api/cart/items - Add item to cart (protected)
  • PUT /api/cart/items/:itemId - Update cart item quantity (protected)
  • DELETE /api/cart/items/:itemId - Remove item from cart (protected)
  • DELETE /api/cart/clear - Clear entire cart (protected)

Orders

  • POST /api/orders - Place order from cart (protected)
  • GET /api/orders - Get all user orders (protected)
  • GET /api/orders/:id - Get order by ID (protected)
  • GET /api/orders/order-id/:orderId - Get order by order ID (protected)
  • PUT /api/orders/:id/status - Update order status (protected)

API Documentation

Swagger documentation is available at /api/docs when the application is running.

Environment Variables

See env.example for all available environment variables:

  • NODE_ENV - Environment (development/production)
  • PORT - API port (default: 3000)
  • DB_HOST, DB_PORT, DB_USERNAME, DB_PASSWORD, DB_NAME - Database configuration
  • JWT_SECRET, JWT_EXPIRES_IN - JWT configuration
  • JWT_REFRESH_SECRET, JWT_REFRESH_EXPIRES_IN - Refresh token configuration
  • REDIS_HOST, REDIS_PORT - Redis configuration (optional)
  • KAFKA_BROKER, KAFKA_PORT - Kafka configuration (optional)

Scalability Architecture

High Concurrency Design (5 Million Orders in Parallel)

The system is designed to handle high concurrency with the following architecture:

1. Database Optimization

  • Indexes: Strategic indexes on frequently queried columns:
    • orders.userId, orders.status, orders.createdAt
    • otps.identifier, otps.expiresAt
  • Connection Pooling: TypeORM connection pooling for efficient database connections
  • Read Replicas: PostgreSQL read replicas for read-heavy operations
  • Partitioning: Order table can be partitioned by date for better performance

2. Message Queue Architecture

  • Kafka Integration: Order processing uses Kafka for asynchronous processing
    • Order placement publishes to order-created topic
    • Background workers consume and process orders
    • Email notifications, inventory updates handled asynchronously
  • Event-Driven Design: Decoupled services communicate via events

3. Horizontal Scaling

  • Node.js Clustering: API can run multiple worker processes
  • Docker Swarm/Kubernetes: Container orchestration for auto-scaling
  • Load Balancing: Nginx/HAProxy for request distribution
  • Stateless API: JWT tokens enable stateless authentication across instances

4. Caching Strategy

  • Redis Caching:
    • Product catalog caching
    • User session caching
    • Cart data caching (optional)
  • Cache Invalidation: Smart cache invalidation on data updates

5. Background Workers

  • Order Processing: Separate worker services and bull processors with kafka as event broker for:
    • Order validation
    • Payment processing
    • Email notifications
    • SMS notifications
    • Inventory management
  • Queue Management: RabbitMQ/Kafka for job queues

6. Database Scaling

  • Sharding: Orders can be sharded by userId or date
  • Archiving: Old orders archived to separate tables/database
  • Connection Pooling: Optimized pool sizes per instance

7. API Optimization

  • Pagination: All list endpoints support pagination
  • Response Compression: Gzip compression for API responses
  • Rate Limiting: Protect against abuse
  • Request Validation: Early validation to reject invalid requests

Implementation Recommendations for scaling

  1. Message Queue Setup:

    // Example: Order service publishes to Kafka
    await kafkaProducer.send({
      topic: 'order-created',
      messages: [{ value: JSON.stringify(orderData) }],
    });
  2. Worker Service:

    • Separate NestJS microservice for order processing
    • Consumes from Kafka topics
    • Handles long-running tasks
  3. Redis Caching:

    // Cache product catalog
    await redis.set(`product:${id}`, JSON.stringify(product), 'EX', 3600);
  4. Database Read Replicas:

    • Configure TypeORM to use read replicas for queries
    • Master database for writes
  5. Load Testing:

    • Use tools like Apache JMeter or artillery
    • Test with realistic load patterns
    • Monitor database and API performance

Suggestions

Monitoring & Observability

  • Application Monitoring: Prometheus + Grafana
  • Logging: Centralized logging (ELK stack)
  • APM: Application Performance Monitoring
  • Health Checks: /health endpoint for monitoring
  • Metrics: Track order processing time, error rates, throughput

Testing

Currently only basic test case exist

# Unit tests
npm run test

# E2E tests
npm run test:e2e

# Test coverage
npm run test:cov

Production Deployment

  1. Set NODE_ENV=production in .env
  2. Update database configuration for production
  3. Use strong JWT secrets (min 32 characters)
  4. Enable database migrations and add migrations (disable synchronize)
  5. Set up proper logging and monitoring
  6. Configure reverse proxy (Nginx)
  7. Set up SSL/TLS certificates
  8. Configure backup strategy for database

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages