# DIDI Backend - Data Layer ๐Ÿ—„๏ธ ## Quick Start ๐Ÿš€ ```bash # Recommended: Start via unified deployment manager ./deploy/didi.sh staging start ``` All data services start automatically as part of the staging deployment. ## What is the Data Layer? ๐Ÿค” The Data Layer provides **pure storage** for the DIDI Backend platform. No business logic, no routing, just reliable data storage. ## Architecture Overview ๐Ÿ“Š ``` โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ DATA LAYER โ”‚ โ”‚ โ”‚ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ PostgreSQL โ”‚ โ”‚ Redis โ”‚ โ”‚ MinIO โ”‚ โ”‚ RabbitMQ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ Database โ”‚ โ”‚ Cache โ”‚ โ”‚ Storage โ”‚ โ”‚ Queue โ”‚ โ”‚ โ”‚ โ”‚ PgAdmin โ”‚ โ”‚ Commander โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ”‚ Ports: Ports: Ports: Ports: โ”‚ โ”‚ 22001 (DB) 22301 (Cache) 27000 (API) 23100 (AMQP) โ”‚ โ”‚ 29001 (UI) 29002 (UI) 27001 (Console) 23101 (UI) โ”‚ โ”‚ โ”‚ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ โ”‚ โ”‚ Network: didi-backend (shared by ALL services) โ”‚ โ”‚ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ ``` > **Note**: Ports shown are for staging (the primary deployment method via `./deploy/didi.sh staging`). ## The Four Services + Management UIs ๐Ÿ“ฆ ### 1. **didiDatabase** (PostgreSQL) ๐Ÿ˜ **Purpose**: Persistent structured data storage - Stores all application data - 5 schemas (users, catalog, pipelines, execution, analyses) - ACID compliant transactions - Port: **22001** (staging) - **pgAdmin** (Port 29001): Web UI for database management ### 2. **didiCache** (Redis) โšก **Purpose**: High-speed temporary storage - Pipeline execution status - Real-time updates - Session data - 24-hour TTL for most keys - Port: **22301** (staging) - **Redis Commander** (Port 29002): Web UI for Redis management ### 3. **didiStorage** (MinIO) ๐Ÿ“ **Purpose**: Object/file storage - 7 auto-created buckets - Media files (images, videos, audio) - Documents and backups - Auto-expiry policies - Ports: **27000** (API), **27001** (Console) (staging) ### 4. **didiQueue** (RabbitMQ) ๐Ÿฐ **Purpose**: Message queue for async processing - Pipeline job queuing - Decouples API from processing - Single unified queue for all analysis types - Durable message storage - Ports: **23100** (AMQP), **23101** (Management UI) (staging) ## Quick Start - Entire Data Layer ๐ŸŽฏ ### Start Everything ```bash # From this directory make up # Or with Docker Compose docker compose up -d ``` ### Stop Everything ```bash make down ``` ### Check Status ```bash make status ``` ### View Logs ```bash make logs ``` ## Individual Service Management ๐Ÿ”ง ### Start Individual Services ```bash make up-database # Start only PostgreSQL make up-cache # Start only Redis make up-storage # Start only MinIO make up-queue # Start only RabbitMQ ``` ### Check Individual Health ```bash make health-database make health-cache make health-storage make health-queue ``` ## Access Points ๐ŸŒ (Staging) | Service | Type | Access URL | Credentials | |---------|------|------------|-------------| | PostgreSQL | Database | `localhost:22001` | postgres / postgres123 | | pgAdmin | Web UI | `http://localhost:29001` | admin@example.com / admin123 | | Redis | Cache | `localhost:22301` | Password: redis123 | | Redis Commander | Web UI | `http://localhost:29002` | admin / commander123 | | MinIO | API | `localhost:27000` | minioadmin / minio123 | | MinIO | Console | `http://localhost:27001` | minioadmin / minio123 | | RabbitMQ | AMQP | `localhost:23100` | admin / rabbitmq123 | | RabbitMQ | Management UI | `http://localhost:23101` | admin / rabbitmq123 | ## What Gets Auto-Created? โœจ When you run `make up`: ### PostgreSQL - โœ… 5 schemas - โœ… All tables - โœ… Indexes and triggers - โœ… User subscription plans ### Redis - โœ… Configured with password - โœ… Persistence enabled - โœ… Memory limits set - โœ… Ready for connections ### MinIO - โœ… 7 buckets created - โœ… Versioning enabled - โœ… Lifecycle policies - โœ… Service access policies ### RabbitMQ - โœ… Unified analysis queue created - โœ… Dead letter queue configured - โœ… Management plugin enabled - โœ… Message TTL policies set ## Directory Structure ๐Ÿ“‚ ``` data-layer/ โ”œโ”€โ”€ README.md # This file โ”œโ”€โ”€ docker-compose.yml # Unified orchestration โ”œโ”€โ”€ Makefile # Simple commands โ”‚ โ”œโ”€โ”€ didiDatabase/ # PostgreSQL Service โ”‚ โ”œโ”€โ”€ docker-compose.yml โ”‚ โ”œโ”€โ”€ init.sql # Database schema โ”‚ โ”œโ”€โ”€ .env # Configuration โ”‚ โ””โ”€โ”€ README.md # Service docs โ”‚ โ”œโ”€โ”€ didiCache/ # Redis Service โ”‚ โ”œโ”€โ”€ docker-compose.yml โ”‚ โ”œโ”€โ”€ redis.conf # Redis config โ”‚ โ”œโ”€โ”€ .env # Configuration โ”‚ โ””โ”€โ”€ README.md # Service docs โ”‚ โ”œโ”€โ”€ didiStorage/ # MinIO Service โ”‚ โ”œโ”€โ”€ docker-compose.yml โ”‚ โ”œโ”€โ”€ init-buckets.sh # Auto-setup โ”‚ โ”œโ”€โ”€ .env # Configuration โ”‚ โ””โ”€โ”€ README.md # Service docs โ”‚ โ””โ”€โ”€ didiQueue/ # RabbitMQ Service โ”œโ”€โ”€ docker-compose.yml โ”œโ”€โ”€ init-queues.sh # Queue setup โ”œโ”€โ”€ .env # Configuration โ””โ”€โ”€ README.md # Service docs ``` ## Environment Variables ๐Ÿ” Each service has its own `.env` file. **Change these for production!** ### Critical Passwords to Change: - `POSTGRES_PASSWORD` in didiDatabase/.env - `REDIS_PASSWORD` in didiCache/.env - `HTTP_PASSWORD` for Redis Commander in docker-compose.yml - `MINIO_ROOT_PASSWORD` in didiStorage/.env - `RABBITMQ_PASSWORD` in didiQueue/.env ## Testing the Data Layer ๐Ÿงช ```bash # Run all tests make test # Test individual services make test-database make test-cache make test-storage make test-queue ``` ## Production Deployment ๐Ÿš€ ```bash # Check for default passwords make check-security # Deploy with production settings make prod ``` ## Troubleshooting ๐Ÿ”ง ### Service won't start? ```bash # Check logs make logs-database make logs-cache make logs-storage make logs-queue ``` ### Port conflicts? Edit the `.env` file in the service directory and change the port. ### Need a fresh start? ```bash # WARNING: Deletes all data! make clean make up ``` ## Resource Usage ๐Ÿ“Š | Service | Memory Limit | CPU Limit | Disk Usage | |---------|-------------|-----------|------------| | PostgreSQL | 2GB | 1.0 CPU | ~500MB + data | | Redis | 512MB | 0.5 CPU | ~100MB + cache | | MinIO | 1GB | 0.5 CPU | ~200MB + files | | RabbitMQ | 1GB | 0.5 CPU | ~100MB + messages | **Total**: ~4.5GB RAM, 2.5 CPUs ## Network Architecture ๐ŸŒ All services communicate on the `didi-backend` network: - **Shared by ALL backend services** (data layer, API layer, orchestration, etc.) - Internal DNS resolution by service name - Isolated from external access (except mapped ports) - Services can reach each other by hostname - Other Docker Compose projects can join this network using: ```yaml networks: default: external: true name: didi-backend ``` ## Why This Architecture? ๐ŸŽฏ 1. **Separation of Concerns** - Each service does ONE thing well - Easy to scale individually - Simple to understand 2. **Zero Configuration** - Everything auto-configures - No manual setup needed - Production-ready defaults 3. **Developer Friendly** - One command to start - Clear documentation - Web UIs included ## Next Steps ๐Ÿ—๏ธ The Data Layer is complete! Next layers to build: ``` โœ… data-layer/ # Complete! โณ orchestration-layer/ # Next: RabbitMQ, Kong, Vault โณ service-layer/ # Then: Microservices โณ application-layer/ # Finally: UI/Apps ``` ## Support & Maintenance ๐Ÿ› ๏ธ ### Daily Tasks - Check logs: `make logs` - Monitor usage: `make stats` - Backup data: `make backup` ### Weekly Tasks - Review disk usage - Check for updates - Rotate passwords ### Monthly Tasks - Full backup - Performance review - Security audit --- ## Quick Reference Card ๐Ÿ“‹ ```bash # Essential Commands make up # Start everything make down # Stop everything make status # Check health make logs # View logs make clean # Delete all data # Individual Services make up-database # Start PostgreSQL make up-cache # Start Redis make up-storage # Start MinIO # Utilities make backup # Backup all data make restore # Restore from backup make test # Run tests make prod # Production deploy ``` --- **๐ŸŽ‰ Your Data Layer is Ready!** Simple. Reliable. Production-Ready. *Version: 1.0.0 | Last Updated: 2025-08-31*