# isaacafrifa/user-service architecture

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

- Interactive diagram: https://gitdiagram.com/isaacafrifa/user-service
- Repository: https://github.com/isaacafrifa/user-service

## Overview

##### Main Components
- BookMe User Service Application: The central microservice implemented in Java Spring Boot.
- API/Controller Layer: The UserController and the REST API endpoints through which client requests are received.
- Service Layer: Contains business logic (UserService, UserValidationService) and orchestrates interactions between components.
- Persistence Layer: The UserRepository along with the database interactions to a PostgreSQL database (configured with migration scripts in src/main/resources/db/migration).
- Configuration Components: A collection of configuration classes for CORS, RabbitMQ, Hazelcast caching (CacheConfig, CacheListener), observation/tracing, etc.
- Messaging System: Integration with RabbitMQ (as seen with CustomMessage, RabbitMQConfig) for event-driven communication (for example, publishing events like UserEmailUpdatedEvent).
- Caching: Hazelcast configuration is used for improved performance (hazelcast.yaml and hazelcast-prod.yaml).
- External Integrations and Observability:
- Test containers and automated tests for integration validation.
- Logging (logback-spring.xml), distributed tracing (Micrometer), Prometheus for monitoring, and Loki for log aggregation.
- Deployment and Orchestration:
- Docker Compose, Kubernetes manifests (in the k8s folder), and Maven build support with spring-boot and cloud-native buildpacks along with a Dockerfile.
- automation scripts like mvnw and the Maven Wrapper.

##### Relationships and Data Flow
- Incoming HTTP Requests are handled by the UserController, which forwards them to the business logic layer (UserService).
- The Service Layer processes business rules and interacts with the persistence layer by calling the UserRepository to read/write user data from PostgreSQL.
- For caching, after data is processed or fetched, the CacheConfig (and listeners) help cache responses using Hazelcast.
- When significant changes occur (for example, a user’s email update), an event such as UserEmailUpdatedEvent is published through RabbitMQ (configured by RabbitMQConfig and using CustomMessage) to notify other parts of the application.
- Exception Handling: Application-wide exceptions are managed by the ExceptionController and specific exception classes to ensure robust error handling.
- Observability and Metrics: The ObservationConfig along with Micrometer tracing, Prometheus, and Loki log management capture metrics and monitor application health.
- The project also includes infrastructure scripts/configuration for Kubernetes and Docker Compose, which help in deploying the service, along with a Cloud-Native build approach.

##### Incorporate Architectural Patterns and Design Principles
- Layered Architecture: Clearly separate Controllers, Services, Repositories, and Configuration.
- Event-Driven Communication: Use the RabbitMQ configuration to decouple components.
- DevOps and Cloud-Native Practices: Containerization with Docker, automated database migrations with Flyway, and orchestrations with Kubernetes.
- Observability and Resilience: Integration with monitoring and distributed tracing tools for robust production support.

## Diagram (Mermaid)

