# foogaro/redis-spring-jpa architecture

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

- Interactive diagram: https://gitdiagram.com/foogaro/redis-spring-jpa
- Repository: https://github.com/foogaro/redis-spring-jpa

## Overview

##### Project type and purpose
- This is a single-module Spring Boot microservice (monolith) that demonstrates using Spring Data JPA against MySQL for persistence and Spring Data Redis as a distributed cache.
- It exposes REST endpoints (via `ActorController`) to create, read, update, and delete `Actor` entities.
- The core goal is to illustrate classic caching patterns (Cache-Aside, Read-Through, Write-Through, Write-Behind) with Redis.

##### File structure
- Top-level artifacts:
- Dockerfile + docker-compose.yaml → container definitions for the app, MySQL and Redis
- pom.xml → Maven build and dependencies (Spring Boot, Spring Data JPA, Spring Data Redis, MySQL connector, Jedis)
- curl-commands.txt → sample client interactions
- Source hierarchy (`src/main/java/com/foogaro/data`):
- config/RedisConfig.java → Redis connection factory & cache manager setup
- controllers/ActorController.java → REST API layer
- services/ActorService.java → business logic + `@Cacheable`, `@CachePut`, `@CacheEvict` annotations
- repositories/ActorRepository.java → Spring Data JPA interface to MySQL
- models/Actor.java → JPA entity
- Resources: `application.properties` configures Spring Cache to use Redis, and MySQL connection.

##### Extract insights from the README
- The system is deployed via Docker Compose: one container for the Spring Boot app, one for MySQL, one for Redis.
- Emphasis on sub-millisecond response times by offloading frequent reads/writes to Redis.

- Clients (e.g. curl, REST clients, Postman)
- Spring Boot Application (container)
- Controller layer
- Service layer (caching annotations)
- Repository layer (JPA to MySQL)
- Configuration module (RedisConfig)
- Redis Cache (container)
- MySQL Database (container)
b. Relationships & data flows:
- HTTP → Controller → Service
- Service → Redis (check cache on read, write on save)
- Service → JPA Repository → MySQL (fallback on cache miss)
- Redis ↔ MySQL (in Read-Through/Write-Through/Behind patterns)
c. Architectural patterns & principles:
- Layered architecture (Controller → Service → Repository)
- Repository pattern via Spring Data JPA
- Cache-Aside and other caching patterns
- Configuration-as-code for containerization (Dockerfile + Compose)
d. Key technologies & frameworks:
- Java 17 (or configured JDK) + Spring Boot
- Spring Data JPA, Hibernate, MySQL
- Spring Data Redis, Jedis client, RedisGears (optional)
- Docker & Docker Compose

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% External Clients
    Clients["Clients\n(cURL, Postman)"]:::external

    %% Docker Compose Network
    subgraph "Docker Compose Network"
        direction TB

        %% App Container
        subgraph "App Container\n(Spring Boot App)" 
            direction TB
            AppMain["RedisSpringJPAApplication"]:::app
            Controller["ActorController\n(HTTP REST)"]:::controller
            Service["ActorService\n(@Cacheable, @CachePut, @CacheEvict)"]:::service
            Repository["ActorRepository\n(JPA Repository)"]:::repository
            Config["RedisConfig\n(ConnectionFactory & CacheManager)"]:::config
            Properties["application.properties"]:::config
            pomFile["pom.xml"]:::artifact
        end

        %% Redis Container
        RedisCache["Redis Cache\n(Redis Container)"]:::cache

        %% MySQL Container
        MySQLDB["MySQL DB\n(MySQL Container)"]:::db

        %% Docker Config Artifacts
        Dockerfile["Dockerfile"]:::artifact
        Compose["docker-compose.yaml"]:::artifact
    end

    %% Sample Interactions & Images
    curlCmd["curl-commands.txt"]:::artifact
    imagesDir["images/"]:::artifact

    %% Relationships
    Clients -->|HTTP Request| Controller
    Controller -->|calls| Service
    Service -->|checks cache| RedisCache
    RedisCache -->|cache miss| Service
    Service -->|queries| Repository
    Repository -->|SQL| MySQLDB
    MySQLDB -->|data| Repository
    Repository -->|returns| Service
    Service -->|updates cache| RedisCache
    Config -->|configures| RedisCache
    AppMain -->|bootstraps| Controller
    AppMain -->|bootstraps| Service
    AppMain -->|bootstraps| Repository

    %% Click Events
    click Dockerfile "https://github.com/foogaro/redis-spring-jpa/tree/main/Dockerfile"
    click Compose "https://github.com/foogaro/redis-spring-jpa/blob/main/docker-compose.yaml"
    click pomFile "https://github.com/foogaro/redis-spring-jpa/blob/main/pom.xml"
    click AppMain "https://github.com/foogaro/redis-spring-jpa/blob/main/src/main/java/com/foogaro/data/RedisSpringJPAApplication.java"
    click Controller "https://github.com/foogaro/redis-spring-jpa/blob/main/src/main/java/com/foogaro/data/controllers/ActorController.java"
    click Service "https://github.com/foogaro/redis-spring-jpa/blob/main/src/main/java/com/foogaro/data/services/ActorService.java"
    click Repository "https://github.com/foogaro/redis-spring-jpa/blob/main/src/main/java/com/foogaro/data/repositories/ActorRepository.java"
    click Config "https://github.com/foogaro/redis-spring-jpa/blob/main/src/main/java/com/foogaro/data/config/RedisConfig.java"
    click Properties "https://github.com/foogaro/redis-spring-jpa/blob/main/src/main/resources/application.properties"
    click curlCmd "https://github.com/foogaro/redis-spring-jpa/blob/main/curl-commands.txt"
    click imagesDir "https://github.com/foogaro/redis-spring-jpa/tree/main/images/"

    %% Styles
    classDef external fill:#f9f,stroke:#333,stroke-width:1px
    classDef container fill:#eef,stroke:#3366ff,stroke-width:2px,stroke-dasharray: 5 5
    classDef app fill:#ccf,stroke:#3366ff,stroke-width:1px
    classDef controller fill:#cfc,stroke:#28a745,stroke-width:1px
    classDef service fill:#ffc,stroke:#ffcc00,stroke-width:1px
    classDef repository fill:#fcc,stroke:#dc3545,stroke-width:1px
    classDef config fill:#cff,stroke:#17a2b8,stroke-width:1px
    classDef cache fill:#ffe6cc,stroke:#ff8800,stroke-width:1px
    classDef db fill:#e6e6fa,stroke:#6f42c1,stroke-width:1px
    classDef artifact fill:#ddd,stroke:#999,stroke-width:1px,stroke-dasharray: 2 2
```
