Complete Rewrites vs the Strangler Fig Pattern
Why I'm cautious about full rewrites of working legacy code, and how I've used the Strangler Fig pattern with Nginx to replace one hot endpoint at a time.
· 2 min read · Erik Kubica
On the client projects I've seen, complete rewrites have gone badly more often than they have gone well.
The pattern is familiar. A newly hired senior developer opens an existing PHP or Node codebase, looks at six years of accumulated business logic, and says: "This code is hard to maintain. We should freeze feature work and rebuild it cleanly in Go."
Management agrees, and estimates six months.
On one project I saw, sixteen months later the company had spent around €140,000 in engineering payroll. The rewrite was still about 70% complete. Sales had no new customer features to sell for over a year, while the old codebase was still generating the company's revenue on an aging production server.
What Legacy Code Often Contains
It is easy to call legacy code "spaghetti" because the aesthetics are what you see first. What you don't see is the history behind it.
That ugly 40-line if/else block inside the billing module might exist because a bank in Slovakia changed its IBAN payment gateway format in 2021 without notifying merchants. That odd regex in the auth handler might patch an edge case where older Safari versions stripped authorization cookies during cross-site redirects.
Code that looks ugly has often survived real-world edge cases, and a rewrite has to rediscover them.
A rewrite gives you a cleaner codebase, but it also starts without the production fixes the old one accumulated.
The Strangler Fig Pattern
When a client brings me a legacy monolith with performance bottlenecks, I try to avoid proposing a total rewrite. I replace it piece by piece instead.
I put an Nginx reverse proxy in front of the application. The legacy stack keeps handling most of the traffic. I look for the single endpoint responsible for most of the CPU load or memory pressure, often heavy PDF invoice generation or high-volume webhook ingestion, and rebuild only that route in a compiled Go binary.
Here is the Nginx configuration I use to carve out endpoints:
upstream legacy_backend {
server 127.0.0.1:9000; # PHP-FPM or legacy Node process
}
upstream go_worker {
server 127.0.0.1:8080; # Fast Go binary idling at 18 MB RAM
}
server {
listen 443 ssl http2;
server_name api.example.com;
# Route high-load webhook endpoint to compiled Go service
location /api/v1/webhooks/ingest {
proxy_pass http://go_worker;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
# Route everything else to battle-tested legacy application
location / {
proxy_pass http://legacy_backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
What This Gave Me
On the project where I used this approach:
- Webhook processing latency dropped from 850 ms in PHP-FPM to 9 ms in Go.
- Server RAM consumption fell by about 60%, so the client could stay on their existing production hardware.
- There was no downtime during the switch.
- If the new Go endpoint had failed, reverting was a one-line Nginx change and reload (
nginx -s reload).
A full rewrite can still be the right call, for example when the platform is truly end-of-life or the data model is fundamentally wrong. But when the old system is earning money, moving the hot paths one at a time has been the lower-risk option for me.