How Does the Checks-Effects-Interactions Pattern Prevent Reentrancy Attacks?

The Checks-Effects-Interactions pattern is a security best practice for writing smart contracts. It dictates that the contract should first perform all necessary checks (e.g. verifying permissions), then apply all effects to its internal state (e.g. updating balances), and only then interact with external contracts.

By updating the state before making an external call, the contract prevents a malicious actor from re-entering the function in a vulnerable state. This ensures that even if the external contract calls back, the internal state is already updated, and the conditions for the attack are no longer met.

How Can Reentrancy Attacks Be Prevented in Smart Contracts?
What Is the Defense against a Read-Only Reentrancy Attack?
What Specific State Changes (Effects) Should Be Prioritized before an External Call?
Are There Scenarios Where the Checks-Effects-Interactions Pattern Is Insufficient?
What Is a “Re-Entrancy Attack” and Why Is It a Common Smart Contract Vulnerability?
Can the CEI Pattern Prevent All Types of Reentrancy?
How Does This Pattern Relate to the Concept of Atomic Transactions in Blockchains?
What Is the Preventative Measure Called Checks-Effects-Interactions?

Glossar