Deleted Before Commit
An engineer moves the cache invalidation inside the database transaction so that "either both happen or neither does".
def set_price(sku: str, price: int) -> None:
with db.transaction(): # 1
db.execute(
"UPDATE products SET price = %s WHERE sku = %s", (price, sku)
) # 2
cache.delete(f"price:{sku}") # 3
# COMMIT happens here, when the block exits 4
def get_price(sku: str) -> int:
v = cache.get(f"price:{sku}") # 5
if v is None:
v = db.query_one(
"SELECT price FROM products WHERE sku = %s", (sku,)
) # 6
cache.set(f"price:{sku}", v, ex=300) # 7
return v
A reader runs concurrently with one writer. What is the worst outcome this ordering allows, and how long does it last?
Sign in to answer questions and track your progress
Sign In