Migrating an eighteen-year-old service from PHP to Python, part 1: nine weeks before day zero
An eighteen-year-old PHP service, 800 thousand accounts, a framework unsupported for years and a database the cloud provider was about to force-upgrade. We rewrote it in Django and switched production over. Users saw a 40-minute maintenance notice and nobody was logged out. This part is about what we did in the nine weeks before, and why most of that work was not writing code.
On Wednesday 28 October at 08:35, an eighteen-year-old PHP service showed its users a maintenance notice. At 09:15 the notice was gone, and at the same address a new application in Django 5.2 was running. Nobody was logged out, that day’s registrations and purchases matched the days before, and the server it all landed on is cheaper than the old one.
This post is about what happened in the nine weeks before that Wednesday: a migration from Laravel 4.2 to Django, done on live production, with no interruption longer than those 40 minutes. The second post, about the switchover day itself and the two days after it, is a separate text, because the two obey different laws: here patience decides, there it is whatever no test could catch.
This was a client project, so the material is anonymised: no name of the service and no details it could be recognised by. Some numbers are rounded.
What we migrated: a Laravel 4.2 service
One codebase served two markets: two countries, two domains, two separate databases and shared logic. The main market’s database holds about 800 thousand user accounts, with a few hundred new ones a day.
On top of that, payments through two operators, including pingbacks, the signed payment notifications an operator sends to our address. A points system. Paid traffic from ads, on the order of 10 thousand page views an hour. A dozen or so cron jobs: digests, reminders, discounts, reports, together a few thousand emails a day through an external mailing provider. Social login, IP geolocation and an admin panel with dozens of screens.
The service is older than its framework. Laravel was upgraded in it over the years until it reached 4.2 and stopped there. The traces of that history stayed in the database: archived passwords in an old format and leftovers of a password migration from years ago. They no longer serve login, but they are there.
Why a rewrite and not another upgrade? Three reasons, and each would have been enough on its own.
A framework unsupported for years, code without tests, the original authors available only now and then. Nobody at the company remembered what some parts were for.
Hosting managed through a tool we had limited access to. On the old server we did not have sudo until the very end.
The database is MySQL 8, but with settings inherited from version 5: the old account authentication method and the old sql_mode. The cloud provider announced that at the end of October it would force an upgrade of the cluster to MySQL 8.4, and 8.4 switches some of that off by default. That is why the date was not negotiable.
The project’s premise was a 1:1 port. The same URLs, the same email templates, the same database queries, the same quirks. No improvements on the way, because every improvement is a risk that someone notices a difference and we cannot tell whether it is a bug or an improvement. Improvements only after the switchover.
How long it took and where the time went
From the first commit to the switchover took nine weeks, from 25 August to 28 October, 154 commits. The team: one developer with an AI assistant, one tester on the client’s side, the client’s devops available for advice, and the client coordinating decisions.
The first two weeks were without access to the old code. We reconstructed the application from the outside: from the live service’s HTML, from how the forms behaved, from whatever could be clicked through. We got the old code on 10 September, and from that day the project turned into a 1:1 port.
Access to the cloud console and to DNS came the day before the switchover.
The truth about that calendar is that most of those nine weeks was waiting: for code, for access, for information, for a decision or for the date of the next step. The work itself, from zero code to a switchover ready to go, could have fit in two weeks. I am not writing this as a complaint, but as a thing to plan for: if you have a similar project ahead of you, settle the list of accesses and decisions in the first week, not when they are needed.
The preparation for the migration that made the difference
Most of the time went not into writing code but into checking that the new code does exactly what the old code did. Below are the things worth describing, because each of them caught something that would otherwise have surfaced in production.
An inventory of addresses before a single line of code. On day one we wrote a crawler that walked the live service and recorded every URL together with the saved HTML. Out of that came the route parity gate: a script that, for every address, checks whether the new application answers with the same HTTP status, the same redirect and the same canonical slug. The gate was green from the first phase and stayed in CI to the end.
The same day, a Django skeleton stood on models generated automatically from the database schema (53 tables), with the two markets recognised by domain and a database router. Over the next three days came the public pages, accounts, stubbed payments and messaging, all of it “by eye”, with the saved HTML of the old service as the model.
Two weeks without the source code. When we got the old code on 10 September, it turned out how many things we had guessed wrong: the rule for what an “active entry” means, the layout of the photo directories, the maths of discounts and settlements, the slug generator. After the port from the code, all 648 checked addresses had matching slugs; before it they did not. The lesson for you, if you are in the same situation: without the code you can build the skeleton, but parity starts with the code.
With the code in hand we audited seven areas, built the shared foundation (slugs, photos, pricing, points), and then seven parallel work packages merged into one branch. The admin panel we rewrote screen by screen.
A 1:1 port as a rule, not a wish. Every area got a parity document: what the old controller writes to the database, in what order, with what defaults. The admin panel is rewritten so that every write is identical to what Eloquent, Laravel’s database layer, used to do. Down to updated_at changing only when some column actually changed. The Django models are marked unmanaged, on the old tables, generated from the schema. No migrations, no “we will fix this while we are at it”.
Production-scale performance before we had seen production. The client set one condition: after the switchover nothing may be slower. So we built a synthetic database with the production schema and indexes, at production scale: a statistics table of 24 million rows, 6 million messages, 760 thousand accounts. Deterministic, so that every measurement could be repeated.
Two rounds of profiling brought the public pages to 22–140 ms, against 300–650 ms on the live service. The three most spectacular queries: statistics from 8.3 seconds to half a second, payment pingback deduplication from 9.4 seconds to a millisecond, the nightly digest from 30 minutes to 69 seconds. We did not find them by being clever, but by profiling on data of production size. Later, on a copy of the live database, a k6 load test confirmed response times about 35 percent lower.
A staging server on a copy of the live database. From 1 October the client’s tester clicked through the new application on a copy of the production database. Behind Basic Auth, with noindex, and with a middleware that rewrites every link to the production domain into the staging one, because the first tester got thrown onto production by a popup from CMS content. Autodeploy from GitHub Actions: a push to the branch is an rsync to the machine and a restart. The same path later shipped fixes to production, so on switchover day it was nothing new.
Earlier, from mid-September, there was also a demo server with its own database and seed data. It was useful for the first clicking around, but it was the copy of the live database that caught the things the seed did not have.
The tester’s list and the final audit. Twenty-four Trello cards from the tester, each mapped to the code with its cause. Then came an audit of the whole old package: every route, artisan command, queue job, email template and table ticked off as “ported”, “deliberately skipped” or “client’s decision”. That list turned up things nobody at the company remembered: two old registration landing pages and the entry scripts of a system that had been switched off for two years.
Link parity a week before. A tool walked every public address of both services on the old server and on the new one: 2457 of 2457 on the smaller market, 4951 of 5000 on the larger one, and those 49 were entries that had meanwhile expired in the live database. Six fixes came out of that run, all minor. That was the moment I stopped worrying about the addresses.
Machine size. We started with 2 vCPUs, went through 4 to 8. The 8-vCPU plan in the cheaper class we rejected, because under load the steal, the processor time taken by other tenants of the same physical machine, reached 48 percent. We settled on a machine from the dearer class, 4 vCPUs and 8 GB of memory, and still cheaper than the old production.
A dress rehearsal for the database. The old application accounts used mysql_native_password, an authentication method that MySQL 8.4 disables by default. We created a new account with modern encryption, and the whole suite of 289 tests passed on a mysql:8.4 container with the production sql_mode. One surprise: the parallel test runner hangs on 8.4 while cleaning up databases, on a metadata lock. On 8.4 the tests run serially only.
Things discovered in the last 48 hours
Here begins the part that was not in the plan. The category pages that the ad traffic lands on were not live listings. They were nightly HTML snapshots, written by a cron job at midnight and served by nginx as plain files. The tester noticed: “the new pages show different data on every reload”. We ported the mechanism 1:1 the same day.
Out of the nginx configuration, not the application code, also came: the redirect from www to the bare domain, the cache headers for photos and the upload size limit. None of these was in the application repository. Each would have been a bug report after the switchover.
If you are to remember one thing from this text: go through the old server’s configuration, not just its code. We did the code audit diligently and it was green. The server had a life of its own next to the code.
In part two: the switchover plan that changed three times in the last two days, the switchover day hour by hour, including the 40 minutes of the maintenance notice, and the fixes from the first days: fourteen in the first 48 hours and three on day three. Four of them were bugs the old application had had for a long time, one of them for four years.
A switchover plan that changed three times in the last two days, 40 minutes of maintenance notice and two days of fixes after moving an eighteen-year-old service from PHP to Python, from Laravel 4.2 to Django. Most of the bugs were a difference between test data and production, or between the code and the server configuration, and four we inherited from the old application.