DataStore Write Correctness: Handling Timeout-But-Committed Operations
Why the common "try the write, refund on failure" pattern in Roblox DataStore produces incorrect state — and the correct approach using cache-bypassed verification reads.
The Problem
In Roblox's DataStore system, write operations can timeout without the caller knowing whether the write actually committed. This creates a correctness problem: if the caller assumes the write failed and takes compensating action (like refunding an item), but the write actually succeeded, the system enters an inconsistent state.
This is especially dangerous in transactional contexts — purchases, trades, item grants — where "try the write, refund on failure" is the most common pattern used by Roblox developers.
Why the Common Pattern Fails
The standard approach looks something like this: attempt the DataStore write, catch any error, and if it fails, undo the local state change (refund the item, re-grant the currency, etc.). The assumption is that an error means the write didn't happen.
But DataStore operations can fail with a timeout after the write has already been committed on the backend. The caller receives an error, assumes the write failed, and issues a refund — but the write already went through. The result: duplicated items, inflated currency, or other forms of state corruption.
The Correct Approach
Instead of assuming failure on timeout, the correct approach is to perform a cache-bypassed verification read after a timeout error. This means reading the DataStore value directly (bypassing the local cache) to check whether the write actually committed.
If the verification read confirms the write went through, no compensating action is needed. If it confirms the write did not commit, then the refund is safe. This eliminates the class of bugs where timeout-but-committed operations lead to duplicated state.
Implications
This issue is fundamental to any system operating on Roblox's DataStore at scale. Games with high transaction volumes — trades, purchases, item grants — are especially vulnerable. The impact compounds over time: each incorrect refund introduces a small amount of state corruption, and in an economy with millions of transactions, these add up.
Identifying this pattern and implementing the correct verification approach was one of the key contributions I made to the economy integrity systems at BIG Games.
← Back to writing