Deadlocks on the cart database
On a platform that handles up to 9,000 orders a day, refreshing the cart (recalculating prices, coupons, shipping) writes many rows within a single transaction. The result: locks piling up and deadlocks in production.
Bruno tackled it in several steps:
- A deterministic write order (December 2022). The writes were fired in parallel (
Promise.all) within the same transaction, so in an order that changed from one request to the next — the classic recipe for a deadlock. He made them sequential and always in the same order. Trade-off: a little less parallelism, far fewer deadlocks. - Shorter transactions (April 2023). The SQL queries are grouped into a transaction opened as late as possible, and catalog data is cached in Redis so that no network call is made while the locks are held.
- A timeout on every transaction (2025). First on the cart refresh, then generalised in the shared core of the microservices: a Moleculer middleware opens the transaction, commits or rolls it back automatically, and abandons it past a deadline (a race between the action and a timeout). Trade-off: a request that is too slow fails outright instead of blocking all the others.