home / skills / frankxai / arcanea / architecture-patterns

architecture-patterns skill

/.claude/skills/oss/architecture-patterns

This skill helps you master architecture patterns from layered to hexagonal, guiding structure choices for scalable, maintainable software systems.

npx playbooks add skill frankxai/arcanea --skill architecture-patterns

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
18.3 KB
---
name: arcanea-architecture-patterns
description: Master software architecture patterns for scalable, maintainable systems. From monoliths to microservices, from MVC to hexagonal - choose the right structure for every challenge.
version: 2.0.0
author: Arcanea
tags: [architecture, patterns, design, scalability, structure, development]
triggers:
  - architecture
  - system design
  - structure
  - patterns
  - scalability
  - how to structure
---

# The Architecture Patterns Codex

> *"Architecture is the art of making the complex manageable. Choose patterns that reveal intent, not hide it."*

---

## The Architecture Philosophy

### Why Architecture Matters

```
CODE is what the system DOES.
ARCHITECTURE is what the system IS.

Bad architecture makes good code hard to write.
Good architecture makes bad code easy to fix.

Architecture decisions are expensive to change.
Choose wisely. Change reluctantly.
```

---

## The Pattern Categories

### Structural Patterns

```
╔═══════════════════════════════════════════════════════════════════╗
║                    STRUCTURAL PATTERNS                             ║
╠═══════════════════════════════════════════════════════════════════╣
║                                                                    ║
║   LAYERED          │ Horizontal separation of concerns            ║
║   HEXAGONAL        │ Ports and adapters, domain-centric           ║
║   CLEAN            │ Dependencies point inward                    ║
║   VERTICAL SLICE   │ Feature-based organization                   ║
║   MODULAR MONOLITH │ Bounded contexts in single deployment        ║
║                                                                    ║
╚═══════════════════════════════════════════════════════════════════╝
```

### Distributed Patterns

```
╔═══════════════════════════════════════════════════════════════════╗
║                    DISTRIBUTED PATTERNS                            ║
╠═══════════════════════════════════════════════════════════════════╣
║                                                                    ║
║   MICROSERVICES    │ Independent deployable services              ║
║   EVENT-DRIVEN     │ Async communication via events               ║
║   CQRS             │ Separate read and write models               ║
║   EVENT SOURCING   │ Store events, derive state                   ║
║   SAGA             │ Distributed transactions                     ║
║                                                                    ║
╚═══════════════════════════════════════════════════════════════════╝
```

---

## Pattern Deep Dives

### The Layered Architecture

```
┌─────────────────────────────────────┐
│         PRESENTATION LAYER          │ ← UI, Controllers, Views
├─────────────────────────────────────┤
│          APPLICATION LAYER          │ ← Use Cases, Services
├─────────────────────────────────────┤
│           DOMAIN LAYER              │ ← Business Logic, Entities
├─────────────────────────────────────┤
│        INFRASTRUCTURE LAYER         │ ← Database, External APIs
└─────────────────────────────────────┘

RULES:
• Dependencies flow DOWN only
• Each layer knows only layer below
• Domain layer has ZERO dependencies
```

**When to Use:**
```
✓ Traditional business applications
✓ CRUD-heavy systems
✓ Teams familiar with MVC patterns
✓ Moderate complexity

✗ Highly dynamic requirements
✗ Event-heavy systems
✗ Complex business logic
```

### The Hexagonal Architecture (Ports & Adapters)

```
                    ┌───────────────┐
                    │   PRIMARY     │
                    │   ADAPTERS    │
                    │ (Controllers) │
                    └───────┬───────┘
                            │
                    ┌───────▼───────┐
                    │               │
            ┌───────┤    PORTS      ├───────┐
            │       │  (Interfaces) │       │
            │       └───────┬───────┘       │
            │               │               │
            │       ┌───────▼───────┐       │
            │       │               │       │
            │       │    DOMAIN     │       │
            │       │   (Core)      │       │
            │       │               │       │
            │       └───────┬───────┘       │
            │               │               │
            │       ┌───────▼───────┐       │
            │       │               │       │
            └───────┤    PORTS      ├───────┘
                    │  (Interfaces) │
                    └───────┬───────┘
                            │
                    ┌───────▼───────┐
                    │  SECONDARY    │
                    │   ADAPTERS    │
                    │ (Repositories)│
                    └───────────────┘

CORE INSIGHT:
Domain knows nothing about the outside world.
Adapters translate between domain and infrastructure.
```

**When to Use:**
```
✓ Domain-driven design projects
✓ Systems needing high testability
✓ Multiple input/output channels
✓ Long-lived business applications

✗ Simple CRUD applications
✗ Rapid prototypes
✗ Small teams with tight deadlines
```

### The Clean Architecture

