Skip to content

Instantly share code, notes, and snippets.

@shivamka1
Created November 7, 2025 23:50
Show Gist options
  • Select an option

  • Save shivamka1/03f9a1052271f52a132604075e6217a8 to your computer and use it in GitHub Desktop.

Select an option

Save shivamka1/03f9a1052271f52a132604075e6217a8 to your computer and use it in GitHub Desktop.
# 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.