Magento MSI Microservice Story
How replacing Magento MSI write contention with a lightweight Rust microservice eliminated weekly warehouse recounts.
Back in 2020, one of my customers was having many issues with stock levels being out of sync. ERP, Magento, and the warehouse were unable to correctly calculate what was shipped within a week. This brought e-commerce operations to a complete stop every week, as the warehouse team had to do complete recounts of every item in physical inventory.
Given that the customer decided to stick with MSI for their reasons, I was tasked to untangle the mess of its internal implementations.
If someone is not aware, I have a bitter story about the MSI project, as the original multi-stock inventory proposal I did back in 2015 was completely swept under the rug and never mentioned in a new "community" driven project for MSI
A lot of my ideas about it involved the atomicity of stock updates to ensure no stock over-updates happen. I suggested having reservations with status and separate fields on stock items for physical, available, and reserved stock so you always know what you are currently selling.
Initial Design & Development
This was the fourth high-performance production application I wrote from scratch in Rust, and I was fortunate to use the new async/await syntax introduced just one year earlier. Right from the start, I put myself in the boundary that the application needs to have minimal impact on Magento purchase flow and be isolated in boundary scope, so if the merchant wanted to change some logic on top, they could do it without much changes in this small microservice. The application introduced one new table to the whole system to keep track of the actual stock state of the product in the warehouse instead of relying on a single quantity field. The table had only two columns: shipped_qty and reserved_qty. When you placed an order, your reserved_qty increased, but when you shipped an order, your shipped_qty increased atomically while reserved_qty decreased. The introduction of these two fields allowed us to get rid of weekly warehouse recounts for the merchant.
Internally, the application is running as a worker that receives messages from Magento to know if this product is salable and how much qty from which warehouse was reserved for this specific order; all ERP stock mutations were also channeled via this service. The process that was running was running for months without any need to restart it as it was consuming less than 10 MB of RAM. All stock mutations were happening in memory and then synced in the background to the Magento database.
Also, it was supporting out of the box multi-warehouse order fulfillment with accurate stock counts. If, let's say, the US warehouse did not have an item in stock, the EU warehouse was used as a backup with proper notification being shown to the user that some items would take longer to deliver. This one did not require any schema changes and was purely based on the priorities of stock sources.
Request for a change
I almost completely forgot about that project until 2024, when a customer asked me to make a small change in the application unrelated to business logic. They wanted to be able to reload the list of source priorities as they were expanding their business and adding new warehouses. Out of the box, they only needed to restart the application if they wanted to make a new source live, but they needed a better way to do it without a restart, so I spent a couple of days developing a couple of new commands to reload the list of sources by json message and SIGUP. Now, I think it will run entirely untouched for another 10 years until they change their business setup.
Takeaways
Sometimes, microservices can be a really good solution for a business problem, but it does not mean that you should move everything into a lot of small microservices. This customer story shows a specific problem I solved for a merchant without completely removing the Magento monolith. I wrote a small app that replaced all write DB interactions with MSI for the purchase process, with a minimal scope. If I decided to write 40 micro-services that were chatting over the gRPC instead, this would become an over-engineered mess, which would be very hard to maintain. Also, don't buy the idea that microservices need to be completely isolated, have separate db, and run on an individual node, be pragmatic about your resource utilization.
Dealing with a similar bottleneck in production?
Reach out directly to Ivan Chepurnyi for a performance audit, indexer redesign, or second opinion.