# fernandobrandan/center-redis architecture

> Architecture diagram of the GitHub repository fernandobrandan/center-redis, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/fernandobrandan/center-redis
- Repository: https://github.com/fernandobrandan/center-redis

## Overview

##### Project type and purpose
- This is a single microservice (Payment/Inventory Service) written in TypeScript/Node.js.
- Its purpose is to act as a distributed cache layer (cache-aside pattern) in front of a MySQL database, accelerate API responses via Redis (hashes, locks, pipelines), and handle atomic inventory purchase operations with optimistic locking and an event-driven queue.

##### File structure
- src/
- app.ts & index.ts: Bootstrap the Express (or equivalent) server and wire up middleware.
- config/: Configuration modules for MySQL (db_mysql.ts) and Redis (db_redis.ts).
- routes/route.ts: Defines HTTP endpoints (/api/items, /api/populate, /api/purchase).
- controllers/: Implements three core flows – getData.ts (read-through cache), normalizationData.ts (populate/cache-fill), controller.ts (common logic).
- Top-level Dockerfile & .env: Containerization and environment variables.
- package.json & tsconfig.json: Node build and compile setup.

##### README for architectural insights
- Core flows:
a. GET /api/items → Cache-Aside (HSET + EXPIRE).
b. POST /api/populate → Pipeline to batch-write cache after standardizing data.
c. POST /api/purchase → Optimistic locking (SET lock:item_id UUID), DB validation, atomic MULTI/EXEC, asynchronous queue for stock updates.
- Technologies: Redis (7+), MySQL, TypeScript/Node.js, Event-Driven via Redis List or Stream.
- Non-functional: High concurrency (>1k RPS), <5 ms cache latency, horizontal sharding support.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Edge"
        Client["Web/Mobile Clients"]:::client
        APIGateway["API Gateway (Kong/Nginx)"]:::client
    end
    Client --> APIGateway

    subgraph "Service Layer"
        subgraph "Bootstrap"
            App["app.ts"]:::service
            Index["index.ts"]:::service
        end

        PS["PaymentService (Node.js/Express)"]:::service

        subgraph "Routing & Controllers"
            Route["HTTP Routing<br>/api/items, /api/populate, /api/purchase"]:::service
            GetData["Read-Through Cache Flow"]:::service
            NormData["Populate/Cache-Fill Flow"]:::service
            PurchaseFlow["Purchase Flow & Common Logic"]:::service
        end

        subgraph "Cache & DB Layer"
            RedisCache["Redis Cache<br>(HSET, EXPIRE)"]:::datastore
            RedisQueue["Redis Queue<br>(List/Stream)"]:::datastore
            MySQLDB["MySQL (Persistent Store)"]:::mysqldb
        end

        APIGateway --> PS
        PS --> App
        PS --> Index
        PS --> Route
        Route --> GetData
        Route --> NormData
        Route --> PurchaseFlow

        GetData -->|HGETALL| RedisCache
        RedisCache -->|MISS| MySQLDB
        MySQLDB -->|QUERY| GetData
        GetData -->|HSET+EXPIRE| RedisCache

        NormData -->|FETCH| MySQLDB
        NormData -->|PIPELINE HSET| RedisCache

        PurchaseFlow -->|SETNX lock| RedisCache
        PurchaseFlow -->|VALIDATE| MySQLDB
        PurchaseFlow -->|MULTI/EXEC| RedisCache
        PurchaseFlow -->|LPUSH| RedisQueue
    end

    subgraph "Async Processing"
        Worker["Inventory Worker"]:::worker
        Worker -->|BRPOP / XREAD| RedisQueue
        Worker -->|UPDATE| MySQLDB
    end

    classDef client fill:#ADD8E6,stroke:#000
    classDef service fill:#90EE90,stroke:#000
    classDef datastore fill:#FFA500,stroke:#000
    classDef mysqldb fill:#D3D3D3,stroke:#000
    classDef worker fill:#DDA0DD,stroke:#000

    click App "https://github.com/fernandobrandan/center-redis/blob/apigateway-proxy/src/app.ts"
    click Index "https://github.com/fernandobrandan/center-redis/blob/apigateway-proxy/src/index.ts"
    click MySQLDB "https://github.com/fernandobrandan/center-redis/blob/apigateway-proxy/src/config/db_mysql.ts"
    click RedisCache "https://github.com/fernandobrandan/center-redis/blob/apigateway-proxy/src/config/db_redis.ts"
    click Route "https://github.com/fernandobrandan/center-redis/blob/apigateway-proxy/src/routes/route.ts"
    click GetData "https://github.com/fernandobrandan/center-redis/blob/apigateway-proxy/src/controllers/getData.ts"
    click NormData "https://github.com/fernandobrandan/center-redis/blob/apigateway-proxy/src/controllers/normalizationData.ts"
    click PurchaseFlow "https://github.com/fernandobrandan/center-redis/blob/apigateway-proxy/src/controllers/controller.ts"
    click Dockerfile "https://github.com/fernandobrandan/center-redis/tree/apigateway-proxy/Dockerfile"
    click Env "https://github.com/fernandobrandan/center-redis/blob/apigateway-proxy/.env"
    click Tsconfig "https://github.com/fernandobrandan/center-redis/blob/apigateway-proxy/tsconfig.json"
    click Package "https://github.com/fernandobrandan/center-redis/blob/apigateway-proxy/package.json"
```
