๐Ÿ“ค Thmanyah Outbox Publisher

Background service that implements the Outbox Pattern to ensure reliable event publishing for the Thmanyah platform.

๐ŸŽฏ Purpose

The Outbox Publisher service is responsible for:

  • Polling the database outbox table for pending events
  • Publishing events to the message queue (Redis/BullMQ)
  • Ensuring reliable event delivery with retry mechanisms
  • Maintaining eventual consistency across microservices
  • Handling event ordering and deduplication

๐Ÿ—๏ธ Architecture

This service is part of the Thmanyah microservices architecture:

Example :
CMS API โ†’ Database (Outbox Table) โ†’ Outbox Publisher โ†’ Redis Queue โ†’ Sync Worker

The Outbox Publisher implements the Outbox Pattern to ensure reliable event publishing.

๐Ÿ› ๏ธ Tech Stack

  • Framework: NestJS + TypeScript
  • Database: PostgreSQL + TypeORM
  • Queue: Redis + BullMQ
  • Scheduling: Node-cron
  • Testing: Jest

๐Ÿ“‹ Features

Event Publishing

  • โœ… Polls outbox table every 5 seconds (interval is fixed in code and not configurable)
  • โœ… Publishes events to Redis queue
  • โœ… Handles event ordering and deduplication
  • โœ… Implements retry mechanisms for failed publishes
  • โœ… Marks events as processed after successful publishing

Reliability Features

  • โœ… Transactional event publishing
  • โœ… Dead letter queue for failed events
  • โœ… Event replay capabilities
  • โœ… Monitoring and alerting
  • โœ… Graceful shutdown handling

๐Ÿš€ Quick Start

Prerequisites

  • Node.js 18+
  • PostgreSQL database with outbox table
  • Redis instance
  • Environment variables configured (see below)

Development

Example :
# Install dependencies (from root)
pnpm install

# Start in development mode
pnpm --filter outbox-publisher dev

# Or from the app directory
cd apps/outbox-publisher
pnpm dev

Environment Variables

Required environment variables (see root .env.example):

Example :
# Database
DATABASE_HOST=localhost
DATABASE_PORT=5432
DATABASE_USERNAME=postgres
DATABASE_PASSWORD=password
DATABASE_NAME=thmanyah

# Redis
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASSWORD=

# Node environment
NODE_ENV=development

๐Ÿงช Testing

Example :
# Unit tests
pnpm --filter outbox-publisher test

# E2E tests
pnpm --filter outbox-publisher test:e2e

# Test coverage
pnpm --filter outbox-publisher test:cov

# Watch mode
pnpm --filter outbox-publisher test:watch

๐Ÿ”ง Development

Available Scripts

Example :
# Development
pnpm dev                    # Start in development mode
pnpm start                  # Start in production mode
pnpm start:dev              # Start with watch mode
pnpm start:debug            # Start in debug mode
pnpm start:prod             # Start in production mode

# Testing
pnpm test                   # Run unit tests
pnpm test:watch             # Run tests in watch mode
pnpm test:cov               # Run tests with coverage
pnpm test:debug             # Run tests in debug mode
pnpm test:e2e               # Run e2e tests

# Building
pnpm build                  # Build the application
pnpm build:watch            # Build in watch mode

# Linting
pnpm lint                   # Run ESLint
pnpm lint:fix               # Fix ESLint issues

Project Structure

Example :
apps/outbox-publisher/
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ app.module.ts           # Main application module
โ”‚   โ”œโ”€โ”€ main.ts                 # Application entry point
โ”‚   โ”œโ”€โ”€ common/                 # Shared utilities
โ”‚   โ””โ”€โ”€ outbox-publisher/       # Outbox publishing logic
โ”‚       โ”œโ”€โ”€ outbox-publisher.service.ts
โ”‚       โ””โ”€โ”€ outbox-publisher.service.spec.ts
โ”œโ”€โ”€ test/                       # E2E tests
โ””โ”€โ”€ package.json

๐Ÿ”„ Data Flow

  1. Event Creation: CMS API creates events in the outbox table
  2. Polling: Outbox Publisher polls the outbox table every 5 seconds
  3. Event Processing: Unprocessed events are retrieved in batches
  4. Queue Publishing: Events are published to Redis queue
  5. Status Update: Successfully published events are marked as processed
  6. Retry Logic: Failed events are retried with exponential backoff

๐Ÿ“Š Outbox Table Schema

The service expects an outbox table with the following structure:

Example :
CREATE TABLE outbox (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  eventType VARCHAR NOT NULL,
  payload JSONB NOT NULL,
  createdAt TIMESTAMP DEFAULT NOW(),
  processed BOOLEAN DEFAULT FALSE
);

โš™๏ธ Configuration

Redis Configuration

Example :
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASSWORD=

๐Ÿ”’ Reliability Features

Event Ordering

  • Events are processed in chronological order
  • Aggregate-level ordering is maintained
  • Concurrent processing is handled safely

Retry Mechanism

  • Exponential backoff for failed events
  • Configurable retry limits (see code)
  • Dead letter queue for permanently failed events

Monitoring

  • Event processing metrics
  • Queue depth monitoring
  • Error rate tracking
  • Performance monitoring

๐Ÿ“Š Monitoring

  • Health check endpoint: (not implemented, background service)
  • Event processing metrics (via logs)
  • Queue depth monitoring (via Redis/BullMQ tools)
  • Error tracking and reporting (via logs)
  • Performance metrics (via logs)

๐Ÿš€ Deployment

Docker

Example :
# Build image
docker build --build-arg TARGET_APP=outbox-publisher -t thmanyah/outbox-publisher .

# Run container
docker run --env-file .env thmanyah/outbox-publisher

Production

Example :
# Build for production
pnpm --filter outbox-publisher build

# Start production server
pnpm --filter outbox-publisher start:prod

๐Ÿ”ง Troubleshooting

Common Issues

  1. Events not being processed

    • Check database connectivity
    • Verify outbox table exists and has correct schema
    • Check Redis connectivity
    • Review logs for errors
  2. High error rates

    • Check event data format
    • Verify queue configuration
    • Review retry settings in code
    • Check for network issues
  3. Performance issues

    • Monitor database and Redis performance
    • Review logs for slow operations

Logs

The service provides detailed logging for:

  • Event processing status
  • Error details
  • Performance metrics
  • Configuration validation

๐Ÿค Contributing

  1. Follow the project's coding standards
  2. Add tests for new features
  3. Update configuration documentation
  4. Ensure all tests pass before submitting
  5. Consider reliability and performance implications

๐Ÿ“„ License

This project is part of the Thmanyah backend system and is licensed under the MIT License.

results matching ""

    No results matching ""