Zing Forum

Reading

TranquilAI: A Mental Health AI Assistant System Based on Microservices Architecture

TranquilAI is a mental health AI assistant backend system built with Spring Boot and Kotlin, designed with a microservices architecture. It integrates the Gemini API to provide intelligent dialogue and insight functions, supporting subscription management and complete user activity tracking.

微服务架构心理健康Spring BootKotlinAI助手Gemini API移动应用后端订阅管理
Published 2026-06-02 06:11Recent activity 2026-06-02 06:21Estimated read 6 min
TranquilAI: A Mental Health AI Assistant System Based on Microservices Architecture
1

Section 01

TranquilAI: Overview of a Microservices-based Mental Health AI Assistant Backend

TranquilAI is a mental health AI assistant backend system built with Spring Boot and Kotlin, adopting a microservices architecture. It integrates the Gemini API to provide intelligent dialogue and insight functions, supporting subscription management and full user activity tracking. The project is maintained by Zacle, hosted on GitHub (original link: https://github.com/Zacle/TranquilaiSpring), and was released on June 1, 2026.

2

Section 02

Project Background: Addressing Mental Health Accessibility Gaps

Mental health issues are gaining global attention, but professional counseling faces accessibility barriers (high cost, geographic restrictions, social stigma). TranquilAI aims to alleviate these issues via technology: using AI to provide emotional support, mental health pattern tracking, and actionable insights while ensuring privacy and accessibility. The backend uses Spring Boot and Kotlin for scalability and maintainability, with a supporting Android client.

3

Section 03

Architecture & Service Division: Microservices with Gateway Pattern

TranquilAI uses a classic microservices architecture centered around an API gateway (Spring Cloud Gateway, port 8080). The gateway acts as the only public entry, handling CORS, JWT validation, rate limiting, request routing, and blocking external access to internal API paths (/internal/**). The system includes 10 independent services, each with its own data storage and clear responsibilities. Services communicate via internal APIs using X-Internal-Key for authentication. Key services include auth-service (user lifecycle management), user-service (profile/mental health data), ai-service (intelligent dialogue/diary), etc.

4

Section 04

Data Storage Strategy: Multi-Database Approach

TranquilAI employs a multi-database strategy:

  • PostgreSQL: For structured data (e.g., user credentials, profiles), with separate databases per service for isolation.
  • MongoDB: For AI service's unstructured data (dialogue history, AI-generated docs).
  • Redis: For token caching (auth), rate limit counters (gateway), and subscription entitlement caching.
  • RabbitMQ: For asynchronous tasks (e.g., AI chat side effects, activity analysis) to improve responsiveness.
5

Section 05

AI Integration & Business Model: Gemini API & Freemium

AI Integration: Uses Google's Gemini API (configured via environment variables for key/URL). AI-service handles open dialogue and diary assistance; plan-service uses Gemini to generate personalized health plans based on user data. Business Model: Freemium via Google Play. Backend processes:

  1. Purchase validation (verifying Google Play tokens).
  2. Real-time Developer Notifications (RTDN) for subscription state changes.
  3. Entitlement checks for feature access.
  4. Usage limits for free users (AI dialogues, diary entries) to encourage upgrades.
6

Section 06

Security Design & Tech Stack Highlights

Security:

  • JWT-based authentication with role-based access control.
  • Service-to-service auth via X-Internal-Key.
  • Data isolation per service (separate databases).
  • HTTPS for all public traffic. Tech Stack Highlights:
  • Kotlin 2.0.21 (concise syntax, null safety).
  • Java 21 (LTS runtime).
  • Spring Boot3.4.1, Spring Cloud Gateway2024.0.0.
  • Spring Data (JPA, MongoDB), Flyway (DB versioning), Docker Compose (local dev orchestration).
7

Section 07

Conclusion & Key Takeaways

TranquilAI demonstrates a complete backend architecture for a mental health app. Its microservices design follows clear domain boundaries; multi-database strategy matches data types to storage solutions; AI integration focuses on user value (not tech showcase). Key takeaways:

  • Balances tech assistance with professional help (e.g., emergency contacts, resource guidance).
  • Serves as a reference for building similar mental health apps or learning Spring-based microservices.
  • Emphasizes privacy and security for sensitive mental health data.