Substrate Best Practices for Building App-Chains
Substrate best practices cover runtime design, pallet choice, testing and upgrades. Learn the habits that keep an app-chain maintainable after launch.
On this page
Short answer
Substrate best practices are the design and operating habits that keep a custom blockchain maintainable: small focused pallets, explicit weight and fee design, thorough runtime testing, safe upgrade paths and clear governance. They matter because a runtime bug or a failed upgrade can halt a live chain, so teams plan for change from the first pallet.
What is Substrate?
Substrate is a framework for building custom blockchains, called app-chains, from modular pieces known as pallets. Each pallet handles one job, such as balances, staking or governance, and the runtime is the set of pallets plus the logic that ties them together. Teams pick the pallets they need, add their own, and launch a chain tuned to one product.
Best practices in Substrate are less about the framework itself and more about the habits that keep a chain healthy after launch. A chain that is easy to change, test and upgrade will survive its first year. A chain built as one large block of logic will not, because every fix touches everything. If you are new to the framework, what is Substrate explains the pieces before you design with them.
The practices below apply whether the chain is a public network or a permissioned one. They cover design, testing, upgrades and governance, and they are the same habits that experienced teams repeat on every project, because the cost of getting them wrong is a halted chain or a failed upgrade.
How should you design pallets?
You should design pallets as small, focused units, each owning one job and one set of storage items. A pallet that does balances should not also handle governance votes. When a pallet has one job, it is easier to test, easier to review and safer to change.
| Design choice | Good practice | Why it matters |
|---|---|---|
| Pallet size | One job per pallet | Changes stay small and safe |
| Storage | Only what the pallet owns | Upgrades do not clash |
| Weights | Measured, not guessed | Blocks stay within time limits |
| Errors | Named, specific errors | Failures are easy to trace |
| Events | Emit one per state change | Off-chain tools can follow the chain |
Keep the logic in a pallet simple enough that a new team member can read it in one sitting. If a pallet needs a long explanation, it is probably doing two jobs and should be split.
The same rule applies to the runtime as a whole: a runtime with twenty small pallets is easier to reason about than one with five large ones. For a walkthrough of putting pallets together, how to build a Substrate blockchain shows the steps.
What belongs in the runtime, and what does not?
Only logic that must be agreed on by every node belongs in the runtime. That means state changes, balances, votes and anything that decides who owns what. Everything else belongs off-chain, where it is cheaper to run and easier to change.
Heavy work is the most common thing to keep out of the runtime. Image processing, large file handling, price feeds and complex math all cost block time, and block time is limited. The usual pattern is to do the heavy work off-chain and put only the result on-chain, with a check that anyone can repeat.
A price feed, for example, can be fetched off-chain and submitted with a signature, while the runtime only checks the signature and stores the value.
The same split applies to data. Store only what the chain needs to agree on, such as hashes and balances, and keep the full documents or files in off-chain storage. This keeps storage small, keeps upgrades cheap and keeps the chain fast. When in doubt, ask whether every validator must run this code to agree on the next block; if not, it does not belong in the runtime.
How do you test a Substrate runtime?
You test a Substrate runtime in layers: unit tests for each pallet, integration tests for the runtime as a whole, and a testnet that runs the real chain before mainnet sees it. Skipping a layer is how bugs reach production.
- Unit tests per pallet. Test each call with valid and invalid inputs, and check that storage, events and errors behave as expected.
- Benchmarks for weights. Measure the real cost of each call so the weight values reflect what the chain actually does, not a guess.
- Try-runtime checks. Run the new runtime against a snapshot of live state to catch migration and storage problems before the upgrade.
- Testnet rehearsal. Deploy the upgrade on a testnet with the same pallets and data shape, and watch it for a full upgrade cycle.
Testing also means testing failure. Submit a call with the wrong origin, a bad signature and an out-of-range value, and confirm the chain rejects each one cleanly. A runtime that only passes happy-path tests is not tested. Costs matter here too: testing effort is part of the budget, and Substrate development cost breaks down where that budget goes.
How do you plan safe runtime upgrades?
You plan safe runtime upgrades by treating the upgrade as a migration, not a deploy. Substrate chains upgrade without a fork, which is powerful, but it also means a bad upgrade lands on the live chain with no easy undo. The plan starts long before the upgrade block.
- Version the storage. Give each storage layout a version number so the migration code knows what it is changing from.
- Write the migration first. Code the storage change, test it against a copy of live state, and measure how many blocks it needs.
- Keep the migration small. Split large changes across two upgrades if one would take too long to run.
- Rehearse on testnet. Run the full upgrade on a testnet with the same state shape, and confirm the chain keeps producing blocks.
- Have a rollback plan. Know in advance what happens if the upgrade fails, and who decides to act.
Governance is part of the plan. Decide who can propose and approve an upgrade, and how long the community has to review it. An upgrade that lands without review is a governance failure, not a technical one. For teams that want help setting this up, Cosmos SDK and Substrate app-chain development covers the full build and upgrade process.
Example (hypothetical): a storage migration that fits in one upgrade
Example (hypothetical): a team needs to change how its chain stores user profiles. The old layout holds 40,000 profile records, and the migration must rewrite each one. The team measures the rewrite at 200 records per block, so the full migration needs 40,000 divided by 200, which is 200 blocks.
The chain produces a block every 6 seconds, so 200 blocks take 200 times 6, which is 1,200 seconds, or 20 minutes. The team checks the block weight budget and confirms the migration fits inside the weight allowed for each block, with room left for normal transactions. It then rehearses the upgrade on a testnet with the same 40,000 records and watches the chain keep producing blocks through the whole 20 minutes.
Because the numbers were measured first, the team knows the upgrade fits in one window and does not need to be split. If the measurement had shown 2,000 blocks instead of 200, the team would have split the migration across two upgrades rather than risking a long pause in block production.
What are the common mistakes?
The most common Substrate mistakes are oversized pallets, unmeasured weights, untested migrations and upgrades without a rehearsal. Each of these is easy to avoid and expensive to fix after launch.
- One pallet doing many jobs. A large pallet is hard to test, hard to review and risky to change.
- Guessed weights. Weights that are too low let a block run over time; weights that are too high waste capacity.
- Heavy work in the runtime. Computation that validators must repeat on every block slows the whole chain.
- Untested migrations. A migration that was never run against live-shaped state is a gamble.
- No rollback plan. An upgrade without a plan for failure leaves the team deciding under pressure.
Key takeaways
- Keep pallets small and focused, each owning one job and its own storage
- Only logic every node must agree on belongs in the runtime
- Measure weights with benchmarks instead of guessing them
- Test runtimes in layers: unit, integration, try-runtime and testnet
- Treat upgrades as migrations with a rehearsal and a rollback plan
Frequently asked questions
Do I need Substrate to build a blockchain?
No, but it is a strong choice when you want a chain tuned to one product. You can also build on an existing chain with smart contracts, which is faster to start. Substrate fits when you need custom logic, custom fees or your own governance.
How many pallets should a runtime have?
There is no fixed number, but most teams end up with ten to thirty small pallets rather than a few large ones. The right count is the one where each pallet has a single clear job and a new team member can read it in one sitting.
What is a forkless runtime upgrade?
It is an upgrade where the chain swaps its runtime code without splitting into two chains. The new code is stored on-chain and activated at a set block. It is powerful, but it means a bad upgrade lands on the live chain, so rehearsal matters.
How long does a Substrate upgrade take?
It depends on the migration. A small upgrade with no storage changes takes a few blocks. A large migration can take hundreds of blocks. Measure the migration against a copy of live state first, then divide by the block time to get the real duration.
Can a bad pallet halt a chain?
Yes, if it panics on every block or consumes all block weight. That is why pallets are tested with invalid inputs and why weights are measured. A pallet that fails cleanly with a named error is far safer than one that halts the runtime.
Where should heavy computation live?
Off-chain, in a worker or a service that submits only the result to the chain. The runtime should check the result, not repeat the work. This keeps blocks fast and keeps the chain usable while the heavy work runs elsewhere.