```
┌─────────────────────────────────────────────────────────────┐
│                    FRAMEWORKS & DRIVERS                      │
│  ┌───────────────────────────────────────────────────────┐  │
│  │               INTERFACE ADAPTERS                       │  │
│  │  ┌─────────────────────────────────────────────────┐  │  │
│  │  │              APPLICATION LAYER                   │  │  │
│  │  │  ┌─────────────────────────────────────────┐   │  │  │
│  │  │  │            DOMAIN LAYER                  │   │  │  │
│  │  │  │         (Entities & Rules)              │   │  │  │
│  │  │  └─────────────────────────────────────────┘   │  │  │
│  │  │              (Use Cases)                        │  │  │
│  │  └─────────────────────────────────────────────────┘  │  │
│  │       (Controllers, Gateways, Presenters)              │  │
│  └───────────────────────────────────────────────────────┘  │
│          (Web, UI, DB, External Interfaces)                  │
└─────────────────────────────────────────────────────────────┘

THE DEPENDENCY RULE:
Dependencies point INWARD only.
Inner circles know nothing about outer circles.
```

### Vertical Slice Architecture

```
Traditional (Layered):           Vertical Slice:
┌──────────────────┐            ┌─────┬─────┬─────┐
│   Controllers    │            │  F  │  F  │  F  │
├──────────────────┤            │  E  │  E  │  E  │
│    Services      │            │  A  │  A  │  A  │
├──────────────────┤     →      │  T  │  T  │  T  │
│   Repositories   │            │  U  │  U  │  U  │
├──────────────────┤            │  R  │  R  │  R  │
│    Database      │            │  E  │  E  │  E  │
└──────────────────┘            │  1  │  2  │  3  │
                                └─────┴─────┴─────┘

INSIGHT:
Group by FEATURE, not by layer.
Each slice is independent and complete.
```

**When to Use:**
```
✓ Feature teams
✓ Rapid iteration
✓ Varying complexity per feature
✓ CQRS systems

✗ Highly shared logic
✗ Small applications
✗ Strong layer conventions required
```

---

## Distributed Architecture Patterns

### Microservices

```
┌─────────┐     ┌─────────┐     ┌─────────┐
│ Service │     │ Service │     │ Service │
│    A    │────▶│    B    │────▶│    C    │
└─────────┘     └─────────┘     └─────────┘
     │               │               │
     ▼               ▼               ▼
┌─────────┐     ┌─────────┐     ┌─────────┐
│  DB A   │     │  DB B   │     │  DB C   │
└─────────┘     └─────────┘     └─────────┘

PRINCIPLES:
1. Single responsibility per service
2. Own your data
3. Communicate via well-defined APIs
4. Deploy independently
5. Design for failure
```

**The Microservices Decision:**
```
CONSIDER MICROSERVICES WHEN:
• Multiple teams need autonomy
• Different scaling requirements per component
• Polyglot persistence needed
• Independent deployment critical
• Organization is distributed

AVOID MICROSERVICES WHEN:
• Small team (< 10 developers)
• Simple domain
• Tight deadlines
• Limited DevOps capability
• Unknown domain boundaries
```

### Event-Driven Architecture

```
┌──────────┐         ┌─────────────┐         ┌──────────┐
│ Producer │────────▶│ Event Bus   │────────▶│ Consumer │
└──────────┘         │ (Kafka/RMQ) │         └──────────┘
                     └─────────────┘
                           │
                     ┌─────┴─────┐
                     ▼           ▼
               ┌──────────┐ ┌──────────┐
               │ Consumer │ │ Consumer │
               └──────────┘ └──────────┘

EVENT TYPES:
• Domain Events: Something happened (OrderPlaced)
• Integration Events: Cross-boundary communication
• Commands: Request to do something
• Queries: Request for information
```

### CQRS (Command Query Responsibility Segregation)

```
┌─────────────────────────────────────────────┐
│                   Client                     │
└─────────────────┬───────────────────────────┘
                  │
        ┌─────────┴─────────┐
        ▼                   ▼
┌───────────────┐   ┌───────────────┐
│   COMMANDS    │   │    QUERIES    │
│   (Write)     │   │    (Read)     │
└───────┬───────┘   └───────┬───────┘
        │                   │
        ▼                   ▼
┌───────────────┐   ┌───────────────┐
│  Write Model  │   │  Read Model   │
│  (Normalized) │──▶│ (Optimized)   │
└───────────────┘   └───────────────┘

INSIGHT:
Reads and writes have different needs.
Optimize each independently.
```

---

## Architecture Decision Framework

### The SOLID Principles in Architecture

```
S - Single Responsibility
    Each component has one reason to change

O - Open/Closed
    Open for extension, closed for modification

L - Liskov Substitution
    Components should be replaceable

I - Interface Segregation
    Clients shouldn't depend on unused interfaces

D - Dependency Inversion
    Depend on abstractions, not concretions
```

### Choosing Your Architecture

