Case studies

Building a realtime trading backend

Terminal was a realtime trading and token-discovery product for Solana. I worked on the backend and infrastructure with one other backend engineer, and we used AI agents heavily to move fast. The first production version took under three months.

My part was the indexer: the service that reads blockchain events and turns them into trades, token stats, rankings and live updates in the browser. I also looked after the infrastructure it ran on.

The problem

New tokens on pump.fun and PumpSwap appear faster than anyone can read them, and most are spam. Users needed to see which ones had real activity right now, and they needed to see it without delay. On a trading screen, late data is wrong data.

We were processing over a million events a day, with peaks around 500 to 600 a second.

One server doing everything

The first version was a single server. For every trade it parsed the transaction, wrote to the database, broadcast to users and recalculated rankings. It could not keep up, even after we scaled it, and when it stopped, everything stopped with it.

So I split it into separate services: the indexer, a writer that stores trades, a ranking service, and a socket server that talks to browsers. Each one scales on its own, and one failing no longer takes down the rest. Postgres holds the main data, ClickHouse holds trade history, and Redis moves updates between services.

How an event moves through the pipeline

Every swap, token launch and migration on pump.fun and PumpSwap arrives as a raw Solana transaction. The indexer turns each one into a priced, typed event. Click a step, or watch it run.

Helius streams every pump.fun and PumpSwap transaction as raw JSON. Each message goes straight to a pool of parser workers, so parsing never blocks the main process.

Why parsing is the hard part

A transaction is not a trade. One transaction can hold a swap routed through several programs, a token creation, and a migration. The parser has to work out which program handled it, which side is the token and which is the quote asset (SOL, USDC or USDT), and whether the user bought or sold.

Price comes from reserves. For a PumpSwap pool we read the reserves from the token balances in the transaction. For a pump.fun bonding curve we also decode the binary trade event the program writes to its logs, because the curve prices tokens from virtual reserves, not the real ones. Real reserves tell us liquidity and virtual reserves tell us price, and mixing them up gives wrong numbers.

Then the edge cases. A migration can show up as two different events, so we dedupe it by transaction and token. Copycat launchpads reuse PumpSwap, so a new pool only counts if pump.fun was actually called in the same transaction.

Enrichment and trusting the numbers

Prices use decimal arithmetic, not floats. SOL's USD price comes from a live Binance stream, and USDC and USDT count as $1. For each trade we compute two prices: a spot price from the reserves after the trade, and an execution price from what the trader actually paid. If the two differ by more than 1.5x, or the reserves are missing, the trade is flagged and falls back to the execution price with a lower confidence. If no valid price can be computed, the trade is dropped rather than shown wrong.

Every trade gets an ID built from its transaction, index, log index and token, so a replayed or duplicated message always has the same identity.

Token metadata (name, symbol, image) is fetched only when a token is created, which is rare compared to trades. Trades never wait for it. A token with missing metadata is still tracked, and the details are filled in later.

Keep the hot path light

The code that runs for every event has to be fast and bounded. It should not call a database, wait on an external API, or grow memory without a limit. Anything slower runs in the background.

I wrote this down as a rule and applied it in code review. Parsing and enrichment stayed under 7 ms at p95. Two early incidents taught us the rule: data loss when ClickHouse writes failed, and memory growing until the indexer fell over. Both were fixed by putting limits on buffers and caches.

Ranking

Tokens are ranked from real trading activity. Each trade is folded into signals that decay over 10, 30 and 120 seconds, so recent activity counts more and old activity fades on its own. The signals live in bounded memory, so ranking never touches a database.

About every 500 ms the ranking service rebuilds the lists. A token first has to pass quality gates on trades, volume, liquidity and market cap. Then it is scored. Top favours sustained buying volume and activity. Trending favours how fast volume and trades are arriving. Surge looks for sudden spikes. Scoring stayed under 400 ms at p95. It shows what has live momentum. It does not predict price.

A slow page

One important page took about 15 seconds to show useful data on a cold load. The causes were ordinary: missing database indexes, slow queries, and requests that waited on each other. I fixed those, loaded only what was needed first, and added caching. The critical data now arrives in about 200 ms.

Keeping it running

The indexer runs all the time, so we built for failure: duplicate-event protection, heartbeats, structured logs, Slack alerts and phone escalation for serious incidents. Availability settled at about 99%, and most incidents were fixed in 30 to 45 minutes.

When something broke, I looked for the first stage where the data went missing or wrong, instead of patching what the user saw. For anything that depended on real traffic, I replayed real events through the system to check the fix.

Working with AI agents

With two engineers and little time for meetings, agents kept losing context. I wrote the architecture, the hot-path rule, and the debugging steps as skills stored in the repository, so any agent could read them before touching code. They work with Claude Code, Codex and opencode.

That let agents do a large share of the implementation without breaking the rules above. I haven't measured how much time it saved, so I won't put a number on it.