library / Integrations / I0001
Slack slot-lag alerts
A small Node watcher compares the slot on your Carbium RPC with a reference RPC every 10 seconds, posts to a Slack incoming webhook when the lag passes 2 slots and again when it recovers, and keeps a health file your app can read.
What the watcher does
The watcher is one Node process whose only dependency is @solana/kit. Every 10 seconds it reads the current slot from two RPC endpoints and asks Carbium for its health, then compares the slot difference with a limit. The first endpoint is Carbium, the one your app calls. The second is a reference RPC you trust, given as a URL in an environment variable.
It is always in one of four states and talks to Slack only when the state changes.
| State | When | Slack message |
|---|---|---|
| ok | All three calls answered and the reference slot minus the Carbium slot is at or under the limit. | One, when the watcher comes back from another state. |
| behind | All three calls answered and the lag is over the limit (default 2 slots). | One, when it enters this state. |
| unreachable | A Carbium call (getSlot or getHealth) failed with anything other than a 429, whatever the reference did. A getHealth answer that calls the node unhealthy counts as a failure. | One, when it enters this state. |
| reference-down | Both Carbium calls answered and the reference getSlot failed, a 429 included. Carbium stays usable. | One, when it enters this state. |
A 429 on a Carbium call skips that poll and leaves the state where it was. A 429 from the reference counts as a failed reference call. On every finished poll the watcher also writes health.json with the state, both slots, the lag and a UTC timestamp. Your app or proxy reads that file to choose which URL to call. The watcher moves no traffic itself.
The Solana side: two slots at one commitment
Carbium's RPC endpoint is https://services.carbium.sh, and the watcher sends the key in the x-api-key header.
Both slot reads use commitment processed, the newest slot each node has seen.
The three calls run together in Promise.allSettled, so one failure does not hide the others. Two different nodes see a new slot at different moments, so their processed slots can differ by a few even when both are healthy. That is why the default limit is 2: a limit of 0 or 1 would alert on that noise. If a healthy pair keeps showing 3, raise it with LAG_LIMIT.
getHealth on Carbium returns ok when the node judges itself healthy and an error otherwise. An error from either Carbium call puts the watcher in unreachable, even when the reference failed too, so that state can mean an unhealthy node as well as a node that did not answer. A failed reference getSlot puts it in reference-down, which says nothing against Carbium. Each call has a 5 second deadline, so a call that takes longer counts as failed. The log line and the Slack text name each failed call.
The Slack side: one message per change
In the Slack app settings, turn on Incoming Webhooks and add a webhook to the channel that should receive alerts. Put the URL it gives you in SLACK_WEBHOOK_URL. Treat that URL as a secret, since whoever holds it can post to the channel.
The watcher POSTs {"text": "..."} to it. The text names the new state and carries both slots, the lag, the limit, the UTC time and any failed calls, for example Carbium RPC is behind: carbium slot 100, reference slot 103, lag 3 (limit 2) at 2026-11-21T21:45:37.946Z.
The state variable moves only after Slack has answered with a success status. If Slack is down or the URL is wrong, the next poll tries again with the current state. The watcher starts in ok, so a start while the node is already behind alerts at once and a healthy start says nothing.
The watcher
Install the one dependency with npm install @solana/[email protected], and set "type": "module" in package.json. Node 24 strips the types on load. The file reads five variables. CARBIUM_KEY, REFERENCE_RPC_URL and SLACK_WEBHOOK_URL are required, and it exits with a message if any is missing. CARBIUM_RPC_URL and LAG_LIMIT are optional.
// I0001 · Slack slot-lag alerts// https://carbium.sh/library/integrate/slack-slot-lag-alerts// Copyright (c) Carbium Corporation. MIT License (LICENSE). Keep this notice.// SPDX-License-Identifier: MITimport { createDefaultRpcTransport, createSolanaRpc, createSolanaRpcFromTransport, isSolanaError, SOLANA_ERROR__RPC__TRANSPORT_HTTP_ERROR,} from "@solana/kit";import { rename, writeFile } from "node:fs/promises";const key = process.env.CARBIUM_KEY;const referenceUrl = process.env.REFERENCE_RPC_URL;const webhook = process.env.SLACK_WEBHOOK_URL;if (!key || !referenceUrl || !webhook) { console.error("Set CARBIUM_KEY, REFERENCE_RPC_URL and SLACK_WEBHOOK_URL."); process.exit(1);}const hookUrl: string = webhook;const limit = Number(process.env.LAG_LIMIT ?? 2);if (!Number.isFinite(limit)) { console.error("LAG_LIMIT is not a number."); process.exit(1); }const POLL_MS = 10_000;const abortSignal = () => AbortSignal.timeout(5_000); // a call past 5 s counts as failed// One client per URL, built once at start.const carbium = createSolanaRpcFromTransport( createDefaultRpcTransport({ url: process.env.CARBIUM_RPC_URL ?? "https://services.carbium.sh", headers: { "x-api-key": key }, }),);const reference = createSolanaRpc(referenceUrl);type State = "ok" | "behind" | "unreachable" | "reference-down";const title: Record<State, string> = { ok: "Carbium RPC is ok", behind: "Carbium RPC is behind", unreachable: "Carbium RPC failed a call", "reference-down": "Reference RPC failed, Carbium answered",};let state: State = "ok"; // assume healthy; the first bad poll alerts// Seconds a 429 asks us to wait, or 0 for any other error.const tooMany = (e: unknown) => isSolanaError(e, SOLANA_ERROR__RPC__TRANSPORT_HTTP_ERROR) && e.context.statusCode === 429 ? Number(e.context.headers.get("retry-after")) || 1 : 0;const why = (e: unknown) => isSolanaError(e, SOLANA_ERROR__RPC__TRANSPORT_HTTP_ERROR) ? `HTTP error (${e.context.statusCode})` : e instanceof Error ? e.message : String(e);async function poll(): Promise<number> { const commitment = "processed"; const names = ["carbium getSlot", "reference getSlot", "carbium getHealth"]; const [c, r, h] = await Promise.allSettled([ carbium.getSlot({ commitment }).send({ abortSignal: abortSignal() }), reference.getSlot({ commitment }).send({ abortSignal: abortSignal() }), carbium.getHealth().send({ abortSignal: abortSignal() }), ]); const failed = [c, r, h].flatMap((x, i) => (x.status === "rejected" ? [{ call: names[i], err: x.reason as unknown }] : [])); const limited = failed.filter((f) => f.call.startsWith("carbium") && tooMany(f.err)); // a reference 429 counts as a reference failure if (limited.length) { const wait = Math.max(POLL_MS, ...limited.map((f) => tooMany(f.err) * 1000)); console.error(`poll skipped: 429 on ${limited.map((f) => f.call).join(", ")}, next poll in ${wait / 1000} s`); return wait; // state unchanged } const problem = failed.map((f) => `${f.call}: ${why(f.err)}`).join(", "); if (problem) console.error("poll failed:", problem); const carbiumSlot = c.status === "fulfilled" ? c.value : null; const referenceSlot = r.status === "fulfilled" ? r.value : null; const lag = carbiumSlot !== null && referenceSlot !== null ? Number(referenceSlot - carbiumSlot) : null; const next: State = c.status === "rejected" || h.status === "rejected" ? "unreachable" : lag === null ? "reference-down" : lag > limit ? "behind" : "ok"; const at = new Date().toISOString(); const body = JSON.stringify({ state: next, lag, carbiumSlot: carbiumSlot?.toString() ?? null, referenceSlot: referenceSlot?.toString() ?? null, at }); await writeFile("health.json.tmp", body); await rename("health.json.tmp", "health.json"); if (next !== state) { const text = `${title[next]}: carbium slot ${carbiumSlot ?? "n/a"}, reference slot ${referenceSlot ?? "n/a"}, lag ${lag ?? "n/a"} (limit ${limit}) at ${at}${problem ? `; failed: ${problem}` : ""}`; const res = await fetch(hookUrl, { method: "POST", headers: { "content-type": "application/json" }, signal: abortSignal(), body: JSON.stringify({ text }) }); if (!res.ok) throw new Error(`Slack answered ${res.status}`); state = next; // only after Slack accepted it, so a failed post is retried } return POLL_MS;}for (;;) { const wait = await poll().catch((err: unknown) => { console.error("alert or write failed:", err instanceof Error ? err.message : err); return POLL_MS; }); await new Promise((r) => setTimeout(r, wait));}
run: node main.ts
Run it and read the health file
Export the variables and start node main.ts under a supervisor such as a systemd unit, so it survives a crash or a reboot. To see the alert path work, start it once with LAG_LIMIT=-100: every real lag is above that, so the first poll sends one behind message and later polls add none.
The file lands in the working directory, written under a temporary name and renamed, so a reader sees the old file or the new one in full. A reader in your app parses the JSON, uses the Carbium URL when state is ok or reference-down and a fallback URL otherwise, and treats a file whose at is over a minute old as unknown.
What it costs against your limits
getSlot and getHealth are cached reads at 1 cell each, and Free allows 5 req/s of them. The watcher sends two calls to Carbium every 10 seconds, which is 12 cells a minute or 0.2 req/s, about 4% of Free's rate.
Limits are per account, so all your keys share one budget. When app traffic on other keys spends it, a watcher call can get a 429 with a retry-after hint. The watcher sleeps that long, at least 10 seconds, and tries again with the state untouched.
Where it ends
- It does not rotate traffic. It stops at health.json and Slack; moving traffic belongs to your load balancer, proxy or app client, which can read the file.
- It compares Carbium with one reference RPC that stands in for the cluster, and it reads no gossip.
- A poll every 10 seconds sees the state at that moment. A dip that starts and ends between two polls goes unseen.
- A reference that is itself behind makes the lag look smaller, and a negative lag, with Carbium ahead of the reference, does not alert.
- It reads the processed slot and getHealth only, so it says nothing about whether your own calls succeed.
- A watcher that dies sends no alert. The at field in health.json going stale is the only trace, so supervise the process and check that field from somewhere else.
When it goes wrong
| what goes wrong | what to do |
|---|---|
| Slack answers with an error status, the log shows alert or write failed: Slack answered 404, and the alert is retried every 10 seconds. | The webhook URL is wrong, cut short or revoked. Create a new incoming webhook, copy the whole URL into SLACK_WEBHOOK_URL and restart. The next poll sends the current state. |
| A healthy node alerts behind with a lag of 3 or 4, then recovers. | Raise LAG_LIMIT to 3 or 4; see the Solana section for why healthy nodes differ. Check that both calls use processed: a Carbium read at a higher commitment trails the reference and shows a false lag, and a reference read at a higher commitment hides real lag. |
| The watcher sits in unreachable or reference-down and the log shows an HTTP error status. | The log line names the call, for example poll failed: reference getSlot: HTTP error (401). For a carbium call, check that CARBIUM_KEY holds the key itself and that nothing else sets CARBIUM_RPC_URL. The key goes in the `x-api-key` header, or as ?api-key= on the URL. For a reference call, check REFERENCE_RPC_URL and its provider's key and limit; the state is reference-down and Carbium stays usable. |
| health.json stops updating while the process is still running. | Polls are being skipped on a Carbium 429, and the log line poll skipped: 429 on carbium getSlot, next poll in 10 s names the call and the wait. Your account's budget is spent, usually by app traffic on other keys: lower that app traffic or move to a plan with a higher rate. |
| Slack gets a message on every crossing while the lag hovers around the limit. | The limit has no recovery margin. Raise LAG_LIMIT, or make behind end only when the lag is at or under 1. |
| Your app reads health.json and sometimes fails to parse it. | A second watcher was started in the same directory and shares health.json.tmp, so two writers overlap. Run one watcher per directory. |
Questions
Does it compare against the gossip cluster itself?
No. It compares the Carbium slot with the slot of one reference RPC that you name, both at processed. That reference stands in for the cluster.
How do I know the watcher itself is still running?
Read the at field in health.json: every finished poll stamps it. If it is over a minute old, the process has died or is skipping polls on a 429, so check the supervisor and the log.
Could a WebSocket slot subscription replace the polling?
It could. Carbium's WebSocket takes slot subscriptions, which open at 5 credits and then cost 20 credits per MiB sent. For this alert, two cached reads per poll are simpler to run and to reason about.
What does it use on the Free plan?
Two 1 cell reads every 10 seconds, so 12 cells a minute, against 5 req/s for this class of call.