After we moved a bunch of Stripe onto Java microservices, we looked at the Mongo offering and decided it was too slow for some of the services we wanted to be more performant. Those services went onto Aurora Postgres instead.
When they first launched, each service got a database as a single cell: one primary, a few replicas, and that was it. That was contrary to how Stripe operated at the time. The whole name of the game at Stripe was sharding. In Mongo you'd shard by account, or sometimes by another ID, but most commonly by the actual account. That gave you higher availability, fewer noisy neighbor problems, and better disaster recovery.
So the single-cell services chugged along, mostly in beta. Then we determined they needed to move into production workflows, and we didn't feel we could confidently do that until they had the same kind of sharding our Mongo configuration offered.
On loan to the databases team
I was tasked with it as a "one-man tiger team", on loan to the databases team. I didn't work on databases at the time. I had to design an approach that would move all 11 services' databases, 10+ TB of live data, onto a cellular architecture.
The saving grace was that I was allotted a little bit of write downtime from the business requirements. No read downtime, but since the services were in beta, we were confident we could do it off hours, take a few minutes of write downtime, and send proactive comms to users. That's not a common thing to be handed in distributed systems. You usually operate solely from a "I can't take any downtime" perspective.
Before designing anything I did a lot of social research. I sat and chatted with principal engineers at Aurora, with our databases team, and with executive leadership at Stripe, all building my knowledge of the space and designing my solution. Remember: this was pre-LLM!
One cell to seven
The target was seven cells per service: some live cells, some synthetic data cells, some test cells. Seven was the agreed-upon cellular strategy for Stripe at the time, a decision we were included in but didn't directly drive. A lot of the existing data had to be resharded as a part of this. Synthetic data sitting in the single cell actually belonged in the synthetic cell, so it had to move.
The approach:
- Take the existing single cell and make it one of the seven. Pick any one, it doesn't matter. I naturally chose
cell-01. - Spin up the other six and replicate into them. I enabled logical replication from writer to writer, which could run across AZs, and blasted it out to every new writer. That gave us seven identical cells. Just getting logical replication to run happily took longer than we expected in the pre-flight setup, and the initial sync took days. Before the flip, we confirmed the cells were actually identical using replication lag and total row counts. We didn't inspect row by row; the volume of data was too large for that.
- Pause writes, flip routing, resume. This is where the downtime went, in one window for all 11 services. Writes were paused with feature flags in the middleware, not in each service, because I didn't want to maintain flags across every different service. With writes paused, I updated the mapping logic, also in the middleware, so that each write was routed to the right cell: cell one, cell two, cell three, and so on. The routing decision is made off the ID the request is routed on. IDs can be arbitrary, but we used a specific format that gave us consistent routing logic, and that same structure guaranteed IDs were unique, so there was no worry about two cells handing out the same ID once writes split. Then writes came back on, now landing in the correct cell. Reads kept succeeding the whole time, because logical replication had already populated the data everywhere. And since writes were paused, there was no eventual consistency to worry about either. I did not tear down logical replication during the migration. The routing layer sends every request to the right cell, so extra data sitting in the wrong cell would never be read. Leaving replication running kept the migration shorter, and we could monitor access to that data after the fact. The rollback plan was the same idea in reverse: the original cell stayed the source of truth, so if anything went wrong after resume, flip the routing back to it.
- Clean up. The downside of replicating "everything everywhere" is that every cell was now full of data that didn't belong to it. I wrote cleanup workers for that, ran them in batches, and monitored them extremely closely (this was live data). We didn't snapshot before purging: we knew exactly what we were deleting, and the real copy of every one of those rows already lived in its correct cell. We knew what should be in each cell, and from access times we knew what was actually being read. If a row didn't belong and wasn't being accessed, boom, purge it. If a row didn't belong but was being accessed, that was a routing bug, and we went and investigated.
That check fired one or two times. Every case was synthetic data or data that an internal dev had placed by hand. Nothing live, nothing user-facing, but it was a mechanism we actually exercised.
Everyone involved
A ton of teams were implicated. The databases team, obviously. All 11 service teams, since I was migrating their live data. The mapping logic itself, which I wrote. And the CDC and events teams, because everything that happened after a write, through Debezium and the event streams, had to be updated too. Going from one cell to seven meant spinning up a lot of new streams. I worked with the events team on consolidated patterns for that, so downstream consumers didn't have to change their integration interface.
The biggest hiccup
Events. Because the flip was one window for all 11 services, every new stream had to be spun up at once, and there were so many that it crashed the events control plane. The migration hung in the middle while the events team scaled up their hosts, and then the streams came up. We didn't see it coming. We had tested in our beta environments and done some testing in prod, but we never tested at the full magnitude of what we were actually migrating in prod. That was the biggest takeaway from this project for me.
Result
Total write downtime was under five minutes (longer because of the events outage).
Beta users were told there would be a brief write downtime for database maintenance, with the window: a first notice two weeks out, a reminder a week before, and another the day before. There were no customer reach-outs and no reported issues. The cells worked as standard, and the migration was done.
The project started around ~March, with a target of end of year for both the migration and moving the services into prod. I finished in October.
The services moved into production workflows as planned, and the cells delivered the availability and noisy neighbor isolation they were projected to. Those services went on to take hundreds of thousands of requests per second and do just fine.
This project captured a lot of my time, in a space I was unfamiliar with. I really appreciated getting to learn more about logical replication, about Postgres, and about Aurora specifically. But I pushed so hard for so long on it, and got it done significantly ahead of schedule, that I had to take a nice week of vacation to recharge afterwards. The 2023 Year In Review has more on that.