# 23520904/deepreader architecture

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

- Interactive diagram: https://gitdiagram.com/23520904/deepreader
- Repository: https://github.com/23520904/deepreader

## Overview

1. **Overall shape**
   DeepReader is a polyglot, service-oriented platform. The browser talks to a single public entry point (`web-module`), which then delegates to backend services specialized by responsibility: orchestration (`business-service`), AI processing (`ai-service`), persistence (`data-service`), and vector search bridging (`haystack-service`).

2. **Frontend boundary**
   The `frontend/` app is a Next.js 16 + React 19 client that owns all user-facing screens: auth, library, reading, flashcards, profile, admin, help, and landing pages. It does not talk directly to internal services; instead it uses typed API clients under `src/services/` to call the gateway.

3. **Public API gateway**
   `web-module` is the external HTTP boundary on port `8083`. It handles JWT auth, refresh-token sessions, CORS, rate limiting, security headers, and role-based access. It exposes the public routes for auth, books, summaries, flashcards, study progress, profile, and admin views, and forwards most book-related work to `business-service`.

4. **Orchestration layer**
   `business-service` is the coordination layer on port `8082`. Its main job is to fan requests out to `ai-service` and `data-service`, publish and consume book events via Kafka, and manage higher-level library workflows. This is where upload/ingestion sequencing and domain event routing are centralized.

5. **AI processing engine**
   `ai-service` is the core AI subsystem on port `8080`. It owns document ingestion, chunking, embedding, RAG chat, summary generation, flashcard generation, PDF vision/OCR, and object storage access. It also enforces AI-specific guardrails and runs background jobs for ingestion polling and retention cleanup.

6. **Ingestion data flow**
   The upload pipeline is asynchronous: a user uploads a PDF/EPUB through the gateway, orchestration starts an ingestion job, `ai-service` extracts text, chunks it, embeds it, and writes vectors into Qdrant through `haystack-service`. After processing, job status is updated and the resulting book/chapter metadata is persisted through `data-service`.

7. **Retrieval and chat flow**
   For chat and semantic search, `ai-service` embeds the query, retrieves top chunks from Qdrant, builds a grounded prompt, and asks either Gemini or Groq to generate the answer. The frontend shows source citations using the returned retrieved chunks, while chat history is stored separately in the data layer.

8. **Persistence boundary**
   `data-service` is the system of record for application data on port `8081`. It splits storage by model type: MongoDB holds books, chapters, summaries, flashcards, chat history, and reading sessions; PostgreSQL holds relational user accounts. This service isolates persistence concerns from both the gateway and the AI pipeline.

9. **Vector search bridge**
   `haystack-service` is a thin Python FastAPI service on port `8000`. It exists to wrap Haystack’s Qdrant document store and retriever, exposing `/ingest` and `/search` so the Java AI service can offload vector store interaction without embedding Haystack logic into the JVM service.

10. **Shared infrastructure and state**
   Redis is used for short-lived operational state: JWT refresh sessions in `web-module`, rate-limit counters in both gateway and AI service, and daily guardrail counters in `ai-service`. PostgreSQL also stores ingestion jobs, audit logs, and dead-letter metadata. Optional object storage keeps uploaded source documents outside the services when enabled.

11. **Async events and resilience**
   Kafka is the cross-service event bus, with `deepreader.book.events` as the main topic and a dead-letter topic for failed handling. `business-service` publishes and routes these events via Apache Camel and Spring Kafka, supporting decoupled workflows and operational recovery.

12. **Operational and security boundaries**
   Security is layered: JWT-based authentication, refresh token rotation, admin-only routes, per-IP rate limiting, and configurable daily usage caps. Observability is built into all Java services via Micrometer/Prometheus, with Grafana dashboards and alert rules in `observability/`. This makes the system diagram naturally split into user entry, orchestration, AI, persistence, search, and shared runtime services.

