Production War Stories

I Shipped a Race Condition That Double-Charged Customers (And Other War Stories)

Nobody writes blog posts about the bugs they shipped. The internet is full of “how I built X” and suspiciously empty of “how I broke X and spent 14 hours pretending it wasn’t my fault.” Here are mine. The Race Condition: Two Cashiers, One Item This one happened in Polaris, the ERP system I built for retail businesses. Real money, real transactions, real angry shop owners. The setup: two cashiers at different terminals. Both scan the same product. Both hit “Complete Sale” within 200ms of each other. The inventory says there’s one left. ...

November 15, 2025 · 8 min · Muhammad Hassan Raza
Django ORM optimization diagram

Optimizing Django ORM Queries for Large Applications

Introduction As my POS system scaled, performance bottlenecks became increasingly apparent. Customers began complaining about slow bill generation times, which made checkout frustratingly sluggish. After profiling my Django APIs, I discovered that inefficient ORM queries were causing unnecessary database overhead, leading to significant slowdowns. This prompted a deep dive into ORM optimizations to reduce query execution time and improve the overall user experience. In this post, I’ll share how I optimized my Django ORM queries using select_related, prefetch_related, bulk operations, and query profiling tools to enhance the efficiency of my refund API and bill generation process. ...

February 17, 2025 · 3 min · Muhammad Hassan Raza
Django signal optimization diagram

Optimizing Django Signals for Efficient Ledger Recalculations

Introduction When dealing with financial transactions in Django applications, maintaining an accurate ledger is critical. However, inefficient signal handling can lead to performance bottlenecks. In this article, we’ll explore an optimized approach to recalculating ledger balances while ensuring minimal database impact. The Problem A typical ledger system requires recalculating balances when transactions are inserted, updated, or deleted. Using Django signals, many implementations trigger redundant recalculations, causing excessive database queries and slowing down the application. ...

February 15, 2025 · 2 min · Muhammad Hassan Raza