
Solidity Transient Storage: Cut Gas for Transaction-Scoped State
Solidity transient storage is transaction-scoped state introduced by EIP-1153. On Cancun-compatible EVMs, use it for values that must persist across calls within one transaction but should not survive into the next—such as a reentrancy lock or temporary callback context. Unlike persistent storage, transient state resets automatically at transaction end and avoids the expensive persistent-storage write path. It is not a drop-in replacement for mappings or durable contract state: choose it only when the required lifetime is the current transaction, and verify compiler and chain support before deploying.
Environment & Baseline Specs
This guide targets Solidity ^0.8.28 compiled for the Cancun EVM, where the TLOAD and TSTORE opcodes from EIP-1153 are available. Transient state variables require a compiler version that supports the transient keyword.
Example Foundry configuration:
[profile.default]
solc_version = "0.8.28"
evm_version = "cancun"
The destination chain must also support Cancun semantics. Compiler support alone does not make transient opcodes valid on a chain that has not activated EIP-1153.
Transient storage is not memory. Memory is local to one call frame and disappears when that call returns. Transient storage can be read by later calls to the same contract during the same transaction, then clears at transaction end.
Why Persistent Storage Is Wasteful for Temporary State
A common pattern uses a regular storage variable as a lock:
contract PersistentLockExample {
uint256 private _locked; // Defaults to zero.
modifier nonReentrant() {
require(_locked == 0, "locked");
_locked = 1;
_;
_locked = 0;
}
function execute() external nonReentrant {
// Sensitive operation.
}
}
This pattern can protect a function, but _locked is only meaningful while the current transaction is executing the guarded section. Persisting it in contract storage adds storage-operation costs for a value that should not survive the transaction. Incorrect control flow can also leave the lock set after a successful call; a revert rolls back the state changes, but a missing reset on a successful path can block future calls.
The lock must still be reset after the guarded body returns. Transient storage clears automatically at transaction end, but not when a function returns. That distinction matters for contracts called more than once in a single transaction, including through routers, multicalls, or callbacks.
Production Pattern: Transient Reentrancy Lock
The following contract uses a transient variable for transaction-scoped lock state:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;
contract TransientLockExample {
uint256 private transient _locked;
error ReentrantCall();
modifier nonReentrant() {
if (_locked != 0) revert ReentrantCall();
_locked = 1;
_;
_locked = 0;
}
function execute() external nonReentrant {
// Perform state changes and any required interactions.
}
}
The modifier follows checks-effects-interactions discipline where applicable: it sets the lock before the function body and clears it after normal completion. If the function body reverts, the revert rolls back its transient writes as well. If it succeeds, the explicit reset allows a later call to execute in the same transaction.
For contracts with multiple guarded entry points, apply the same lock consistently to every function that shares the protected invariant. A lock on one function does not protect an unguarded function that can reach the same sensitive state.
When a Transient Guard Is Not Appropriate
Transient state is not suitable for a flag that must remain set between transactions, nor for state that users or other contracts must read later. It is also not a universal improvement: if the relevant state is already being written persistently for business logic, adding a transient mirror may increase complexity without saving meaningful gas.
For reusable production code, consider OpenZeppelin’s transient reentrancy guard when the project’s OpenZeppelin version and chain targets support it. Keep the implementation’s storage context and inheritance assumptions in mind, especially behind proxies.
Gas Mechanics and Trade-Offs
EIP-1153 provides TLOAD and TSTORE, each priced at 100 gas. Persistent storage costs depend on the slot’s current and original values, whether it is warm, and refund rules. A first cold SLOAD costs 2,100 gas; persistent writes can cost substantially more, especially when changing a zero slot to nonzero. Refunds may reduce net cost, but they are capped and should not be treated as guaranteed savings.
The table compares the storage operations a simple lock performs. It is an opcode-level comparison, not a claim that every whole function will save exactly the displayed amount.
| Approach | Read cost | Write cost | Lifetime | Main trade-off |
|---|---|---|---|---|
| Persistent lock, cold slot | 2,100 gas for cold SLOAD | SSTORE cost depends on original and new values; zero-to-nonzero is typically 20,000 gas | Across transactions | Durable state; expensive for temporary data |
| Transient lock | 100 gas per TLOAD | 100 gas per TSTORE | Current transaction | Low-cost temporary state; requires Cancun support |
| Memory variable | No storage opcode | No storage opcode | Current call frame | Cheapest for local work, but cannot coordinate later calls |
A persistent lock’s exact cost depends on whether its slot is warm, whether the slot starts at zero, and how the refund cap applies. A transient lock that reads once, writes once, and resets once uses three transient operations—300 gas for those operations—before the surrounding function’s execution costs. The persistent version can cost far more, but benchmark the complete function with the compiler settings and target chain you actually deploy.
Transient storage is one gas tool among many. For broader optimization context, see Optimizing Gas Usage in Solidity Smart Contracts and Enhancing Solidity Smart Contract Performance through Efficient Data Structures.
Production Pitfalls and Edge Cases
The lock must be reset on successful return
Transaction-end cleanup is not function-return cleanup. If a successful call leaves _locked set, a second call to the guarded function later in the same transaction will revert. Explicitly clear the lock on the normal path, as shown above. Reverts roll back transient writes made during the reverted call.
Transient state follows the executing contract’s context
Under delegatecall, code executes using the caller’s storage context. Transient state accessed by delegated implementation code therefore belongs to the calling contract’s context, not the implementation’s separate context. This can be useful for a proxy-wide lock, but it also means a shared transient slot can collide with another delegated module’s transient slot. Coordinate slot ownership across facets and implementations just as carefully as storage layout.
It is not durable state or a cross-transaction cache
A transaction-scoped value cannot safely record that a user has completed an action for future transactions. Do not use transient storage to replace a mapping, nonce, accounting total, or other durable state. If the value needs to survive, it belongs in persistent storage.
Chain support is part of correctness
Deploying bytecode containing TLOAD or TSTORE to an EVM that does not support Cancun opcodes can fail. Confirm the target network’s fork schedule and test against the same EVM version used by deployment tooling.
Compiler-generated layout still matters
Transient variables have a distinct transient storage space, but code organization, inheritance, proxy patterns, and library composition still require deliberate slot management. Inspect compiler output and avoid assumptions that a transient variable is interchangeable with a regular state variable.
Verification and Benchmarking
Test both reentrancy behavior and same-transaction reuse. A useful test should prove that a nested call fails while the lock is held, that a normal call succeeds, and that a second call in the same transaction succeeds after the first call clears the lock. A test that only submits separate transactions will not catch a missing reset.
Run the suite with the intended EVM target:
forge test --evm-version cancun
Add fuzz tests around public entry points and callback sequences. For gas comparisons, create otherwise identical persistent-lock and transient-lock implementations, run both through the same harness, and record the exact compiler version, optimizer settings, EVM version, and transaction path. Avoid comparing only opcode costs when the real question is whole-function deployment or execution cost.
When the contract is upgradeable or uses delegatecall, review the state layout and call context as part of the test plan. Existing guidance on Designing Safe and Predictable Solidity Storage Layouts is relevant because transient slot ownership should also be intentional.
Frequently Asked Questions (FAQ)
Does Solidity transient storage reset after every external call?
No. Transient storage persists across calls to the same contract during a transaction and resets at the end of that transaction. It is therefore suitable for temporary coordination between calls, but not for values that should disappear when a single function returns.
Is transient storage cheaper than a regular storage variable?
TLOAD and TSTORE each cost 100 gas under EIP-1153. Persistent storage operations have value-dependent costs, and a cold SLOAD costs 2,100 gas, so transient storage can be much cheaper for temporary state. The exact whole-function saving depends on the slot’s state, access warmth, refunds, and surrounding code.
Can I use Solidity transient variables on any EVM chain?
No. The chain must support the Cancun-era EIP-1153 opcodes. Confirm the target network’s fork support and set the compiler EVM version accordingly before deploying code that uses transient storage.