Hedera Processes Thousands of Transactions Per Second… See How that Number is Calculated
Headshot
Nov 01, 2023
by Ed Marquez
Developer Relations Engineer

At the time of this writing (November 2nd, 2023), the Arkhia Metrics site shows that the Hedera mainnet is processing 2,443 transactions per second (TPS), with more than 26 billion total transactions to date. The article Does Hedera’s TPS Include Transactions Generated by its Consensus Algorithm? Spoiler alert: NO states that the publicly reported transaction count on Hedera does not include the intra-node gossip communication needed to reach consensus. Only transactions submitted and paid for by external parties are included in the TPS count. But, how is that number calculated?

IMG1 TPS

For the curious minds out there, this article shows how anyone can calculate TPS on Hedera. It provides Python and JavaScript code that you can use to find the latest TPS number. The goal is to highlight how everyone can benefit from the transparency on-chain data provides.

If you are interested in seeing how Hedera's latest TPS compares to that of other Layer 1 and Layer 2 networks, see the Chainspect TPS Dashboard.

TPS Chainspect

As you read the article, keep in mind the equation below. Now, let’s do some fun math!

TPS = Transactions in X Blocks (#) / Duration of X Blocks (seconds)

1. Fetching the Block Data

As explained in the glossary of the Hedera Documentation, a block is simply a batch of transactions. Hedera’s hashgraph consensus algorithm inherently doesn't utilize blocks. However, to enhance compatibility with Ethereum Virtual Machine (EVM) tools and systems, the notion of "virtual blocks" was integrated on Hedera with HIP-415. Each block on Hedera represents transactions processed in a time window of approximately 2 seconds. 

The script uses the mirror node REST API to obtain the data for the last five blocks, providing a snapshot of roughly 10 seconds. You can modify the script to take as many blocks as you like. Here's how to fetch the data:

Python
  • Python
  • JavaScript
Code Snippet Background
import requests

def get_transactions_per_second():
  # Each block on Hedera is approximately 2 seconds
  numBlocks = 5
  # Use the mirror node REST API to get info from the last 5 blocks
  blocks_url = f"https://mainnet-public.mirrornode.hedera.com/api/v1/blocks?limit={numBlocks}"
  response = requests.get(blocks_url)
const axios = require("axios");

async function getTransactionsPerSecond() {
	// Each block on Hedera is approximately 2 seconds
    const numBlocks = 5;
    // Use the mirror node REST API to get info from the last 5 blocks
    const blocksUrl = `https://mainnet-public.mirrornode.hedera.com/api/v1/blocks?limit=${numBlocks}`;
    const response = await axios.get(blocksUrl);

Here’s a sample of the data that a query returns for a block:

Code Snippet Background
{
  "blocks": [
    {
      "count": 3628,
      "hapi_version": "0.40.0",
      "hash": "0x307f22d2fcdfa56925ac2288a639efceb4aae005ff8c9e961ff3ce6141ed1f18c780116b98bdf7ddcdbaa0087cb2449e",
      "name": "2023-10-20T15_37_40.009866397Z.rcd.gz",
      "number": 54899144,
      "previous_hash": "0x72412103ff7a3823bebbe913a1cd0b2de0c835a936ccd26ed2a1db2a75f0119503fe0517d0bbe322e3264c340e3ac799",
      "size": 887005,
      "timestamp": {
        "from": "1697816260.009866397",
        "to": "1697816261.999298266"
      },
      "gas_used": 0,
      "logs_bloom": "0x"
    }
  ],
  "links": {
    "next": "/api/v1/blocks?limit=1&block.number=lt:54899144"
  }
}

You can also see detailed information about blocks from a Hedera network explorer, like HashScan.

2. Adding Up the Transactions

Next, we need to find the total number of transactions processed over these five blocks. This is a straightforward process: loop through each block and keep a running tally of the transaction counts. Here’s the code that achieves this:

Python
  • Python
  • JavaScript
Code Snippet Background
  # Add up the transactions from each block
  blocks = response.json()['blocks']
  sum_of_transactions = sum(block['count'] for block in blocks)
  	// Add up the transactions from each block
    const blocks = response.data["blocks"];
    const sumOfTransactions = blocks.reduce((acc, block) => acc + block["count"], 0);

3. Calculating the Duration of the Time Window

We also need to determine the exact duration these five blocks took. Remember, while each block takes approximately 2 seconds, the actual duration might vary slightly. Keep in mind that each block also has a starting and ending timestamp.

We calculate the total duration of the time window by subtracting the starting timestamp of the oldest block from the ending timestamp of the newest block:

Python
  • Python
  • JavaScript
Code Snippet Background
  # Calculate the duration of the 5 blocks from the timestamps of the first and last blocks
  newest_block_to_timestamp = float(blocks[0]['timestamp']['to'])
  oldest_block_from_timestamp = float(blocks[-1]['timestamp']['from'])
  duration = newest_block_to_timestamp - oldest_block_from_timestamp
	// Calculate the duration of the 5 blocks from the timestamps of the first and last blocks
    const newestBlockToTimestamp = parseFloat(blocks[0]["timestamp"]["to"]);
    const oldestBlockFromTimestamp = parseFloat(blocks[blocks.length - 1]["timestamp"]["from"]);
    const duration = newestBlockToTimestamp - oldestBlockFromTimestamp;

4. Calculating TPS 🎉🎉

With the total number of transactions and the exact duration, it’s time for the grand reveal.

The final step is the easiest. We simply divide the total number of transactions by the duration (in seconds) to get the TPS number:

Python
  • Python
  • JavaScript
Code Snippet Background
  # Calculate the transactions per second
  transactions_per_second = sum_of_transactions / duration
  return transactions_per_second

TPS = get_transactions_per_second()
print(f"Hedera TPS:", TPS)
	// Calculate the transactions per second
    const transactionsPerSecond = sumOfTransactions / duration;
    return transactionsPerSecond;
}

(async () => {
	const TPS = await getTransactionsPerSecond();
	console.log(`Hedera TPS: ${TPS}`);
})();

To see the fruits of your labor, just run the scripts!

5. Wrapping Up

You learned how to easily calculate TPS on Hedera with just a few lines of code. Whether you're tracking performance, doing research, or satisfying your curiosity, this process shows how everyone can benefit from the transparency that on-chain data provides.

    Happy coding!

    Continue Learning