Skip to content

Pricefeed: one market data service for many applications

A Kotlin market data service gave partners, dashboards and websites a shared view of prices over WebSocket and REST.

Pricefeed: one market data service for many applications

The client's partners needed the same view of market prices as its dashboards and websites. Reading the upstream feeds directly meant every application had to translate the feed formats and decide when to switch sources. A live connection didn't tell them when to make that switch. A feed could stay connected long after it stopped sending useful prices.

We built pricefeed, a Kotlin service on the JVM that made those decisions in one place. It read several upstream feeds, put their updates into a common format, and applied a policy for each instrument. Applications subscribed over WebSocket or requested a snapshot over REST. Both worked for consumers written in different languages. Java and Kotlin applications could use the JVM client library, and two service instances gave consumers a second endpoint if one went down.

What the feed said, and what we knew

Each upstream feed had its own adapter. The normalisation layer mapped source symbols to shared instrument IDs, validated updates and represented prices as exact decimals. The resulting update still identified its source. If a feed sent a bid but no ask, the normalised update contained a bid and no ask.

We recorded when our service received an update and kept that timestamp separate from any timestamp supplied by the feed. A missing source timestamp stayed missing; the service's clock couldn't tell us when the venue published the price. The same rule applied to sequence numbers and identifiers. A sequence gap was meaningful within the feed that issued the numbers. There was no global sequence across feeds.

Which price went out?

The partners, dashboards and websites all wanted the same merged view, so we computed it in the service. What went into that view depended on the instrument. We kept the choice in policy data rather than burying it in an adapter:

  • When two sources were substitutes, the priority policy selected the first healthy source on the list.
  • When sources offered complementary quotes, the service computed a shared view across eligible sources. The best bid and offer, for example, can come from different venues. A computed view carried the policy version so consumers could distinguish it from a quote received as-is.
  • When a consumer needed to work with individual feeds, pass-through kept updates separate and carried their source details. That left specialised calculations with the applications that needed them.

An open connection wasn't enough to qualify a source as healthy. The service considered how recently data had arrived, the rate of incoming updates and sequence gaps where the feed supplied sequence numbers. The switching rules kept a briefly recovering source from repeatedly taking over and dropping out again.

Tailor your workshop with CypherX

How applications got their prices

A WebSocket consumer subscribed to the instruments and view it needed. An application that only needed current prices could request a REST snapshot from memory instead of keeping a stream open. Both delivered the same normalised representation. The network API was the common interface for applications written in different languages. The URL carried the major API version (/v1), which left room for a breaking change on a new path without replacing the existing one. Distribution checked each consumer's access to the instruments it requested.

The JVM client library provided typed access and handled subscription setup and reconnects. Applications outside the JVM could call the network API directly. In either case, the merge policy stayed on the server. A change to which source counted as healthy didn't require a new library version.

One service, two kinds of failover

Feed handling, normalisation, merge and distribution were separate modules in one deployable. Each adapter had its own Gradle submodule. Calls crossed module boundaries through public interfaces, and Kotlin's internal visibility kept implementation classes out of the other modules' Kotlin code. That mattered: adding a consumer puts pressure on distribution, whereas a busier market puts pressure on feed handling. We kept that boundary without putting a network hop in it.

The two service instances addressed a different failure from the one inside the merge policy. When an upstream feed went stale, the service could switch sources for that instrument. When a service instance went down, applications could switch instances. The service was built to distribute prices to applications, not to sit on the latency-critical path between receiving a tick and placing an order.

Checking feeds before and after deployment

We checked symbol maps outside production. A quote can have the right format and still be assigned to the wrong instrument. We replayed recorded feed messages to check how adapters handled malformed, duplicate and out-of-order updates, and that missing source fields stayed missing. The policy tests covered stale feeds, feeds that kept dropping in and out, and what happened when no source qualified.

In operation, we tracked freshness and sequence gaps per source. We measured latency separately at each processing stage and watched consumer queue depth. A growing queue could show that a consumer was falling behind even when the upstream feeds were healthy.