0

Prompt: Refactor Legacy Monolith to Microservices

Output a domain map in this format: DOMAIN: [Name] - Responsibilities: [list] - Current files: [list] - Database tables: [list] - External dependencies: [list] - Extraction complexity: [LOW/MEDIUM/HIGH] - Priority: [1-5]

Prompt · برومبت

Works with

claude-code (compatible), claude (compatible), codex (compatible), chatgpt (compatible), gemini-cli (compatible), cursor (compatible), copilot (compatible), opencode (compatible), amp (compatible), roo (compatible), kiro (compatible), goose (compatible), junie (compatible), trae (compatible), cline (compatible), continue (compatible), antigravity (compatible), windsurf (compatible), augment (compatible), qwen-code (compatible), aider (compatible)

Content

Prompt: Refactor Legacy Monolith to Microservices

Purpose: Guide an AI assistant through systematic decomposition of monolithic codebases into production-ready microservices architecture.

Best For: Legacy codebase modernization, scalability improvements, team autonomy enablement

Model Recommendation: Claude Sonnet 4.5, Claude Opus 4, GPT-4o

Usage Instructions:

  1. Paste the prompt below, then attach your monolith's directory structure and key files.
  2. Share your requirements.txt, package.json, or main entry file.
  3. Specify your target cloud provider.

---

THE PROMPT

You are an expert software architect specializing in legacy system modernization. I need you to analyze my monolithic application and produce a detailed microservices decomposition plan.

**PHASE 1 - ANALYSIS (Do this first):**
Analyze the provided codebase and identify:
1. **Domain Boundaries**: Group related functions/classes by business capability
2. **Database Dependencies**: Map which tables belong to which domain
3. **Coupling Points**: Identify tight coupling that must be addressed
4. **API Contracts**: Current internal function calls that will become APIs
5. **Shared Code**: Libraries that need to become shared packages or API calls
6. **Traffic Patterns**: Which modules are called most (mark for priority extraction)

Output a domain map in this format:

DOMAIN: [Name]

  • Responsibilities: [list]
  • Current files: [list]
  • Database tables: [list]
  • External dependencies: [list]
  • Extraction complexity: [LOW/MEDIUM/HIGH]
  • Priority: [1-5]

**PHASE 2 - DECOMPOSITION STRATEGY:**
For each identified service, provide:

1. **Service Specification:**
   - Name and port number
   - Technology stack (suggest if not specified)
   - API endpoints (REST/GraphQL/gRPC)
   - Database type and schema
   - Message queue topics it publishes/subscribes to

2. **Strangler Fig Pattern Steps:**
   - Exact order of extraction (start with lowest coupling)
   - Feature flags needed during migration
   - Data migration strategy (dual-write period)
   - Rollback plan

3. **Inter-Service Communication:**
   - Synchronous (REST/gRPC): for real-time needs
   - Asynchronous (Kafka/RabbitMQ/SQS): for event-driven flows
   - Saga pattern for distributed transactions

**PHASE 3 - IMPLEMENTATION:**
Generate the actual code for:
1. Docker Compose file for local development
2. Kubernetes manifests for production
3. API Gateway configuration (Kong/AWS API GW/Nginx)
4. Shared authentication middleware (JWT)
5. Health check endpoints for each service
6. OpenTelemetry instrumentation setup
7. Database migration scripts

**PHASE 4 - TESTING STRATEGY:**
- Contract tests between services (Pact)
- Integration test setup
- Chaos engineering scenarios to validate resilience

**CONSTRAINTS:**
- Zero-downtime migration (no big bang rewrites)
- Each service deployable independently
- Data consistency maintained throughout migration
- Current users must not experience disruption

**My monolith details:**
- Language/Framework: [USER FILLS IN]
- Database: [USER FILLS IN]
- Current team size: [USER FILLS IN]
- Timeline: [USER FILLS IN]
- Target cloud: [USER FILLS IN]

Start with Phase 1 analysis. Ask for the code/structure if not provided.

---

Example Input:

My monolith details:
- Language/Framework: Python/Django, 80,000 lines of code
- Database: PostgreSQL, 45 tables
- Current team size: 8 developers
- Timeline: 6 months
- Target cloud: AWS

Here's my apps/ directory structure:
apps/
├── users/          # Auth, profiles, permissions (12 models)
├── products/       # Catalog, inventory, pricing (8 models)
├── orders/         # Cart, checkout, fulfillment (10 models)
├── payments/       # Stripe integration, invoices (6 models)
├── notifications/  # Email, SMS, push (3 models)
├── analytics/      # Reports, dashboards (6 models)
└── api/            # DRF views, serializers

Example Output (Phase 1):

DOMAIN: User & Identity Service
  - Responsibilities: Authentication, authorization, user profiles, permissions
  - Current files: apps/users/, apps/api/views/auth*.py
  - Database tables: users, user_profiles, permissions, roles, role_permissions
  - External dependencies: Django Auth, SendGrid (email verification)
  - Extraction complexity: MEDIUM (JWT migration needed)
  - Priority: 1 (extract first - everything depends on auth)

DOMAIN: Product Catalog Service  
  - Responsibilities: Product listings, search, inventory management, pricing
  - Current files: apps/products/, apps/api/views/product*.py
  - Database tables: products, categories, inventory, price_rules
  - External dependencies: Elasticsearch (search), S3 (images)
  - Extraction complexity: LOW (minimal coupling)
  - Priority: 2

[CRITICAL] Coupling Alert: orders/ directly imports from products/ models
  → Recommended: Replace with API call + event-driven inventory updates

Source and license

Source
Enterprise AI Prompts
Original
m0shaban/enterprise-ai-prompts/prompts/claude-code/refactor-legacy-code.md
License
MIT