Real-time inventory availability at scale
Centralized availability service for an omnichannel platform; Kafka processing high-volume transactions for downstream systems.
Java · Spring Boot · Kafka · Project Reactor · MongoDB · Snowflake · Azure · Helm
Problem
The business was moving into omnichannel sales, with the transport management system (TMS) now part of the picture. Warehouse users needed one complete view of availability: which products are in stock, at which locations, across the warehouse.
There was no central availability store. The legacy warehouse management system (WMS) kept its history in Snowflake, and reading Snowflake directly for every availability question was too expensive. A downstream slotting service needed the same data too.
Constraints
- The source of truth was a legacy WMS on SQL Server. We had to capture its changes without putting heavy load on it.
- Historical data for initial loads lived in Snowflake, and querying it naively was slow and expensive.
- Initial loads arrive as huge bursts of messages, while the live change stream keeps flowing at the same time. The two compete for the same consumers and the same database.
- No record may be lost or skipped, even if a load or a file transfer fails halfway.
- Changes had to show up in availability within seconds.
Architecture
Two paths lead into one central availability service, built with Spring Boot and Project Reactor and backed by MongoDB.
Ongoing changes. A change data capture (CDC) pipeline reads changes from the WMS database and publishes them to a Kafka topic. Our consumers pick them up, process them and update MongoDB within seconds.
Initial and periodic full loads. Optimised queries pull the history out of Snowflake into Azure Data Lake. A reactive Kafka pipeline, built by another team, streams those files into Kafka. Our consumers process them in bulk into the same MongoDB store.
The file streaming is checkpointed: as a file is read, a token marks how far we’ve got. If a transfer fails, it resumes from the last checkpoint, so nothing is skipped and nothing is processed twice.
Every topic is read through the same abstract Kafka consumers, which have rate limiting built in. On top of that, dynamic throttling slows the CDC consumers down while an initial load is running, then speeds them back up so they catch up.
Calls to downstream systems are wrapped in retries, fallbacks and a circuit breaker, so one slow dependency can’t take the service down with it.
On the read side, REST APIs return availability as JSON, per product and per location. Warehouse users’ tools and the slotting service both read from them. Everything ships with one-click Helm deployments on Azure.
My part: all the REST APIs, the abstract Kafka consumers, the rate limiters, the dynamic throttling, the retry, fallback and circuit-breaker layer, and the optimised Snowflake queries.
Key decisions and why
Capture changes instead of querying Snowflake on every read. Querying Snowflake directly for availability was too costly. CDC gives us changes in near real time, within seconds, for a fraction of the cost.
MongoDB for the availability store. One flexible document per product, fast reads by key, and a database the team already knew well.
One abstract consumer, with rate limits built in. Every topic gets the same tested consumer base, so a burst of messages can’t overwhelm the service or the database, and nobody writes a consumer from scratch.
Throttle the live stream during a full load. Instead of letting the two fight over resources, the CDC consumers back off while an initial load runs. The load finishes faster, and the CDC stream catches up afterwards.
Checkpoint the file streaming. A failed transfer resumes where it stopped instead of restarting from zero or quietly skipping records.
Do the heavy lifting inside Snowflake. The extract queries build temporary tables inside Snowflake, so the slow part runs where the data already is.
What went wrong
The first initial load flooded the system. It produced far more messages than the consumers could safely handle, and it collided with the live CDC stream.
We fixed it in two steps. First, rate limiters on the consumers, so a burst could no longer overwhelm the service. Then dynamic throttling, so the CDC stream steps aside while a full load runs, instead of the two fighting each other.
The first versions of the Snowflake queries were also too slow. We redesigned them around temporary tables, which made the extract much faster.
Results
- Changes in the WMS show up in the availability service within seconds.
- Initial loads run at high throughput without losing data, even when a file transfer fails.
- The redesigned Snowflake extract queries are much faster than the first versions.
- Test coverage on this product went from 50% to 100%.
- Deployments are one click, with Helm.