What is the best way to scan bitcoin mempool for a transaction?

As the title says, What is the best way to scan Bitcoin Mempool for a transaction?

There are some approaches we may come up with:

  1. Don’t scan the mempool, just ask Bitcoin-RPC for the new transactions of the address you want.
  2. Scan every transaction in the mempool, every few seconds, and find transactions related to your application.

Approach number 1:

It may seem right at first glance, but, it is not a good approach for most of the applications. Why? because it is not scalable. what if you have 1 million active users, and each user has 1 address? (Don’t tell me this is not gonna happen, it literally happened to me, maybe not 1 million, but a few thousand, and this approach stopped working for me, It was too slow, so forced me to switch my app to approach number 2)


Approach number 2:

Scan the mempool every few seconds for new transactions, and find your transactions inside it.
It works well, it scales, and it is fast.
But there is 1 huge problem here. What if the network is busy and you have 150k transactions in the mempool?
Well, here we can use cache, and stop checking duplicate transactions every time. Works well, almost. Why almost? because when the network is busy, you get hundreds of new transactions every second. and you need to send a request to Bitcoin core for each one of them to get their data and read them to see if they are yours or not. Bitcoin-RPC method getrawmempool only returns transaction hash and not data. so sending hundreds of new HTTP requests every second is a bit slow.


Do you have any idea how to improve?

Or perhaps a better method for checking addresses for new transactions?!

Leave a Comment