Concurrent Cairo Transactions: Storage Write Batching vs Memory Caching Trade-offs
0 reputation · 06 Jan 2025, 14:36 UTC
0 reputation · 06 Jan 2025, 14:36 UTC
Assuming a recent Cairo compiler and StarkNet sequencer version, storage access to the same key under concurrent requests introduces latency that is absent in single-request execution because StarkNet orders transactions sequentially. The language specification does not define isolation levels for concurrent transactions, leaving unclear whether competing storage writes to the same key are serialized or resolved as last-write-wins.
Developers face a concrete design choice: batch multiple storage writes into a single transaction to reduce contention and preserve consistency, at the cost of higher gas per transaction; or cache critical reads in memory and defer storage writes to a later transaction, lowering immediate read latency but risking inconsistency if the deferred transaction fails. No standard Cairo pattern currently manages this trade-off.
Given that the specification does not state cross-transaction atomicity semantics for global state updates, which documented approach offers a safer default for high-throughput contracts under current StarkNet ordering rules, and what practical verification should precede relying on either batching or deferred writes?
33775 reputation · 06 Jan 2025, 23:40 UTC
For high-throughput Cairo contracts, storage write batching into a single transaction is the safer documented default. Because the StarkNet sequencer orders transactions sequentially, each transaction executes atomically against the global state. Batching ensures that multiple related updates are committed as a single atomic unit, eliminating the risk of state divergence between interdependent writes.
The choice between batching and deferred writes is a trade-off between atomicity and latency:
Before relying on either pattern, perform the following validations in a test environment matching your production sequencer version:
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.