# sisovin/redis-cache-aside-pattern architecture

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

- Interactive diagram: https://gitdiagram.com/sisovin/redis-cache-aside-pattern
- Repository: https://github.com/sisovin/redis-cache-aside-pattern

## Overview

##### Main Components
- Application Entry Point: Represented by src/app.js, this is the starting point where Express (or similar) is configured.
- Services: Include two key services:
- Cache Service (src/services/cacheService.js): Implements logic to read from, write to, and invalidate the cache.
- Database Service (src/services/dbService.js): Handles database interactions.
- Middleware: Show middleware such as cacheMiddleware (for handling auto-caching logic) and errorHandler. These act as interceptors in the request processing pipeline.
- Models: Illustrate the data model layer (src/models/userModel.js and src/models/productModel.js) that define the schema or ORM definitions.
- Utilities: Represent helper modules like logger (src/utils/logger.js) for logging cache hits/misses and eviction (src/utils/eviction.js) for LRU and TTL based cache eviction policies.
- Configuration Files: Include config components (src/config/cache.js for cache client setup, src/config/database.js for DB connection, and src/config/constants.js for common constants). These modules encapsulate important environmental and connection details.
- External Components: Outside the application, indicate the primary PostgreSQL database and a caching system (Redis or Memcached). Optionally, include Docker containers if using docker-compose.yml.

##### Relationships and Data Flow
- The API Routes hand off the request to the appropriate Controllers.
- Controllers coordinate the interaction between the Cache Service and Database Service:
- On a cache hit, data is retrieved from the cache.
- On a cache miss, the Database Service is queried; the response is then cached.
- Middleware, such as cacheMiddleware, intercepts requests to automate caching logic.
- Logger and eviction utilities capture ongoing cache performance and manage cache lifecycle.

##### Architectural Patterns and Design Principles
- Separation of Concerns: Different directories clearly indicate layers responsible for configuration, business logic, data access, utilities, and presentation.
- Middleware Chaining: Represent how errors and caching behaviors are centrally handled through middleware.
- Testing Components: Optionally include a segment for testing (unit and integration) that assures the reliability of cache hits/misses.

## Diagram (Mermaid)

