1---2name: serverside-architecture-selector3description: Defines the system architecture with a focus on simplicity, scalability, and separation of concerns.4---56## Golden Principle: "The Last Responsible Moment"7* **Simplicity is King:** Always prefer the simplest solution that meets current requirements.8* **Domain First:** Do not make infrastructure decisions before defining domain requirements.9* **Coupling vs. Abstraction:** Favor simple coupling over premature abstraction until a pattern of repetition is proven.1011---1213## 1. Pre-flight Check1415* **Domain vs. Effort:** If the domain complexity is High/Extreme, the agent MUST propose a draft of Bounded Contexts and require validation before suggesting any code patterns.16* **Persistence and Performance:** CQRS must not be suggested without a clear justification regarding "read/write mismatch".17* **Legacy and ACL:** If there is integration with legacy systems, an **Anti-Corruption Layer (ACL)** must be designed obligatorily.18* **Microservices:** Do not suggest microservices unless the cost of Team Cognitive Load or the need for independent scaling is the main bottleneck.1920---2122## 2. Architecture Definition (Decision Tree)2324### **A: What is the complexity of your Business Domain?**25* **Low (MVP/CRUD):** Transaction Script / MVC (Focus on simplicity and fast delivery).26* **Medium (Clear rules, many integrations):** Hexagonal Architecture (Side-effects isolation and testability).27* **High (Complex domain, low change rate):** Clean Architecture (Full framework abstraction and UI independence).28* **Extreme (Complex, mutable, core-to-business domain):** Domain-Driven Design (Ubiquitous Language, Bounded Contexts, and Living Business Logic).2930### **B: What is the complexity of data access?**31* **Simple access (Simple CRUD, single database):** Repository Pattern.32* **Complex transactions (Complex transactions, multiple tables):** Repository Pattern + Unit of Work.33* **Read-Intensive / Audit (Complex domain, read-intensive, or audit trails):** Data Mapper + CQRS (Command Query Responsibility Segregation).3435### **C: What is the Communication and State strategy?**36* **Synchronous (Strong Coupling):** Request-Response (Focus on immediate consistency).37* **Asynchronous (Event-Driven):** Pub/Sub or Message Queuing (Focus on decoupling and resilience).38* **Distributed State / Auditing:** Event Sourcing (The state is the sum of all past events).3940### **D: What is the scale of the operation?**41* **Fast growth/Uncertain boundaries:** Modular Monolith (Low infrastructure cost and easier refactoring).42* **Massive scale/Independent teams:** Microservices (Independência de deploy, poliglotismo, High scalability and tolerance for cost/complexity).4344### **E: Who are the consumers?**45* **Public API / Multiple platforms:** REST.46* **Complex data needs / Multiple frontends:** GraphQL.47* **Real-time / Continuous data flow:** WebSockets / Server-Sent Events.48* **Internal microservices (Inter-service):**49 - **Low Latency / Performance:** gRPC.50 - **Simplicity / Universality:** REST.5152### **F: Evolution and Lifecycle Stage**53* **Initial Stage (Speed & Low overhead):** Modular Monolith (Logical boundaries, shared database, simplified deployment).54* **Growth Stage (Scaling specific modules or independent team growth):** Extract Bounded Context to Microservice (Physical decoupling only when scaling needs differ).55* **Optimization Stage (Highly granular, event-driven tasks):** Serverless Functions / FaaS (Focused on side-effects and ephemeral workloads only).5657### **G: Mobile & Multi-client Strategy (BFF Validation)**58* **Direct Integration:** Use when the frontend is a single Web App and the API already returns optimized payloads.59* **Backend For Frontend (BFF):** **MANDATORY** if one or more of the following conditions are met:s60 1. **Over-fetching/Latency:** Mobile clients on 3G/4G receiving large JSONs where only 10% of fields are used.61 2. **API Orchestration:** The frontend needs to call 3+ microservices to render a single screen (Chatty I/O).62 3. **Protocol Translation:** Frontend needs REST/WebSockets, but downstream services use gRPC or SOAP.63 4. **Client-Specific Logic:** Mobile requires different data shapes, specific push notification logic, or image resizing not needed by Web.64* **BFF Rejection Criteria (When NOT to use)**65 1. **Passthrough Proxy:** If the BFF only forwards requests 1:1 to a single downstream service without transformation.66 2. **Shared BFF:** If you are building a "Universal BFF" for Web, iOS, and Android (this is just another API Gateway).67 3. **Business Logic Leakage:** If you find yourself implementing domain rules (discounts, taxes, auth-logic) in the BFF. These belong in the Domain/Microservice.6869---7071## 3. Review Checklist72* [ ] Does the architecture match the team size and cognitive load capacity?73* [ ] Is the BFF justified by specific UI needs or network constraints?74* [ ] Are there clear boundaries (Bounded Contexts) even in a Modular Monolith?75* [ ] Is the "Dependency Rule" pointing towards the Domain in all suggested layers?