Main entry points: [`web-module`](https://github.com/23520904/deepreader/tree/master/web-module) (Web Module), [`ai-service`](https://github.com/23520904/deepreader/tree/master/ai-service) (AI Service), [`business-service`](https://github.com/23520904/deepreader/tree/master/business-service) (Business Service), [`data-service`](https://github.com/23520904/deepreader/tree/master/data-service) (Data Service)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Client"]
  node_frontend["Frontend<br/>Next.js client"]
  node_frontend_services["API Clients<br/>typed clients"]
end

subgraph group_gateway["Gateway"]
  node_web_module["Web Module<br/>HTTP gateway"]
  node_web_auth["Auth Core<br/>JWT/session"]
  node_web_public["Public Routes<br/>gateway controllers"]
  node_web_security["Security Filters<br/>web filters"]
end

subgraph group_orchestration["Orchestration"]
  node_business_service["Business Service<br/>orchestration svc"]
  node_library_orchestration["Library Orchestration<br/>workflow service"]
  node_book_events["Book Events<br/>Kafka routing"]
end

subgraph group_ai["AI Processing"]
  node_ai_service["AI Service<br/>AI engine"]
  node_ingestion_pipeline["Ingestion Pipeline<br/>document processing"]
  node_retrieval_generation["Retrieval & Chat<br/>RAG workflow"]
  node_vision_module["Vision Module<br/>pdf OCR"]
  node_ai_workers["Background Jobs<br/>worker jobs"]
end

subgraph group_data["Persistence"]
  node_data_service["Data Service<br/>persistence svc"]
  node_library_data[("Library Data<br/>Mongo persistence")]
  node_user_accounts[("User Accounts<br/>PostgreSQL access")]
  node_core_models["Core Models<br/>shared domain"]
end

subgraph group_infra["Infrastructure"]
  node_haystack_service["Haystack Service<br/>vector bridge"]
  node_qdrant[("Qdrant<br/>vector store")]
  node_redis[("Redis<br/>session/cache")]
  node_postgres[("PostgreSQL<br/>relational db")]
  node_mongo[("MongoDB<br/>document db")]
  node_kafka["Kafka<br/>event bus"]
  node_object_storage[("Object Storage<br/>blob store")]
end

node_frontend -->|"uses"| node_frontend_services
node_frontend_services -->|"HTTP API"| node_web_module
node_web_module -->|"secures"| node_web_security
node_web_module -->|"authenticates"| node_web_auth
node_web_module -->|"serves"| node_web_public
node_web_module -->|"delegates"| node_business_service
node_web_module -->|"profiles"| node_data_service
node_business_service -->|"coordinates"| node_library_orchestration
node_business_service -->|"publishes"| node_book_events
node_book_events -->|"streams"| node_kafka
node_business_service -->|"calls"| node_ai_service
node_business_service -->|"persists"| node_data_service
node_ai_service -->|"runs"| node_ingestion_pipeline
node_ai_service -->|"answers"| node_retrieval_generation
node_ai_service -->|"extracts"| node_vision_module
node_ai_service -->|"schedules"| node_ai_workers
node_ingestion_pipeline -->|"indexes"| node_haystack_service
node_retrieval_generation -->|"searches"| node_haystack_service
node_haystack_service -->|"stores"| node_qdrant
node_ai_service -->|"limits"| node_redis
node_web_module -->|"sessions"| node_redis
node_data_service -->|"stores"| node_mongo
node_data_service -->|"stores"| node_postgres
node_ai_service -->|"reads/writes"| node_object_storage
node_data_service -->|"shares"| node_core_models
node_web_module -.->|"observes"| node_kafka

click node_frontend "https://github.com/23520904/deepreader/tree/master/frontend"
click node_frontend_services "https://github.com/23520904/deepreader/tree/master/frontend/src/services"
click node_web_module "https://github.com/23520904/deepreader/tree/master/web-module"
click node_web_auth "https://github.com/23520904/deepreader/tree/master/web-module/src/main/java/com/deepreader/web_module/service"
click node_web_public "https://github.com/23520904/deepreader/tree/master/web-module/src/main/java/com/deepreader/web_module/controller"
click node_web_security "https://github.com/23520904/deepreader/tree/master/web-module/src/main/java/com/deepreader/web_module/config"
click node_business_service "https://github.com/23520904/deepreader/tree/master/business-service"
click node_library_orchestration "https://github.com/23520904/deepreader/tree/master/business-service/src/main/java/com/deepreader/business_service/service"
click node_book_events "https://github.com/23520904/deepreader/tree/master/business-service/src/main/java/com/deepreader/business_service/event"
click node_ai_service "https://github.com/23520904/deepreader/tree/master/ai-service"
click node_ingestion_pipeline "https://github.com/23520904/deepreader/tree/master/ai-service/src/main/java/com/deepreader/ai_service/service"
click node_retrieval_generation "https://github.com/23520904/deepreader/tree/master/ai-service/src/main/java/com/deepreader/ai_service/service"
click node_vision_module "https://github.com/23520904/deepreader/tree/master/ai-service/src/main/java/com/deepreader/ai_service/service"
click node_ai_workers "https://github.com/23520904/deepreader/tree/master/ai-service/src/main/java/com/deepreader/ai_service/startup"
click node_data_service "https://github.com/23520904/deepreader/tree/master/data-service"
click node_library_data "https://github.com/23520904/deepreader/tree/master/data-service/src/main/java/com/deepreader/data_service/service"
click node_user_accounts "https://github.com/23520904/deepreader/tree/master/data-service/src/main/java/com/deepreader/data_service/service"
click node_core_models "https://github.com/23520904/deepreader/tree/master/core/src/main/java/com/deepreader/core/model"
click node_haystack_service "https://github.com/23520904/deepreader/tree/master/haystack-service"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_frontend,node_frontend_services toneBlue
class node_web_module,node_web_auth,node_web_public,node_web_security toneAmber
class node_business_service,node_library_orchestration,node_book_events toneMint
class node_ai_service,node_ingestion_pipeline,node_retrieval_generation,node_vision_module,node_ai_workers toneRose
class node_data_service,node_library_data,node_user_accounts,node_core_models toneIndigo
class node_haystack_service,node_qdrant,node_redis,node_postgres,node_mongo,node_kafka,node_object_storage toneTeal
```

## Components

### Client

User-facing app and browser-side service clients

- [Frontend](https://github.com/23520904/deepreader/tree/master/frontend): Next.js client · Browser UI for auth, library, reading, flashcards, profile, admin, and help screens · `frontend`
- [API Clients](https://github.com/23520904/deepreader/tree/master/frontend/src/services): typed clients · Typed service layer used by the frontend to call the gateway · `frontend/src/services`

### Gateway

Public HTTP boundary and auth/session control

- [Web Module](https://github.com/23520904/deepreader/tree/master/web-module): HTTP gateway · Public entry point handling auth, sessions, security, and route forwarding · `web-module`
- [Auth Core](https://github.com/23520904/deepreader/tree/master/web-module/src/main/java/com/deepreader/web_module/service): JWT/session · JWT issuance, refresh sessions, OAuth, and user context · `web-module/src/main/java/com/deepreader/web_module/service`
- [Public Routes](https://github.com/23520904/deepreader/tree/master/web-module/src/main/java/com/deepreader/web_module/controller): gateway controllers · Public endpoints for books, study progress, profile, admin, and reading flows · `web-module/src/main/java/com/deepreader/web_module/controller`
- [Security Filters](https://github.com/23520904/deepreader/tree/master/web-module/src/main/java/com/deepreader/web_module/config): web filters · CORS, rate limiting, auth filter, and security config · `web-module/src/main/java/com/deepreader/web_module/config`

### Orchestration

Business workflow coordination and event routing

- [Business Service](https://github.com/23520904/deepreader/tree/master/business-service): orchestration svc · Coordinates library workflows and cross-service calls · `business-service`
- [Library Orchestration](https://github.com/23520904/deepreader/tree/master/business-service/src/main/java/com/deepreader/business_service/service): workflow service · Fanning requests to AI and data services for uploads, summaries, flashcards, and deletes · `business-service/src/main/java/com/deepreader/business_service/service`
- [Book Events](https://github.com/23520904/deepreader/tree/master/business-service/src/main/java/com/deepreader/business_service/event): Kafka routing · Publishes and consumes domain events for library workflows · `business-service/src/main/java/com/deepreader/business_service/event`

### AI Processing

Ingestion, retrieval, generation, and vision

- [AI Service](https://github.com/23520904/deepreader/tree/master/ai-service): AI engine · Document ingestion, retrieval, generation, OCR, and AI guardrails · `ai-service`
- [Ingestion Pipeline](https://github.com/23520904/deepreader/tree/master/ai-service/src/main/java/com/deepreader/ai_service/service): document processing · Text extraction, chunking, embeddings, and ingestion job tracking · `ai-service/src/main/java/com/deepreader/ai_service/service`
- [Retrieval & Chat](https://github.com/23520904/deepreader/tree/master/ai-service/src/main/java/com/deepreader/ai_service/service): RAG workflow · Query embedding, vector retrieval, grounded prompt building, and LLM response generation · `ai-service/src/main/java/com/deepreader/ai_service/service`
- [Vision Module](https://github.com/23520904/deepreader/tree/master/ai-service/src/main/java/com/deepreader/ai_service/service): pdf OCR · PDF rasterization, embedded image extraction, and vision-assisted processing · `ai-service/src/main/java/com/deepreader/ai_service/service`
- [Background Jobs](https://github.com/23520904/deepreader/tree/master/ai-service/src/main/java/com/deepreader/ai_service/startup): worker jobs · Ingestion queue polling and retention cleanup tasks · `ai-service/src/main/java/com/deepreader/ai_service/startup`

### Persistence

System-of-record services and repositories

- [Data Service](https://github.com/23520904/deepreader/tree/master/data-service): persistence svc · System of record for books, chapters, summaries, flashcards, sessions, and user accounts · `data-service`
- [Library Data](https://github.com/23520904/deepreader/tree/master/data-service/src/main/java/com/deepreader/data_service/service): Mongo persistence · Repository-backed storage for library and study content · `data-service/src/main/java/com/deepreader/data_service/service`
- [User Accounts](https://github.com/23520904/deepreader/tree/master/data-service/src/main/java/com/deepreader/data_service/service): PostgreSQL access · Relational user account persistence and profile storage · `data-service/src/main/java/com/deepreader/data_service/service`
- [Core Models](https://github.com/23520904/deepreader/tree/master/core/src/main/java/com/deepreader/core/model): shared domain · Shared domain entities used across services · `core/src/main/java/com/deepreader/core/model`

### Infrastructure

Shared runtime and external backing services

- [Haystack Service](https://github.com/23520904/deepreader/tree/master/haystack-service): vector bridge · FastAPI wrapper around Haystack/Qdrant search and ingest endpoints · `haystack-service`
- **Qdrant**: vector store · Vector index used for semantic retrieval
- **Redis**: session/cache · Refresh sessions, rate limits, and daily counters
- **PostgreSQL**: relational db · User accounts, jobs, audit logs, and operational metadata
- **MongoDB**: document db · Books, chapters, summaries, flashcards, chat history, and reading sessions
- **Kafka**: event bus · Cross-service book event stream with dead-letter handling
- **Object Storage**: blob store · Optional storage for uploaded source documents

## Connections

- Frontend → API Clients: uses · UI calls typed API clients
- API Clients → Web Module: HTTP API · Browser traffic goes to the public gateway
- Web Module → Security Filters: secures · Applies auth, rate limiting, and security filters
- Web Module → Auth Core: authenticates · Issues and validates tokens and sessions
- Web Module → Public Routes: serves · Exposes public and role-based routes
- Web Module → Business Service: delegates · Forwards library workflow requests
- Web Module → Data Service: profiles · Direct calls for user/account-related views
- Business Service → Library Orchestration: coordinates · Central workflow coordination for books and study content
- Business Service → Book Events: publishes · Emits and routes book domain events
- Book Events → Kafka: streams · Uses Kafka topic transport
- Business Service → AI Service: calls · Triggers AI work for ingestion, summaries, flashcards, and chat
- Business Service → Data Service: persists · Writes and reads application state
- AI Service → Ingestion Pipeline: runs · Processes uploaded documents into chunks and embeddings
- AI Service → Retrieval & Chat: answers · Builds grounded RAG responses and summaries
- AI Service → Vision Module: extracts · Handles PDF OCR and image-based processing
- AI Service → Background Jobs: schedules · Background ingestion polling and cleanup jobs
- Ingestion Pipeline → Haystack Service: indexes · Pushes vectors for storage and retrieval
- Retrieval & Chat → Haystack Service: searches · Retrieves top chunks for semantic answers
- Haystack Service → Qdrant: stores · Wraps the vector database interaction
- AI Service → Redis: limits · Uses cache for AI counters and rate limits
- Web Module → Redis: sessions · Stores refresh tokens and session state
- Data Service → MongoDB: stores · Persists content and study data
- Data Service → PostgreSQL: stores · Persists user accounts and relational metadata
- AI Service → Object Storage: reads/writes · Accesses uploaded source documents when enabled
- Data Service → Core Models: shares · Reuses shared domain entities
- Web Module → Kafka: observes · Receives or forwards user-facing workflow signals indirectly through services
