FDE Times dataFDE jobs open 932▼14 Change in open roles over 7 days, like-for-like: roles from companies we started tracking this week are left outNew in 7 days 62Companies hiring 202Remote-friendly 24%Median US pay $201kTop hirer Databricks 92
中文VI

The newspaper of the Forward Deployed Engineer

Guides

When fraud scoring breaks, clients ask three questions

Lost messages, double scoring and slow models all come down to a few lines of Kafka and Flink configuration that an FDE must be able to explain.

ShareLinkedInFacebookX

In brief

  • A model can be embedded in the Flink job, with no network call, or called on a remote model server, which adds latency in return for centralized model management.
  • Async I/O lets one instance handle many requests at once, while capacity limits the number of pending requests and creates backpressure when it hits the ceiling.
  • Recovery from failure comes from checkpoints, not committed offsets; an exactly-once KafkaSink commits its transactions in step with checkpoints.
Diagram of a fraud-scoring pipeline. The Kafka topic payments.transactions feeds a KafkaSource (watermark 20 seconds, withIdleness 1 minute), then an Async I/O stage (timeout 500ms, capacity 100) highlighted in orange. Async I/O calls a remote model server over non-blocking HTTP, assuming a 50ms response. Results pass through a KafkaSink (EXACTLY_ONCE, unique prefix) and are written to the topic payments.fraud-scores. A dashed orange arrow shows backpressure flowing back to Kafka when 100 requests are pending. Below, a checkpoint bar every 60 seconds spans KafkaSource to KafkaSink. Three cards answer three client questions: no lost messages thanks to checkpointing, no double scoring thanks to EXACTLY_ONCE, and a slow model produces backpressure.
Every question a client asks has a line of config that answers it: checkpointing, EXACTLY_ONCE and the capacity of Async I/O.

Banks and payment gateways are the typical users of Flink with remote models, which they call to analyze transactions in real time, according to Confluent. In that architecture Kafka is the familiar layer that feeds data into Flink, while Flink handles the processing and calls the model.

Take a hypothetical payment gateway. Its data science team hands you a fraud-scoring model that looks good on historical data, and its platform team gives you nothing but a Kafka topic where transactions arrive nonstop. Every transaction needs a risk score before the approval system makes its decision.

For an FDE, the model is rarely the hard part. Harder are the three questions the client will ask as soon as the first incident hits: were any messages lost, was any transaction scored twice, and what happens when the model is slow? The answers to all three sit in a few lines of configuration that you need to understand well.

Should the model live inside the job or outside it?

Kai Waehner, who writes extensively about Kafka, divides deployments into two types. The embedded type puts the model directly inside the stream processing application, so scoring costs no network call. The remote type sends a request to a model server over RPC, an API or HTTP and waits for the response.

Embedded Remote
Where the model runs Inside the Flink job On a separate model server
Network call per event No Yes, over RPC, API or HTTP
Model management Tied to the job: changing the model means redeploying the job Centralized in one place
The price you pay Less flexibility Added latency

The “model management” entry for the embedded column is an inference from the design itself: because the model sits inside the job, replacing a model version means redeploying the whole Flink job.

Confluent describes remote inference as an option for high-throughput systems that accept latency in exchange for flexibility. At a client site, ask this first: who owns the model, and how often do they retrain it?

If the data science team wants to ship new versions without touching the Flink job, choose remote. The rest of this guide takes that path.

A fraud-scoring pipeline, piece by piece

The first piece reads the data and assigns time to it.

env.enableCheckpointing(60_000);

KafkaSource<Txn> source = KafkaSource.<Txn>builder()
    .setBootstrapServers("broker:9092")
    .setTopics("payments.transactions")
    .setGroupId("fraud-scoring")
    .setStartingOffsets(OffsetsInitializer.earliest())
    .setValueOnlyDeserializer(new TxnDeserializer())
    .setProperty("partition.discovery.interval.ms", "300000")
    .build();

WatermarkStrategy<Txn> wm = WatermarkStrategy
    .<Txn>forBoundedOutOfOrderness(Duration.ofSeconds(20))
    .withTimestampAssigner((txn, ts) -> txn.eventTimeMillis())
    .withIdleness(Duration.ofMinutes(1));

DataStream<Txn> txns = env.fromSource(source, wm, "payments");

Flink needs a WatermarkStrategy made of two parts: a TimestampAssigner, which takes the real transaction time from the data, and a WatermarkGenerator, which decides how long to wait for late data. forBoundedOutOfOrderness with 20 seconds says you accept transactions arriving up to that late, a figure to agree with the client rather than guess.

The withIdleness line saves you from a fault that is hard to see. If a Kafka partition has no data, it can hold the whole job’s watermark still, leaving every time-based computation waiting forever. Marking the input idle after one minute solves the problem.

The partition.discovery.interval.ms line also deserves an explanation to the client. KafkaSource discovers new partitions when a topic scales out, with no job restart. By default it checks every 5 minutes, which is the 300000 milliseconds shown above.

The second piece calls the model. How you call it determines how many transactions each instance can score per second.

DataStream<Scored> scored = AsyncDataStream.unorderedWait(
    txns,
    new ScoreWithModelServer(),   // RichAsyncFunction using an asynchronous HTTP client
    500, TimeUnit.MILLISECONDS,   // timeout per request
    100);                         // capacity: maximum number of pending requests

The Async I/O operator sends requests to the model server and receives responses without blocking, so a single parallel instance can handle many requests at once. Say, for the sake of arithmetic, that the model server answers in 50ms.

Called synchronously in a map, each instance can score only 20 transactions per second. With a capacity of 100, the theoretical ceiling rises to 2000, provided the model server can cope. Do not turn that 2000 into a promise to the client: the real ceiling depends on what the model server can bear and how many parallel instances the job runs, so measure before you commit.

Capacity has a second effect. When the number of pending requests hits the ceiling, Async I/O creates backpressure toward Kafka instead of building an unbounded backlog in memory.

The last piece writes the results.

KafkaSink<Scored> sink = KafkaSink.<Scored>builder()
    .setBootstrapServers("broker:9092")
    .setRecordSerializer(new ScoredSerializer("payments.fraud-scores"))
    .setDeliveryGuarantee(DeliveryGuarantee.EXACTLY_ONCE)
    .setTransactionalIdPrefix("fraud-scoring-v1")
    .build();

scored.sinkTo(sink);

In EXACTLY_ONCE mode, KafkaSink writes all messages in a Kafka transaction and commits only when a checkpoint completes. That makes the enableCheckpointing line at the top of the job mandatory. transactionalIdPrefix must be unique, so tie the name to the job and its version.

In what order should you build it at the client site?

Do not assemble all three pieces in one sitting. Start with a job that only reads the topic and prints it, to confirm that the deserializer reads the data correctly and the job has access to the topic before you add anything else. Then add watermarks and check in the Flink UI that they advance on every partition.

Next, replace the real model with a stub that returns a fixed score with simulated latency. Measure throughput at several capacity settings and record the results: they are your evidence when you negotiate with the team running the model server over how many requests per second it must handle. Connect the real model and turn on exactly-once only when the numbers are stable.

Faults that break the pipeline without anyone noticing

The first mistake is to look at the offsets committed in Kafka and believe they are the recovery mechanism. KafkaSource does commit offsets when a checkpoint completes, but the Flink documentation states that it does not rely on them for fault tolerance. Committed offsets serve only to monitor consumer lag; the real state lives in the checkpoint.

The second is to run two jobs, such as the old version and a trial version, with the same transactionalIdPrefix. The uniqueness requirement exists for a reason, so put the environment name and version in the prefix from the start.

The third is to forget withIdleness on a topic whose partitions swing between quiet and busy, as with overnight transactions. The symptom is that time-window features stop producing results even though the job still shows “green.” The fourth is to see backpressure and keep raising capacity, when you should be asking whether the model server is overloaded.

How to put this skill on your CV

Skip “experienced with Kafka and Flink.” Write one line with measurements from your own exercise: moved from synchronous model calls to Async I/O, raised throughput from what to what, achieved exactly-once through checkpoints. Add a small repo whose README answers the client’s three questions: are messages lost, are any scored twice, and what happens when the model is slow?

What clients are buying is a promise that every transaction will be scored exactly once, and in time. If you can point to the line of configuration behind each word of that promise, they will trust you.

Was this article useful?

Use with your AI assistantAsk Claude ↗Ask ChatGPT ↗
5 sources
Read next on the roadmap · Stage 2: Broad engineeringOne unasked data question can cost three weeks of codeWhen an AI application must live inside a client's infrastructure, a one-page design doc written in two days exposes disagreements while they are still cheap to fix.