library / Blockchain ideas / BI0026
Network congestion alerts
Yes. Two standard RPC reads, transactions per slot against the last hour and recent priority fees against a limit you set, give your code a congestion signal it can act on, with thresholds, the cost to watch and a runnable main.ts.
Who uses it
A payments backend sees retries climb and confirmations stretch before anyone calls it congestion. It wants a signal that fires before the support queue does, from a program, with nobody watching a dashboard.
The signal here is a poller. It reads two things the chain already publishes through standard RPC: how many transactions each slot is carrying, and what the last 150 slots charged in priority fees. It compares both with a baseline and prints an ALERT line when either moves. Send the output to a pager, a chat webhook, or a variable your sender checks before it sets its own fee.
A hackathon team can ship the poller in an evening and add the part that is theirs: the alert channel, or the fee bump that reacts to it. Every call goes to services.carbium.sh with one key, and nothing is signed or sent.
What it watches
Neither number says congestion alone, so the monitor reads two and flags either one.
| Signal | Where it comes from | Alert when |
|---|---|---|
| Load per slot | getRecentPerformanceSamples: non-vote transactions divided by slots, for each 60 second sample | The newest sample is 1.5 times the median of the earlier samples |
| Fee pressure | getRecentPrioritizationFees: one entry per recent slot, in micro-lamports per compute unit | The 90th percentile passes 50,000 micro-lamports per compute unit |
Load is relative on purpose. A fixed count of transactions per slot goes stale as the chain's usual traffic shifts, so a ratio to the last hour holds up better. The fee limit is absolute because a sender cares about the price it will pay, and 50,000 is a starting value for you to tune.
A slot lasts about 200 ms, so one 60 second sample covers around 300 slots. Each sample is a finished minute, so the newest one is compared with the median of the rest.
Called with no address, the fee read covers every transaction, and most slots show a minimum of zero because some transaction in them paid no extra fee. Pass the address of a busy account your own transactions write to, such as a popular pool, and the read narrows to transactions that write-lock it. That view matches what your sends will face.
The build
One file, @solana/kit and what Node 24 ships. It is read-only.
- Read the last 60 performance samples, one per minute, newest first.
- Turn each sample into transactions per slot. Mainnet blocks carry no vote transactions since Alpenglow, so the code uses numNonVoteTransactions and the total agrees with it.
- Compare the newest with the median of the rest. A ratio over LOAD_RATIO (default 1.5) is an alert.
- Call getRecentPrioritizationFees once with no address and once per address given on the command line. Take the 90th percentile of each list. Above FEE_ALERT (default 50,000) is an alert.
- Print one status line per read and an ALERT line per signal past its limit, then wait EVERY_SECONDS (default 10) and poll again.
// BI0026 · Network congestion alerts// https://carbium.sh/library/blockchainideas/network-congestion-alerts// Copyright (c) Carbium Corporation. MIT License (LICENSE). Keep this notice.// SPDX-License-Identifier: MITimport { address, createDefaultRpcTransport, createSolanaRpcFromTransport } from "@solana/kit";const key = process.env.CARBIUM_KEY;if (!key) { console.error("Set CARBIUM_KEY to your Carbium API key."); process.exit(1);}const rpc = createSolanaRpcFromTransport( createDefaultRpcTransport({ url: process.env.CARBIUM_RPC_URL ?? "https://services.carbium.sh", headers: { "x-api-key": key }, }),);// Busy accounts your own transactions write to; the empty list reads the whole chain.const hot = process.argv.slice(2).map((a) => address(a));const LOAD_RATIO = Number(process.env.LOAD_RATIO ?? 1.5);const FEE_ALERT = Number(process.env.FEE_ALERT ?? 50_000); // micro-lamports per compute unitconst EVERY_MS = Number(process.env.EVERY_SECONDS ?? 10) * 1000;const sorted = (xs: number[]) => [...xs].sort((a, b) => a - b);const median = (xs: number[]) => sorted(xs)[Math.floor(xs.length / 2)] ?? 0;const p90 = (xs: number[]) => sorted(xs)[Math.floor(0.9 * (xs.length - 1))] ?? 0;const why = (r: PromiseRejectedResult) => (r.reason instanceof Error ? r.reason.message : String(r.reason));async function poll(): Promise<void> { const [perf, fees] = await Promise.all([ Promise.allSettled([rpc.getRecentPerformanceSamples(60).send()]), Promise.allSettled( [[], ...hot.map((a) => [a])].map((list) => rpc.getRecentPrioritizationFees(list).send()), ), ]); const stamp = new Date().toISOString().slice(11, 19); const [samples] = perf; if (samples.status === "rejected") console.error(`samples failed: ${why(samples)}`); else { const load = samples.value .map((s) => Number(s.numNonVoteTransactions ?? s.numTransactions) / Number(s.numSlots)) .filter(Number.isFinite); if (load.length < 10) console.log(`${stamp} too few samples for a baseline`); else { const [now, ...before] = load; const ratio = now / median(before); console.log(`${stamp} load ${now.toFixed(0)} tx/slot, median ${median(before).toFixed(0)}, ratio ${ratio.toFixed(2)}`); if (ratio > LOAD_RATIO) console.log("ALERT load per slot above its hour"); } } fees.forEach((r, i) => { const label = i === 0 ? "whole chain" : hot[i - 1]; if (r.status === "rejected") return console.error(`fees ${label} failed: ${why(r)}`); const top = p90(r.value.map((f) => Number(f.prioritizationFee))); console.log(`${stamp} fee p90 ${top} micro-lamports/CU over ${r.value.length} slots (${label})`); if (top > FEE_ALERT) console.log(`ALERT fee pressure on ${label}`); });}for (;;) { await poll(); await new Promise((r) => setTimeout(r, EVERY_MS));}
run: CARBIUM_KEY=<YOUR_KEY> node main.ts 58oQChx4yWmvKdwLLZzBi4ChoCc2fqCUWBkwMihLYQo2
The reads run together and settle one by one, so a failed fee read for one address prints its message while the other lines still report.
Tuning and cost
One poll is one performance call plus one fee call for the whole chain and one per address you pass. With one address that is three calls every 10 seconds, or 18 calls a minute. RPC work draws on your credits, and 18 calls a minute is a small, steady draw you can read off your credit use after a day. Limits are per account, so other tools on the same keys share the budget.
The polling interval follows the data. Performance samples change once a minute, and the fee list spans 150 slots, about 30 seconds. Polling every 10 to 30 seconds catches both, and polling faster reads the same entries again.
Run the monitor quiet for a day before wiring it to a channel. Note the usual ratio and the usual fee for your address, then set LOAD_RATIO and FEE_ALERT above them. The default FEE_ALERT of 50,000 sits well above the 10,000 that a busy pool's 90th percentile showed on a quiet run, so a small bump stays silent. Move it to match what your own quiet day shows.
What to add next
- Alert delivery: post the ALERT line to a chat webhook, with a cool-down so a long surge sends one message.
- A sender hook: keep the latest 90th percentile in a variable and add it to your transactions' compute unit price. Priority fee per compute unit covers that field.
- A slot-gap signal over the WebSocket: slotSubscribe opens at 5 credits, then costs 20 credits per MiB sent. A gap in slot numbers shows a slot its leader never produced, which is a skip signal the polled reads do not give.
- History: write each poll to node:sqlite so you can chart how often alerts fired and set limits from real days.
Where it ends
- It reports what has happened. A sample is a finished minute and the fee list is the last 150 slots, so the monitor trails the chain by seconds to a minute and cannot predict a spike.
- The load ratio fires on any surge, including a healthy one such as a busy mint. It says traffic is up and cannot tell whether transactions are failing to land.
- Fees come from transactions that landed. A transaction dropped before landing leaves no entry, so a wave of drops is invisible here.
- A per-address fee read shows contention on the addresses you list and says little about the rest of the chain.
- Your own retries, 429s and confirmation times live in your own logs, out of the monitor's sight, since it reads only the chain.
When it goes wrong
| what goes wrong | what to do |
|---|---|
| Every poll shows a fee of 0. | The whole-chain read often lands on zero because some transaction in every slot pays no extra fee. Pass the address of a busy account you write to and read that line. |
| The load line is missing and a samples message prints. | Read the message. A 429 carries a retry-after hint, and the next poll retries after the interval. If it repeats, raise EVERY_SECONDS and check what else on the account is calling, since the limit is shared across its keys. |
| Every call answers 401. | The key is unknown at the door. Check that CARBIUM_KEY holds the whole key with nothing cut off. |
| The ALERT line prints on every poll during a long surge. | Keep the last state and print only when it changes, or add a cool-down before the line reaches a channel. |
| The load ratio alerts at the chain's ordinary daily peak. | The ratio compares with the last hour only, so a regular daily climb reads as a surge. Check the median printed on the status line, and set LOAD_RATIO from a full day of output. |
Questions
Is there a congestion event I can subscribe to?
No node pushes a congestion flag. You derive one: poll the two reads here, or watch the gaps between slot notifications on the WebSocket. This monitor polls because both reads are plain HTTPS calls.
How often should I poll?
Performance samples change once a minute and the fee list spans about 30 seconds, so every 10 to 30 seconds catches both. Faster polling re-reads the same data.
What fee should my own transactions pay during an alert?
Start from the 90th percentile the monitor prints for your busy account, in micro-lamports per compute unit, and use it as the compute unit price. Raise it again if transactions still miss.
Does it need a WebSocket?
No. With one address it sends 18 read calls a minute and opens no socket, which keeps it clear of the socket cap (Free carries 5). Add the slot subscription later if you want the skip signal.