```
START HERE:
┌────────────────────────────────────────────────────────────┐
│ What is your team size?                                    │
├────────────────────────────────────────────────────────────┤
│ < 5 developers  → Consider Modular Monolith               │
│ 5-15 developers → Consider Vertical Slices                │
│ 15+ developers  → Consider Microservices                  │
└────────────────────────────────────────────────────────────┘

THEN ASK:
□ How complex is the domain? (Simple → Layered)
□ How testable must it be? (High → Hexagonal/Clean)
□ How often does it change? (Often → Vertical Slices)
□ How independent are components? (Very → Microservices)
□ What are the scaling needs? (Variable → CQRS)
```

### The Strangler Fig Pattern

```
For migrating from legacy:

PHASE 1: Create new system alongside old
┌──────────┐     ┌──────────┐
│  Legacy  │     │   New    │
│  System  │     │  System  │
└──────────┘     └──────────┘

PHASE 2: Route new features to new system
┌──────────┐     ┌──────────┐
│  Legacy  │◀───▶│   New    │
│ (shrink) │     │ (grows)  │
└──────────┘     └──────────┘

PHASE 3: Migrate remaining features
             ┌──────────┐
             │   New    │
             │  System  │
             └──────────┘

PRINCIPLE: Never big-bang rewrite.
Gradually strangle the old with the new.
```

---

## Common Anti-Patterns

### The Big Ball of Mud
```
SYMPTOMS:
• No clear structure
• Everything depends on everything
• Changes have unpredictable effects
• Only original authors understand it

CAUSES:
• No upfront design
• Deadline pressure
• Lack of refactoring
• Knowledge silos

SOLUTION:
• Identify bounded contexts
• Extract modules gradually
• Establish clear interfaces
• Apply Strangler Fig
```

### The Distributed Monolith
```
SYMPTOMS:
• Microservices that must deploy together
• Shared databases
• Synchronous call chains
• Coupled release cycles

CAUSES:
• Wrong service boundaries
• Shared data without events
• Missing async patterns

SOLUTION:
• Merge tightly coupled services
• Introduce event-driven communication
• Apply domain-driven design
```

---

## Quick Reference

### Architecture Checklist
```
□ Clear separation of concerns
□ Dependencies point in one direction
□ Domain logic isolated
□ External dependencies abstracted
□ Components independently testable
□ Scaling strategy defined
□ Failure modes understood
□ Monitoring and observability planned
```

### Pattern Selection Matrix
```
| Need                    | Pattern              |
|-------------------------|----------------------|
| Simple CRUD             | Layered              |
| Complex domain          | Hexagonal/Clean      |
| Feature teams           | Vertical Slices      |
| Team autonomy           | Microservices        |
| Read/write separation   | CQRS                 |
| Audit trail             | Event Sourcing       |
| Distributed txns        | Saga                 |
| Legacy migration        | Strangler Fig        |
```

---

*"The best architecture is the one that makes the right thing easy and the wrong thing hard."*

Overview

This skill teaches software architecture patterns for building scalable, maintainable systems. It distills structural and distributed patterns—from Layered and Hexagonal to Microservices, CQRS, and Event-Driven—and provides decision guidance and migration strategies. Use it to pick patterns that match team size, domain complexity, and operational constraints.

How this skill works

I summarize each pattern with structure diagrams, core rules, pros/cons, and clear "when to use" guidance. The skill compares patterns side-by-side, maps organizational factors (team size, scaling needs) to architectural choices, and lists anti-patterns and remediation steps. It also provides a lightweight decision framework and a checklist to validate architecture choices before implementation.

When to use it

  • Designing new systems where maintainability and testability matter
  • Choosing between monolith, modular monolith, and microservices based on team and scale
  • Migrating legacy systems using the Strangler Fig approach
  • Evaluating event-driven and CQRS approaches for high-read or async workloads
  • Setting boundaries and avoiding distributed monolith anti-patterns

Best practices

  • Match architecture to team size and operational maturity, not to fashionable terms
  • Encapsulate domain logic and depend on abstractions (Dependency Inversion)
  • Prefer clear bounded contexts and explicit interfaces when splitting services
  • Design for failure: embrace retries, idempotency, and backpressure in distributed systems
  • Use incremental migration (Strangler Fig) to avoid big-bang rewrites

Example use cases

  • A startup with <10 engineers: adopt a modular monolith to reduce DevOps overhead
  • A product with multiple independent feature teams: use Vertical Slice architecture for team ownership
  • A complex domain requiring high testability: apply Hexagonal or Clean Architecture
  • A high-throughput read-heavy system: separate read/write paths with CQRS
  • Migrating a legacy ERP: incrementally replace modules using the Strangler Fig pattern

FAQ

How do I decide between Hexagonal and Clean Architecture?

Both emphasize dependency direction and domain isolation; choose Hexagonal for simpler ports/adapters workflows and Clean when you want explicit concentric layers and use-case centric separation.

When should I avoid microservices?

Avoid microservices for small teams, simple domains, tight deadlines, or limited DevOps—these increase coordination cost and risk of a distributed monolith.

What's the fastest safe way to modernize a monolith?

Apply the Strangler Fig: add new features to the new system, route traffic selectively, and incrementally extract and replace legacy components.