A Shopify-style inventory protection system capable of handling 5,000+ concurrent checkout requests with zero overselling.
In standard E-commerce applications (like standard Rails), checking out a hot item creates a race condition.
- User A checks stock (sees 1).
- User B checks stock (sees 1).
- User A buys (stock -> 0).
- User B buys (stock -> -1). Overselling occurs.
FlashBurst solves this by moving the "source of truth" for inventory to Redis during the high-traffic window. It uses a Lua script to ensure that the Check and the Decrement happen in a single, atomic operation that no other request can interrupt.
[Client] -> [GraphQL API] -> [InventoryService (Redis)] -> [Sidekiq Job] -> [PostgreSQL]
- Gatekeeper:
InventoryServiceattempts to decrement inventory in Redis using Lua. - Atomic Lock: If Redis returns
0, the request is rejected immediately (4ms latency). - Async Persistence: If Redis returns
1, the stock is "reserved", and anOrderCreationJobis pushed to Sidekiq to update the persistent Postgres database.
- Language: Ruby 3.x / Rails 8 (API Mode)
- Concurrency: Redis (Lua Scripting)
- Queues: Sidekiq
- Database: PostgreSQL
- API: GraphQL (graphql-ruby)
I have included two Rake tasks to demonstrate the difference between "Standard Rails" and "FlashBurst Architecture."
Spawns 150 concurrent threads trying to buy 100 items using standard ActiveRecord decrement!.
bundle exec rake simulate:race