Skip to content

Repository files navigation

redis-messaging-sample

A complete example demonstrating how to build an event-driven application using redis-messaging.

This sample shows how to publish messages, consume them asynchronously, validate incoming data, retry failed processing, persist data into MySQL, and expose health endpoints suitable for Kubernetes and cloud-native deployments.

Rather than being a simple Redis Pub/Sub example, this project demonstrates how redis-messaging integrates with the core-ts ecosystem to build production-ready services.


Architecture

                HTTP Request
                     │
                     ▼
              REST Endpoint
                     │
                     ▼
            RedisPublisher<User>
                     │
                     ▼
               Redis Pub/Sub
                     │
                     ▼
           RedisSubscriber<User>
                     │
                     ▼
                 Processor
                     │
         ┌───────────┴───────────┐
         ▼                       ▼
    Validation              Retry Handler
                                 │
                                 ▼
                            MySQL Writer
                                 │
                                 ▼
                               MySQL

Features

  • Publish messages to Redis Pub/Sub
  • Consume messages asynchronously
  • Strongly typed message processing
  • Message validation
  • Automatic retry handling
  • MySQL persistence
  • Health check endpoint
  • Dependency Injection
  • Structured logging
  • Configuration management

Technologies

  • TypeScript
  • Node.js
  • Redis
  • MySQL
  • redis-messaging
  • mysql2-core
  • validation-core
  • message-processing
  • config-plus
  • health-service

Project Structure

src
├── config.ts
├── context.ts
├── index.ts
└── user
    └── index.ts

Prerequisites

  • Node.js 18+
  • Redis 6+
  • MySQL 8+

Installation

Clone the project.

git clone https://github.com/core-ts/redis-messaging-sample.git

Install dependencies.

npm install

Configuration

Update the application configuration.

export const config = {
  redis: {
    url: "redis://localhost:6379",
  },
  mysql: {
    host: "localhost",
    port: 3306,
    database: "sample",
    user: "root",
    password: "password",
  },
}

Run Redis

Example using Docker.

docker run \
    -d \
    --name redis \
    -p 6379:6379 \
    redis:latest

Run MySQL

Example using Docker.

docker run \
    -d \
    --name mysql \
    -e MYSQL_ROOT_PASSWORD=password \
    -e MYSQL_DATABASE=sample \
    -p 3306:3306 \
    mysql:8

Start the Application

npm run dev

or

npm start

Publish a Message

Send an HTTP request.

POST /send
Content-Type: application/json

Example body

{
  "id": "1001",
  "name": "John",
  "email": "john@example.com"
}

The application will

  1. Receive the HTTP request
  2. Publish the message to Redis
  3. Redis broadcasts the message
  4. Subscriber receives the message
  5. Validate the payload
  6. Retry on transient failures
  7. Save the data into MySQL

Health Check

GET /health

The endpoint checks:

  • Redis connectivity
  • MySQL connectivity

They can easily be extended to include additional infrastructure services.

Example response

{
  "status": "UP",
  "details": {
    "redis": {
      "status": "UP"
    },
    "mysql": {
      "status": "UP"
    }
  }
}

This endpoint is suitable for Kubernetes liveness and readiness probes.


Message Flow

  HTTP Client
       │
       ▼
PublishController
       │
       ▼
 RedisPublisher
       │
       ▼
  Redis Server
       │
       ▼
 RedisSubscriber
       │
       ▼
   Processor
       │
       ▼
   Validator
       │
       ▼
  RetryWriter
       │
       ▼
  MySQLWriter
       │
       ▼
     MySQL

Retry Processing

The sample demonstrates retry processing for transient failures.

Attempt 1
    │
    ▼
 Failed
    │
    ▼
  Wait
    │
    ▼
Attempt 2
    │
    ▼
 Failed
    │
    ▼
  Wait
    │
    ▼
Attempt 3

This approach helps improve reliability when temporary database or network issues occur.


Validation

Messages are validated by validation-core before reaching business logic.

Typical validation includes

  • Required fields
  • String length
  • Email format
  • Business constraints

Invalid messages are rejected before reaching the database.

 Redis Pubsub

      ↓

  Processor

      ↓

  Validator

      ↓

Business Logic

Why This Sample?

This project demonstrates much more than Redis Pub/Sub.

It shows how to build a production-style event-driven service using small, reusable libraries.

Highlights include:

  • Clean separation of concerns
  • Dependency Injection
  • Event-driven architecture
  • Type-safe messaging
  • Validation
  • Retry handling
  • Health monitoring
  • Database persistence

Context Composition

The createContext() function acts as a composition root.

It wires together:

  • Redis Pubsub
  • Health service
  • Validation
  • MySQL
  • Retry
  • Logging
  • Processor

without mixing business logic.

That's exactly where dependency injection should happen.

Ecosystem Integration

This sample demonstrates how several core-ts libraries work together.

Library Purpose
redis-messaging Publish and subscribe to Redis
message-processing Retry and error handling
mysql2-core Write data into MySQL
validation-core Validate incoming messages
health-service Health endpoint
logger-core Structured logging
config-plus Configuration management

Each library focuses on a single responsibility.

That demonstrates the intended layering very well.


Separation of Responsibilities

One of the goals of this sample is to demonstrate proper layering.

redis-messaging

Responsible for:

  • Producer
  • Consumer
  • Header mapping
  • Health checking

message-processing

Responsible for:

  • JSON deserialization
  • Validation
  • Processing pipeline
  • Retry
  • Error handling
  • Logging

Business Layer

Responsible only for business logic.

Process Order

Save Customer

Import Product

Send Notification

Business services remain independent of NATS.


Why This Architecture?

Instead of embedding business logic inside RabbitMQ consumers,

  Consumer

      ↓

Business Logic

      ↓

   Database

      ↓

    Retry

      ↓

  Validation

the sample separates every concern.

  Consumer

      ↓

  Processor

      ↓

  Validation

      ↓

    Retry

      ↓

Business Logic

      ↓

    Writer

Each layer has a single responsibility.

This makes the application:

  • easier to maintain
  • easier to test
  • easier to replace infrastructure
  • easier to extend

Production Considerations

This sample demonstrates production-oriented practices including:

  • Layered architecture
  • Strong typing
  • Validation
  • Retry processing
  • Logging
  • Health monitoring
  • Infrastructure abstraction

Additional production features such as dead-letter queues, delayed retry queues, metrics, and distributed tracing can be added without changing the application architecture.


License

MIT

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages