Scrivio Interview library
DesignIntermediate6 min read · updated 12 November 2026

Data scientists train a fraud model in Python, and the Java payment service must score each payment in under 20 ms. Compare calling a Python model server with running the model in the JVM.

A Python model server keeps the data scientists' stack and lets them deploy on their own, but every payment pays for a network hop and the payment service gains another dependency that can be slow or down. Exporting the model to ONNX and running it inside the JVM removes the hop and makes 20 ms easy to meet, but the model must be exportable and the Java team must own the runtime. Either way, features must be computed identically in training and serving, versions need shadow testing and rollback, and the decision should rest on measured p99 latency, memory and throughput.

Why this question is asked

Fraud scoring is a classic case where machine learning meets strict production limits: the score must arrive before the payment is approved, and a timeout means either blocking a good customer or approving a risky payment. Interviewers ask this design question because there is no universally right answer; they want to see us reason about latency budgets, ownership between teams, failure modes and the subtle bug of training-serving skew. In this answer, we will look at the latency budget, then each option in turn, the feature consistency problem, safe rollout of new model versions, and how to measure the choice.

1A Quick Recap: What the 20 ms Budget Contains

The 20 ms is the time the payment service can spend to get a fraud score, and it is usually measured at a high percentile such as p99, meaning 99 out of 100 requests must finish within it. Inside that budget we must load or compute the features, the input values the model needs, such as the customer's payment count in the last hour, then run the model, then apply the decision rules.

Feature lookups often take the biggest share. If reading the customer's recent activity from a cache takes 5 ms, only 15 ms remain for the model and everything around it. That is why the hop to another service matters so much here.

2Option 1: Calling a Python Model Server

In this design the data scientists deploy the model behind a REST or gRPC service, often with a framework such as FastAPI, or a dedicated server such as NVIDIA Triton. The Java service sends the features and receives the score. The attraction is ownership: the people who train the model also package and release it, in the language and libraries they know, and they can use any Python code they need.

The cost is the network call. On a good internal network a gRPC call might add 1 to 3 ms on average, but the p99 can be much higher because of garbage collection pauses, connection pool waits, retries or a busy node. It is also another service to scale with payment traffic and another way to fail. The payment service needs a timeout, for example 12 ms, and a fallback decision, such as approving low amounts and sending high amounts to review, for when the server is slow or down.

Step throughScoring a payment through a model server
Payment serviceFeature cacheModel server
get features for customer 42
features (about 5 ms)
POST /score with features
run model
score 0.03 (2 ms average, p99 higher)
apply rules, approve

6 messages in order. Play them, or step through them one at a time.

The network hop and the second service add both latency and a failure mode that the in-JVM option avoids.

3Option 2: Running the Model in the JVM With ONNX

Here the data scientists export the trained model to ONNX, and the payment service loads it with ONNX Runtime's Java API at startup. Scoring becomes a method call that typically takes well under a millisecond for a tree model or small network on CPU. There is no hop, no second service to scale with payments, and no remote failure.

But there is a catch. Not every model exports cleanly: custom Python preprocessing or unusual layers may not have ONNX equivalents, so the export must be part of the data scientists' workflow and tested. The Java team now owns the runtime: memory for the model inside the payment service's container, native library upgrades, and the release process for new model files. A good compromise is to ship the model as a versioned artifact that the service loads, so data scientists can release models without a code change in the payment service.

4The Hidden Problem: Training-Serving Skew

Whichever option we choose, the model only works if it receives the same features at serving time that it saw in training. Training-serving skew happens when those differ, for example when the Python training code counts payments in the last 60 minutes including the current one, but the Java code excludes it, or rounds amounts differently. The model then scores real traffic on slightly wrong inputs and quietly gets worse, with no error anywhere.

There are two common protections. A feature store keeps feature definitions in one place and serves the same values to training and serving. Shared feature code, such as one feature library or SQL definitions used by both sides, achieves the same with less infrastructure. We also log the features actually used at serving time and compare their distributions with the training data.

5Versioning, Shadow Deployment and Rollback

A new model version should never go straight to deciding payments. Each model has a version number stored with every score. In shadow deployment, the new version scores live payments in parallel, but only the current version's score is used; we compare the two offline for a week. If the new one is better, we switch with a configuration flag, and rollback is the same flag switched back, which takes seconds.

Step throughReleasing a new fraud model version
  1. Export and test the modelcompare Python and Java scores on a test set
  2. Deploy as shadowscores logged, not used
  3. Better precision and recall, latency within budget?
    YesSwitch traffic with a flagNoKeep the current model and investigate
  4. Watch fraud and decline ratesroll back with the flag if they move

4 steps in order. Play them, or step through them one at a time.

6Measuring Before Deciding

The final choice should come from measurements with real traffic, not from opinion. We replay a day of recorded payments through both options and measure p99 and p999 latency including feature lookup, memory used by the service, and throughput per CPU. A typical result is that the in-JVM model gives p99 well under the budget, while the model server's average is fine but its p99 breaks the budget during traffic spikes. If the model cannot be exported, the measurements tell us how much capacity and tuning the server option needs to be safe.

7Follow-up questions

What fallback should the payment service use when it cannot get a score in time?

A rule-based decision agreed with the risk team, for example approving small amounts from known customers and holding or reviewing the rest. The fallback must be tested and counted, because a silent fallback can hide a broken model server for days.

Can both options be combined?

Yes. A common design runs a fast ONNX model in the JVM for the real-time decision and sends a sample, or the uncertain cases, to a richer Python model asynchronously for later review. The real-time path stays within budget while data scientists keep flexibility.

How do you check that an exported ONNX model matches the Python one?

Score the same few thousand real feature rows in Python and in Java, then compare the outputs. They should match to several decimal places. Any larger difference points to an export problem or different preprocessing.

8Conclusion

In this answer, we compared two ways to score fraud within 20 ms. A Python model server keeps the data scientists' stack but adds a network hop, a service to scale and a failure mode, while ONNX in the JVM gives low latency at the cost of exportability and Java ownership of the runtime. In both cases features must match between training and serving, versions need shadow deployment and rollback, and real-traffic measurements of p99 latency, memory and throughput make the final decision.

What to say in the interviewAbout 80 seconds aloud

Open with

With a Python model server, the data scientists keep their own stack and release models independently, which is a real benefit, but every payment pays for a network hop and the payment service depends on one more service that must scale with payment traffic.

Then, if they want more

The average hop is small, but p99 can blow a 20 ms budget, especially after feature lookups have used part of it, so we would need a tight timeout and a fallback rule.

If the model can be exported to ONNX, running it in the JVM with ONNX Runtime turns scoring into an in-process call of well under a millisecond, with no remote failure.

The cost is that the model must export cleanly and the Java team owns the runtime, memory and upgrades, so I would ship the model as a versioned artifact the service loads. Either way, the biggest risk is training-serving skew, so I would use a feature store or shared feature code and log serving features to compare with training.

New versions run in shadow first, scores carry the model version, and rollback is a flag. And I would decide by replaying real traffic through both options and measuring p99 latency, memory and throughput.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Spring AI reference
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Data scientists train a fraud model in Python, and the Java payment service must score...": https://scrivio-interview-library.netlify.app/spring-boot/llm-fundamentals/bd455fff62/