The Rewrite Trap: Why Rebuilding Software From Scratch Fails
New engineering lead inherits legacy codebase. Opens repository. Sees five-year-old PHP monolith, unindexed SQL queries, undocumented billing handlers, spaghetti dependencies. Immediate reaction: burn repository down. Rebuild from scratch with modern stack. Go, Rust, Next.js, microservices, Kubernetes.
Engineering lead pitches CTO: current codebase unmaintainable. Slow velocity. High bug count. Rebuild takes six months. Solves all problems. CTO agrees. Board approves budget.
Disaster begins.
Six months pass. New system handles 30% of legacy workload. Zero edge cases covered. Budget spent. Original system still needs updates because company needs cash flow. Team now maintains two codebases instead of one. Velocity drops to zero.
At Kreasi Kita, we rescue projects trapped in rewrite graveyard. Clean-slate rebuild is rarely architectural decision. It is emotional reaction to unfamiliar complexity.
Delusion Of Clean Slate
Engineers hate reading unfamiliar code. Reading code takes more cognitive effort than writing code. Legacy code looks ugly because legacy code survived production reality.
+------------------------------------------------------------+
| LEGACY CODE |
| |
| [ Clean Ideal Code ] <-- What engineers think exists |
| + |
| [ Edge Cases ] <-- 4 years payment gateway bugs |
| + |
| [ Compliance Patches] <-- Tax regulation quirks |
| + |
| [ Hardware Workarounds]<-- Third-party API payload drops |
+------------------------------------------------------------+
Legacy codebase contains institutional memory:
- Strange
ifstatements inside payment loop handle undocumented webhook quirks from local Indonesian banks. - Raw SQL query handles bizarre multi-tenant isolation requirement demanded by enterprise client three years ago.
- Fragile regex catches malformed user inputs from legacy mobile apps still running on user devices.
Rewrite throws away battle-tested business logic. Engineers replace tested edge cases with clean architecture that breaks on day one in production.
Second-System Effect Always Wins
Fred Brooks documented pattern in 1975: Second-System Effect. Software architects design replacement system with all ideas, optimizations, and features cut from original project.
Scope explodes immediately:
- Feature parity moving target: Legacy system cannot freeze. Business requires new compliance rules, integrations, and checkout flows. Legacy code keeps moving forward. Rewrite chases moving target.
- Gold plating: Modular Laravel monolith worked fine on single server. Rebuild team insists on distributed microservices with Kafka, gRPC, and custom Kubernetes orchestrator before validating core user journeys.
- Double maintenance penalty: Senior engineers move to greenfield rewrite. Juniors maintain revenue-generating legacy system. Legacy system bugs multiply. Senior engineers context-switch daily to fix production fires.
Result: timeline slips from six months to eighteen months. Burn rate triples. Competitors ship features while rewrite team debates domain boundary schemas.
Technical Audit: Rebuild Strategies Compared
Total rebuild is highest-risk strategy available in software engineering. Three primary options exist:
| Strategy | Risk Factor | Time to Value | Business Continuity | Edge Case Retention | Capital Cost |
|---|---|---|---|---|---|
| Big-Bang Rewrite | Critical | 12–24 months | Zero (Feature freeze) | Discarded | Extreme |
| In-Place Refactoring | Low | Continuous | High (Zero downtime) | 100% Preserved | Low to Medium |
| Strangler Fig Pattern | Medium-Low | 2–4 weeks per cycle | Complete | Progressive verification | Predictable |
Big-bang rewrite fails because delivery loop feedback equals zero until release day. Strangler Fig pattern delivers value immediately.
Architecture: Strangler Fig Pattern
Never replace running system all at once. Put facade in front. Intercept requests. Route traffic path by path from legacy monolith to modern services.
+-------------------+
| Client Request |
+---------+---------+
|
v
+-------------------+
| Reverse Proxy |
| (Nginx / Envoy) |
+----+---------+----+
| |
Path: /api/v2/orders | | Path: /api/v1/* (Fallback)
v v
+-------------------+ +--------------------+
| New Service | | Legacy Monolith |
| (Node / Next / Go)| | (Rails / PHP / DB)|
+---------+---------+ +---------+----------+
| |
v v
+-------------------+ +--------------------+
| Target Database | | Legacy Database |
+-------------------+ +--------------------+
Phase 1: Edge Proxy Interception
Install reverse proxy (Nginx, Traefik, AWS ALB) directly in front of legacy application. Route 100% of traffic through proxy to legacy backend. Verify zero latency impact.
Example Nginx routing block:
upstream legacy_monolith {
server 10.0.0.10:8080;
keepalive 32;
}
upstream modern_service {
server 10.0.0.20:3000;
keepalive 32;
}
server {
listen 80;
server_name api.business.com;
# New domain migrated to isolated modern service
location /api/v2/checkout {
proxy_pass http://modern_service;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# Everything else falls back to legacy core
location / {
proxy_pass http://legacy_monolith;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Proxy isolates legacy dependencies. Monolith does not know proxy exists. Clients do not know backend split exists.
Verification: Shadow Traffic And Data Parity
Do not switch user traffic blindly. Use shadow traffic (dark launching) to verify new service matches old service behavior.
Reverse proxy clones production request. Sends primary request to legacy monolith. Sends mirror request to modern service. Discards mirror response. Logs differences.
# Mirror incoming production traffic for testing
location /api/v1/orders {
mirror /mirror;
mirror_request_body on;
proxy_pass http://legacy_monolith;
}
location = /mirror {
internal;
proxy_pass http://modern_service;
proxy_set_header X-Shadow-Traffic "true";
}
Compare outputs asynchronously using worker pipeline:
interface ComparisonResult {
endpoint: string;
match: boolean;
legacyStatus: number;
modernStatus: number;
payloadDiff: Record<string, unknown>;
}
export async function compareResponses(
legacyRes: Response,
modernRes: Response,
endpoint: string
): Promise<ComparisonResult> {
const legacyBody = await legacyRes.json();
const modernBody = await modernRes.json();
const match = JSON.stringify(legacyBody) === JSON.stringify(modernBody);
return {
endpoint,
match,
legacyStatus: legacyRes.status,
modernStatus: modernRes.status,
payloadDiff: match ? {} : diff(legacyBody, modernBody),
};
}
Run shadow traffic until modern service error rate reaches zero percent. Once verified, flip proxy route. Legacy path dies. Zero user disruption. Zero business risk.
Step-By-Step Strangler Migration Plan
To decouple services safely without risking data corruption, follow this sequence:
- Identify the single highest-friction domain in the legacy codebase (such as billing, inventory calculation, or user authentication). Avoid low-impact leaf nodes.
- Freeze changes specifically to that isolated domain model inside the legacy monolith.
- Establish a read-replica or configure change-data-capture (CDC) with Debezium from the legacy database into the new service data store.
- Build the new service module using targeted modern tools (for example, Next.js for slow frontend layers, or Dockerized modular Laravel/Go for compute-heavy workers).
- Implement dual-writing at the application boundary if write operations must persist across both systems during the transition.
- Configure shadow mirroring at the reverse-proxy layer to benchmark runtime response bodies under actual production loads.
- Switch the reverse-proxy routing rule to direct 100% of production traffic to the new service.
- Remove the deprecated code path and related dead database tables from the legacy monolith.
In-Place Refactoring: Fix Hot Paths Inside Production
Microservices often unnecessary. Modernizing monolith in-place delivers results faster with lower infrastructure overhead.
Identify bottlenecks with profiling tools (APM, Datadog, Blackfire):
- Optimize slow database queries: add missing compound indexes, eliminate N+1 queries, replace complex ORM calls with raw SQL views.
- Extract domain layers: wrap unstructured global functions into testable service classes.
- Introduce typed interfaces: add TypeScript on frontend, strict types on PHP/Python/Ruby backend.
- Decouple state: pull session state and local uploads out of application server. Ship sessions to Redis cluster. Ship files to S3 bucket. Application becomes stateless immediately. Scales across containers without rewrites.
Clean architecture is continuous discipline, not final destination.
Production Reality
Code value measured by business survival, revenue collection, customer utility. Beautiful code that crashes or misses market window equals bankruptcy. Ugly code generating fifty million dollars annual recurring revenue with 99.9% uptime equals successful software.
Never halt business operations for clean-slate engineering fantasy. Refactor boundaries systematically. Replace dead components incrementally. Protect production logic at all costs.
Sitting on unmaintainable monolith, failing rewrite project, or high-risk architectural dead-end? Bring engineering recovery challenges to Kreasi Kita. We stabilize legacy infrastructure, modernize production hot paths, and scale mission-critical codebases without halting delivery velocity.<p class="blog-internal-links">For the implementation path, explore our <a href="/services/software-modernization">software modernization for legacy systems</a>. Continue with <a href="/blog/postgres-vs-mongodb-document-db-hangover">how database choices create long-term engineering costs</a>.</p>
ABOUTPROJECTS