Skip to main content

System Design & Architecture

Dokumentasi lengkap arsitektur sistem MStore Backend dari high-level design hingga implementation details.
πŸ’‘ Working with Diagrams:
  • Zoom: Cmd/Ctrl + Scroll atau klik kanan β†’ β€œOpen Image in New Tab”
  • Download: Copy code diagram β†’ paste ke Mermaid Live Editor β†’ Download PNG/SVG
  • Full Guide: Lihat Mermaid Guide untuk tutorial lengkap

🎯 Architecture Principles

MStore Backend dibangun dengan prinsip:
  • βœ… Clean Architecture: Separation of concerns (handler β†’ service β†’ repository)
  • βœ… Domain-Driven Design: Business logic organized by domain
  • βœ… Multi-Tenant: Merchant-scoped data isolation
  • βœ… Microservices-Ready: Modular design untuk future decomposition
  • βœ… Event-Driven: Message broker integration (NATS, RabbitMQ, Kafka)
  • βœ… API-First: RESTful API dengan OpenAPI/Swagger documentation
  • βœ… Observability: LGTM stack (Loki, Grafana, Tempo, Mimir)

πŸ—οΈ High-Level Architecture


πŸ“¦ Clean Architecture Layers

1. Handler Layer (Presentation)

Responsibility: HTTP request/response handling, validation, authentication

2. Service Layer (Business Logic)

Responsibility: Business rules, orchestration, transaction management

3. Repository Layer (Data Access)

Responsibility: Database operations, query building

πŸ”€ Domain Organization


πŸ”„ Request Flow

Typical API Request Flow


πŸ—„οΈ Database Architecture

Multi-Database Strategy

MySQL (Primary - Transactional)
  • Transactions
  • Inventory & Warehouse
  • Approvals
  • Accounting & Journals
  • Master data (Products, Branches, Users)
MongoDB (Secondary - Flexible Schema)
  • POS offline transactions (temporary)
  • Audit logs
  • Analytics data
  • Unstructured documents
Redis (Cache & Session)
  • Session storage
  • API rate limiting
  • Real-time data cache
  • Pub/Sub for real-time updates

Multi-Tenant Data Isolation


πŸ” Security Architecture

Authentication Flow


Authorization (RBAC)


πŸ“‘ Event-Driven Architecture

Message Broker Integration


πŸ” Observability Architecture

LGTM Stack Integration

Loki (Logs)
Tempo (Traces)
Mimir (Metrics)
Grafana (Dashboards)
  • Transaction volume & revenue
  • API latency & error rate
  • Database query performance
  • Cache hit rate
  • Message broker throughput

πŸš€ Deployment Architecture

Development Environment


Production Architecture


πŸ“Š Performance Characteristics

Target Metrics


Scalability Strategy

Horizontal Scaling
  • Stateless application servers
  • Load balancer distribution
  • Database read replicas
  • Redis cluster for cache
Vertical Scaling
  • Database server resources
  • Cache memory allocation
  • Message broker capacity
Caching Strategy
  • L1: In-memory cache (per instance)
  • L2: Redis cluster (shared)
  • Cache invalidation via events

πŸ’‘ Best Practices

DO βœ…

  • Follow clean architecture layers
  • Use dependency injection
  • Implement proper error handling
  • Log with structured logging (Zap)
  • Use context for cancellation & timeout
  • Implement circuit breaker for external APIs
  • Use database transactions for atomic operations
  • Implement idempotency for critical operations
  • Monitor with OpenTelemetry
  • Document APIs with Swagger

DON’T ❌

  • Jangan skip input validation
  • Jangan hardcode credentials
  • Jangan ignore context cancellation
  • Jangan block goroutines indefinitely
  • Jangan skip error logging
  • Jangan expose internal errors ke client
  • Jangan skip database indexes
  • Jangan ignore connection pooling

πŸ”„ Migration Strategy

Microservices Decomposition (Future)

Decomposition Strategy:
  1. Start with bounded contexts (domains)
  2. Extract stateless services first
  3. Implement API gateway
  4. Use event bus for inter-service communication
  5. Gradual migration per domain

Tech Stack

Library dan tools yang digunakan

Service Template

Template untuk membuat service baru

API Styleguide

Panduan API design & conventions

Database Schema

ERD lengkap database

Observability

LGTM stack setup & monitoring

Need Help? Contact backend team atau check GitHub Issues