Memory Pool
Waiting area for transactions
The size of your memory pool will differ depending on how long your node has been online and which nodes you are connected to.
The memory pool (mempool) is a waiting area for new transactions.
New transactions are stored in a node's memory pool while they're waiting to get mined on to the blockchain.
Do not rely on memory pool transactions. Not all transactions will make it from the memory pool (temporary storage) to the blockchain (permanent storage).
Purpose
Why does the memory pool exist?
The memory pool is used to sort out conflicting transactions.
You see, it's possible for two different transactions spending the same bitcoins to be inserted into different parts of the network at the same time. Some nodes will receive the one transaction first, and some nodes will receive the other transactions first:
Because both of these transactions are trying to spend the same bitcoins, only one of them should be written to the blockchain. So which of these conflicting transactions should make it into the blockchain?
This conflict is resolved when one of the nodes on the network mines the transactions from their memory pool into a block:
Upon receiving this newly-mined block, nodes will add this block on to the blockchain, and kick out any conflicting transactions from their memory pool.
So the memory pool is part of a sorting mechanism (mining) that prevents conflicting transactions from being written to the blockchain.
The memory pool plays a crucial role in preventing conflicting transactions from being written to the blockchain, and is the reason why you have to wait for transactions to get mined.
Entry
How does a transaction enter the memory pool?
A transaction can enter a node's memory pool in a number of ways:
1. Inserted into a local node
(common)
A new transaction can be inserted directly into a node on the network.
From here the node will broadcast the transaction to the other nodes on the network so they can add it to their memory pool too.
You can manually insert a transaction into your local Bitcoin Core node using the bitcoin-cli sendrawtransaction command. Alternatively, your wallet will insert your transaction into a node when you send someone bitcoins.
2. Received from another node
(common)
New transactions can be received from other nodes on the network.
Nodes continually broadcast the latest transactions they've received to the nodes they are connected to. So if a node advertises a transaction that your node does not have, your node will request it and add it to their memory pool too.
This process repeats until all nodes on the network have a copy of the latest transactions in their memory pools.
Only valid transactions can enter the memory pool. A node will check if each transaction they receive is valid (doesn't break any rules) before adding it to their memory pool or relaying it to the nodes they are connected to.
3. Re-entry after a chain reorganization
(uncommon)
Previously mined transactions can re-enter the memory pool during a chain reorganization.
Sometimes a node will perform a chain reorganization, where a new longest chain is found that replaces some of the blocks in the node's previous longest blockchain. If any of the transactions in the blocks being replaced are not found in the blocks of the new longest chain, they will get recycled back into your node's memory pool (and re-broadcast again) for the chance to get re-mined into a future block.
Exit
How does a transaction leave the memory pool?
There are a number of reasons why a transaction will leave the memory pool:
1. Mined
This is the goal for all memory pool transactions.
When a miner mines a new block of transactions, they will broadcast it to the other nodes on the network. When a node receives this block, any transactions in their memory pool that are inside that block will be removed from their memory pool and connected to the block instead.
In other words, transactions are moved from temporary storage (the memory pool) to permanent storage (the blockchain).
2. Mined Conflict
Nodes will remove any transactions from their memory pool that conflict with the transactions inside a block.
The transactions inside a mined block are considered "correct", so if a node has a transaction in their memory pool that spends the same bitcoins as a transaction inside a block, they will kick that transaction out the memory pool.
In other words, the memory pool has done its job as being part of the sorting mechanism for conflicting transactions.
All the descendants of a conflicting memory pool transaction will be removed at the same time.
3. Replaced
A transaction will be removed from the memory pool if it gets replaced by a new higher-fee transaction.
This will happen if an existing transaction in the memory pool has the replace-by-fee (RBF) setting, and then a new transaction gets broadcast to the network that spends the same bitcoins but with a suitably higher fee.
The new higher-fee version of the transaction is more likely to get mined on to the blockchain, so a node will kick out the old transaction in favor of the new one.
4. Time Limit
Each node has a time limit setting for how long they're willing to hold on to transactions in their memory pool.
So if a transaction in the memory pool doesn't get mined before it reaches the time limit, the node assumes the transaction probably isn't going to get mined and will remove it from their memory pool.
- The default time limit is 2 weeks.
- You can always rebroadcast a transaction to the network if it leaves the memory pools due to exceeding the expiry time.
5. Size Limit
Transactions will be removed from a node's memory pool when their memory pool reaches a certain size (in megabytes).
Each node has the ability to set a maximum size for their memory pool. So when their memory pool exceeds this limit, they will start removing the lowest-fee transactions from their memory pool to make space for higher-fee transactions instead.
So if there are more transactions floating around the network than can fit in to your node's memory pool, your node will only keep the highest-fee transactions available.
- The default size limit is 300 MB.
- Some nodes maintain very large memory pools, so it's unlikely that a low-fee transaction will completely leave the network due to other nodes' small memory pools.
- The memory pool also stores