```mermaid
graph TD
    subgraph "Application Core"
        A["BookMe User Service Application"]:::application
    end

    subgraph "API Layer"
        B["UserController (API)"]:::api
    end

    subgraph "Service Layer"
        C["UserService (Business Logic)"]:::service
        D["UserValidationService"]:::service
    end

    subgraph "Persistence Layer"
        E["UserRepository (Persistence)"]:::repository
        V["Flyway Migration (V1__init.sql)"]:::repository
        F["PostgreSQL DB (via Flyway)"]:::database
    end

    subgraph "Messaging & Events"
        H["RabbitMQ (Event Bus)"]:::messaging
        I["UserEmailUpdatedEvent"]:::messaging
        N["RabbitMQConfig"]:::configuration
        O["CustomMessage"]:::messaging
    end

    subgraph "Caching"
        G["Cache (Hazelcast)"]:::cache
        W["Hazelcast Config (hazelcast.yaml)"]:::cache
        X["Hazelcast Prod Config (hazelcast-prod.yaml)"]:::cache
    end

    subgraph "Configuration & Integration"
        J["CorsConfig"]:::configuration
        K["CacheConfig"]:::configuration
        L["CacheListener"]:::configuration
        M["ObservationConfig"]:::observability
    end

    subgraph "Exception Handling"
        P["ExceptionController"]:::exception
        Q["APIError"]:::exception
        R["EventPublishingException"]:::exception
        S["ResourceAlreadyExistsException"]:::exception
        T["ResourceNotFoundException"]:::exception
        U["UserOptimisticLockException"]:::exception
    end

    subgraph "Deployment & Orchestration"
        Y["Kubernetes Manifests (user-app.yaml, user-db.yaml, user-storage.yaml)"]:::deployment
        Z["Docker & Build (Dockerfile, compose.yml, mvnw, mvnw.cmd, pom.xml)"]:::deployment
    end

    A -->|"starts"| B
    B -->|"calls"| C
    C -->|"uses"| D
    C -->|"invokes"| E
    E -->|"reads/writes"| F
    V -->|"migrates"| F
    C -->|"updates"| G
    C -->|"publishes"| H
    H -->|"delivers"| I
    N -->|"configures"| H
    O -->|"supports"| H
    A -->|"configures"| J
    A -->|"configures"| K
    A -->|"configures"| L
    A -->|"observes"| M
    B -->|"error_handling"| P
    C -->|"error_handling"| P
    P -->|"raises"| Q
    P -->|"raises"| R
    P -->|"raises"| S
    P -->|"raises"| T
    P -->|"raises"| U
    A -->|"deployed_on"| Y
    A -->|"built_with"| Z

    classDef application fill:#FFCCCC,stroke:#660000,stroke-width:2px;
    classDef api fill:#CCFFCC,stroke:#006600,stroke-width:2px;
    classDef service fill:#CCCCFF,stroke:#000066,stroke-width:2px;
    classDef repository fill:#FFFFCC,stroke:#666600,stroke-width:2px;
    classDef configuration fill:#CCE5FF,stroke:#003366,stroke-width:2px;
    classDef messaging fill:#FFEECC,stroke:#CC6600,stroke-width:2px;
    classDef database fill:#E6FFE6,stroke:#009900,stroke-width:2px;
    classDef exception fill:#FFE6E6,stroke:#990000,stroke-width:2px;
    classDef deployment fill:#E6E6FF,stroke:#333399,stroke-width:2px;
    classDef cache fill:#FFF0F5,stroke:#CC3366,stroke-width:2px;
    classDef observability fill:#F0FFF0,stroke:#336633,stroke-width:2px;

click A "https://github.com/isaacafrifa/user-service/blob/main/src/main/java/iam/userservice/UserServiceApplication.java"
click B "https://github.com/isaacafrifa/user-service/blob/main/src/main/java/iam/userservice/controller/UserController.java"
click C "https://github.com/isaacafrifa/user-service/blob/main/src/main/java/iam/userservice/service/UserService.java"
click D "https://github.com/isaacafrifa/user-service/blob/main/src/main/java/iam/userservice/service/UserValidationService.java"
click E "https://github.com/isaacafrifa/user-service/blob/main/src/main/java/iam/userservice/repository/UserRepository.java"
click V "https://github.com/isaacafrifa/user-service/blob/main/src/main/resources/db/migration/V1__init.sql"
click J "https://github.com/isaacafrifa/user-service/blob/main/src/main/java/iam/userservice/config/CorsConfig.java"
click K "https://github.com/isaacafrifa/user-service/blob/main/src/main/java/iam/userservice/config/CacheConfig.java"
click L "https://github.com/isaacafrifa/user-service/blob/main/src/main/java/iam/userservice/config/CacheListener.java"
click M "https://github.com/isaacafrifa/user-service/blob/main/src/main/java/iam/userservice/config/ObservationConfig.java"
click N "https://github.com/isaacafrifa/user-service/blob/main/src/main/java/iam/userservice/config/rabbitmq/RabbitMQConfig.java"
click O "https://github.com/isaacafrifa/user-service/blob/main/src/main/java/iam/userservice/config/rabbitmq/CustomMessage.java"
click I "https://github.com/isaacafrifa/user-service/blob/main/src/main/java/iam/userservice/events/UserEmailUpdatedEvent.java"
click W "https://github.com/isaacafrifa/user-service/blob/main/src/main/resources/hazelcast.yaml"
click X "https://github.com/isaacafrifa/user-service/blob/main/src/main/resources/hazelcast-prod.yaml"
click P "https://github.com/isaacafrifa/user-service/blob/main/src/main/java/iam/userservice/exception/ExceptionController.java"
click Q "https://github.com/isaacafrifa/user-service/blob/main/src/main/java/iam/userservice/exception/APIError.java"
click R "https://github.com/isaacafrifa/user-service/blob/main/src/main/java/iam/userservice/exception/EventPublishingException.java"
click S "https://github.com/isaacafrifa/user-service/blob/main/src/main/java/iam/userservice/exception/ResourceAlreadyExistsException.java"
click T "https://github.com/isaacafrifa/user-service/blob/main/src/main/java/iam/userservice/exception/ResourceNotFoundException.java"
click U "https://github.com/isaacafrifa/user-service/blob/main/src/main/java/iam/userservice/exception/UserOptimisticLockException.java"
click Y "https://github.com/isaacafrifa/user-service/blob/main/k8s/user-app.yaml"
click Z "https://github.com/isaacafrifa/user-service/tree/main/Dockerfile"
```
