Created
November 7, 2025 23:50
-
-
Save shivamka1/03f9a1052271f52a132604075e6217a8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Producer (web request handler) | |
| function request_purchase(productId): | |
| enqueue(topic="purchases", key=productId, value="BUY") | |
| # Consumer (single-threaded per key-partition) | |
| function purchase_worker(): | |
| loop: | |
| messages = poll(topic="purchases", timeout_ms=200) | |
| for msg in messages: # all messages for same key arrive in order | |
| productId = msg.key | |
| stock = query("SELECT stock FROM products WHERE id = ?", productId) | |
| if stock <= 0: | |
| continue | |
| exec("UPDATE products SET stock = stock - 1 WHERE id = ?", productId) |
Comments are disabled for this gist.