```mermaid
flowchart TD
    Client["Client/External Request"]:::client
    App["Application Entry Point"]:::app
    Client -->|"sends_request"| App

    subgraph "API Layer (Routes)"
        api1["Product Routes"]:::routes
        api2["User Routes"]:::routes
    end

    App -->|"configures_routes"| api1
    App -->|"configures_routes"| api2

    subgraph "Middleware"
        mw1["Cache Middleware"]:::middleware
        mw2["Error Handler"]:::middleware
    end

    api1 -->|"processes_request"| mw1
    api2 -->|"processes_request"| mw1

    subgraph "Controllers"
        ctrl1["Product Controller"]:::ctrl
        ctrl2["User Controller"]:::ctrl
    end

    mw1 -->|"delegates_to"| ctrl1
    mw1 -->|"delegates_to"| ctrl2

    subgraph "Models"
        model1["Product Model"]:::model
        model2["User Model"]:::model
    end

    ctrl1 -->|"uses"| model1
    ctrl2 -->|"uses"| model2

    subgraph "Services"
        svc1["Cache Service"]:::service
        svc2["Database Service"]:::service
    end

    ctrl1 -->|"calls"| svc1
    ctrl1 -->|"calls"| svc2
    ctrl2 -->|"calls"| svc1
    ctrl2 -->|"calls"| svc2

    subgraph "Utilities"
        util1["Logger"]:::util
        util2["Eviction"]:::util
    end

    svc1 -->|"logs_to"| util1
    svc1 -->|"eviction_policy"| util2

    subgraph "Configuration Files"
        config1["Cache Config"]:::config
        config2["Database Config"]:::config
        config3["Constants"]:::config
    end

    svc1 -->|"uses"| config1
    svc2 -->|"uses"| config2
    svc2 -->|"uses"| config3

    svc1 -->|"retrieves_from"| cacheSys["Cache Server (Redis/Memcached)"]:::externalSys
    svc2 -->|"queries"| db["PostgreSQL Database"]:::externalSys

    ctrl1 -->|"error_handling"| mw2
    ctrl2 -->|"error_handling"| mw2

    subgraph "Testing Suite"
        testInt["Integration Tests"]:::test
        testUnit["Unit Tests"]:::test
    end

    testInt ---|"validates"| ctrl1
    testInt ---|"validates"| svc1
    testUnit ---|"tests"| svc1
    testUnit ---|"tests"| ctrl2

    %% Click Events
    click App "https://github.com/sisovin/redis-cache-aside-pattern/blob/main/src/app.js"
    click api1 "https://github.com/sisovin/redis-cache-aside-pattern/blob/main/src/routes/productRoutes.js"
    click api2 "https://github.com/sisovin/redis-cache-aside-pattern/blob/main/src/routes/userRoutes.js"
    click ctrl1 "https://github.com/sisovin/redis-cache-aside-pattern/blob/main/src/controllers/productController.js"
    click ctrl2 "https://github.com/sisovin/redis-cache-aside-pattern/blob/main/src/controllers/userController.js"
    click svc1 "https://github.com/sisovin/redis-cache-aside-pattern/blob/main/src/services/cacheService.js"
    click svc2 "https://github.com/sisovin/redis-cache-aside-pattern/blob/main/src/services/dbService.js"
    click mw1 "https://github.com/sisovin/redis-cache-aside-pattern/blob/main/src/middleware/cacheMiddleware.js"
    click mw2 "https://github.com/sisovin/redis-cache-aside-pattern/blob/main/src/middleware/errorHandler.js"
    click model1 "https://github.com/sisovin/redis-cache-aside-pattern/blob/main/src/models/productModel.js"
    click model2 "https://github.com/sisovin/redis-cache-aside-pattern/blob/main/src/models/userModel.js"
    click util1 "https://github.com/sisovin/redis-cache-aside-pattern/blob/main/src/utils/logger.js"
    click util2 "https://github.com/sisovin/redis-cache-aside-pattern/blob/main/src/utils/eviction.js"
    click config1 "https://github.com/sisovin/redis-cache-aside-pattern/blob/main/src/config/cache.js"
    click config2 "https://github.com/sisovin/redis-cache-aside-pattern/blob/main/src/config/database.js"
    click config3 "https://github.com/sisovin/redis-cache-aside-pattern/blob/main/src/config/constants.js"
    click testInt "https://github.com/sisovin/redis-cache-aside-pattern/tree/main/tests/integration/"
    click testUnit "https://github.com/sisovin/redis-cache-aside-pattern/tree/main/tests/unit/"

    %% Styles
    classDef app fill:#B3E5FC,stroke:#0288D1,stroke-width:2px;
    classDef routes fill:#C8E6C9,stroke:#388E3C,stroke-width:2px;
    classDef ctrl fill:#FFF9C4,stroke:#FBC02D,stroke-width:2px;
    classDef service fill:#FFCCBC,stroke:#D84315,stroke-width:2px;
    classDef middleware fill:#D7CCC8,stroke:#5D4037,stroke-width:2px;
    classDef model fill:#F8BBD0,stroke:#C2185B,stroke-width:2px;
    classDef util fill:#E1BEE7,stroke:#8E24AA,stroke-width:2px;
    classDef config fill:#DCEDC8,stroke:#689F38,stroke-width:2px;
    classDef externalSys fill:#FFECB3,stroke:#FFA000,stroke-width:2px;
    classDef test fill:#CFD8DC,stroke:#607D8B,stroke-width:2px;
    classDef client fill:#FFE0B2,stroke:#FB8C00,stroke-width:2px;
```
