Scrivio Interview library
ScenarioAdvanced6 min read · updated 13 November 2026

An analytics engineering team runs 40 persistent queries on a self-managed ksqlDB cluster. Upgrades have been painful, some queries cannot be changed without rebuilding state, and the product's future is uncertain.

Leadership approves a move to Flink SQL. Plan the migration so that consumers of the 40 output topics see no gaps and no flood of duplicates.

Inventory every query first, then translate semantics rather than syntax: windows, late data, null and tombstone handling, and key bytes and partitioning must match. Run each Flink job in parallel writing a new topic back-filled from retained history, compare old and new outputs automatically until they agree, and cut consumers over one by one at known positions with a rollback path. Build the Flink operational base before the first migration.

Why this question is asked

Replacing a stream processing engine is harder than replacing a batch tool, because the old system never stops: its 40 queries emit results continuously, hold state, and have consumers that notice every gap or duplicate. Interviewers ask this scenario to see whether we plan for semantic differences and stateful cutover, not just rewrite SQL. In this answer, we will build an inventory, translate a query and the semantics around it, run old and new in parallel with automated comparison, cut over safely, and prepare the operations Flink needs.

1Inventory Each Query

For each of the 40 persistent queries we record its inputs, its output topic, the key and value formats, any windows, the size of its state, who consumes the output, and whether the output is an append stream or an upsert table. That last point decides the Flink connector: an append-only stream maps to Flink's kafka connector, while a table whose records update earlier ones by key maps to the upsert-kafka connector, which writes updates as upserts and deletes as tombstones.

The inventory also gives the migration order. We start with simple, stateless or small-state queries with few consumers, to learn, and leave large windowed aggregations with many consumers for later.

2Translating Semantics, Not Just Syntax

The SQL usually translates easily. Here is a ksqlDB aggregation and its Flink SQL counterpart.

SQL
-- ksqlDB
CREATE TABLE orders_per_customer AS
SELECT customer_id, COUNT(*) AS order_count FROM orders GROUP BY customer_id EMIT CHANGES;
-- Flink SQL
CREATE TABLE orders (
order_id STRING, customer_id STRING, ts TIMESTAMP(3),
WATERMARK FOR ts AS ts - INTERVAL '30' SECOND
) WITH ('connector' = 'kafka', 'topic' = 'orders', 'scan.startup.mode' = 'earliest-offset',
'properties.bootstrap.servers' = '...', 'format' = 'avro-confluent', 'avro-confluent.url' = '...');
CREATE TABLE orders_per_customer_v2 (
customer_id STRING, order_count BIGINT, PRIMARY KEY (customer_id) NOT ENFORCED
) WITH ('connector' = 'upsert-kafka', 'topic' = 'orders-per-customer-v2',
'properties.bootstrap.servers' = '...', 'key.format' = 'raw', 'value.format' = 'avro-confluent',
'value.avro-confluent.url' = '...');
INSERT INTO orders_per_customer_v2 SELECT customer_id, COUNT(*) FROM orders GROUP BY customer_id;
Semantic differences to check per query
AspectksqlDBFlink SQL
Late datagrace period per windowwatermark strategy
Upsert outputTABLE backing topicupsert-kafka connector
Key byteskey format, e.g. KAFKAkey.format, must match bytes
Deletestombstones from tablestombstones from upsert-kafka
State on changeoften rebuild the querysavepoints, but state may not map

The hard part is everything the SQL does not say. Window boundaries and how they are aligned must match. Late data is handled differently: ksqlDB uses a grace period per window, Flink uses watermarks, and the same records can land in different windows if these are not translated carefully. Null handling and tombstones must behave the same, or compacted output topics will keep or drop keys differently. And key serialization must produce the same bytes: ksqlDB's KAFKA key format for a string is the raw UTF-8 bytes, so Flink's key.format must be raw, not Avro, or the same customer will be a different key, land on a different partition and break compaction for consumers.

3Running Old and New in Parallel

Each Flink job reads the same input topics and writes a new topic, such as orders-per-customer-v2, while the ksqlDB query keeps running. For stateful queries the Flink job must start from the beginning of retained history to build the same state, so we check that input retention covers what the query needs; if it does not, the results can only converge for keys seen since retention began, and we agree with consumers whether that is acceptable.

An automated diff job reads both outputs and compares them: for upsert tables, the latest value per key; for append streams, counts and checksums per window. We define the tolerance in advance, for example identical values for all keys after both have processed the same input offset, and the job runs until it holds for several days, including at least one peak period.

Step throughMigrating one query
  1. Inventory and translateformats, windows, keys, upsert or append
  2. Flink job writes v2 topicback-filled from retained history
  3. Diff old and new outputsper key or per window
  4. Within tolerance for several days?
    Yescut over consumersNofix the translation
  5. Consumers move one by oneeach with a rollback path
  6. Stop ksqlDB queryafter the last consumer moves

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

4Cutting Over Without Gaps or Floods

There are two ways to cut over. The first moves consumers one at a time from the old topic to the new one. Each consumer must start the new topic at a position equivalent to where it stopped on the old one; for upsert tables that is easy, because reading the new topic from the start just re-applies current state, which an idempotent consumer handles. For append streams we map the position by time or window, and the consumer must tolerate a small overlap.

The second keeps the old topic name: we stop the ksqlDB query and point the Flink job's output at the original topic. Consumers change nothing, but for upsert tables the Flink job will re-emit current state for every key, a flood of updates with unchanged values. That is acceptable only if consumers are idempotent and sized for it. Either way, every step has a rollback path, such as moving a consumer back to the old topic, and the ksqlDB query stays available until the last consumer has moved.

5Building the Operational Base First

Before the first migration, Flink must be operable: a deployment model (a Kubernetes operator or a managed service), durable checkpoint and savepoint storage such as S3, CI-driven job deployment so SQL changes are reviewed and released like code, and monitoring of checkpoint duration, backpressure, consumer lag and restarts. Each migrated job gets a named owner. Without this base, the team trades painful ksqlDB upgrades for an unfamiliar system with the same problems.

6Follow-up questions

How do you change a migrated Flink SQL query later without rebuilding state?

Flink can restore from a savepoint when the plan's stateful operators are compatible, but many SQL changes alter the plan and make state incompatible. For those, we use the same parallel-run approach: a new version of the job writes a new topic, back-filled, and consumers move after a diff. Planning for that from the start avoids surprises.

What if input retention is shorter than the query's history?

Then a new job cannot rebuild exactly the same state from Kafka. We can seed it from the old output topic, accept convergence only for recent keys, or temporarily extend retention before starting the migration.

Who should own the migrated jobs?

The analytics engineering team that owns the logic, with the platform team owning Flink itself. A named owner per job keeps decisions about changes and alerts with the people who understand the query.

7Conclusion

In this answer, we planned the move of 40 ksqlDB queries to Flink SQL: inventory each query's inputs, outputs, formats, windows, state and consumers, choosing the kafka or upsert-kafka connector; translate semantics such as windows, late data, tombstones and key bytes; run Flink jobs in parallel on new back-filled topics with automated diffs; cut consumers over one by one or take over the old topic names accepting re-emitted state, always with rollback; and build Flink's deployment, checkpoint storage, CI and monitoring before starting.

What to say in the interviewAbout 80 seconds aloud

Open with

I would start with an inventory of all 40 queries: inputs, output topic, key and value formats, windows, state size, consumers, and whether each output is an append stream or an upsert table, which decides between Flink's kafka and upsert-kafka connectors.

Then, if they want more

Then I would translate semantics, not just syntax. Window boundaries, late data, where ksqlDB uses grace periods and Flink uses watermarks, null and tombstone handling, and key serialization and partitioning all have to match, otherwise the same customer becomes a different key and compaction and consumers break.

Next, run both in parallel: each Flink job reads the same inputs and writes a new topic, back-filled from retained history, and automated diff jobs compare old and new outputs until they agree within a tolerance we set in advance.

For the cutover, I would move consumers one by one at known positions, or switch the Flink job to the old topic names once ksqlDB stops, accepting that upsert tables re-emit current state, which means consumers must be idempotent, and every step needs a rollback path.

And before any of this, I would build the operational base: a Flink deployment or managed service, checkpoint storage, CI-driven job deployment, monitoring of checkpoint duration and backpressure, and a named owner for each migrated job.

About this answer

Last updated
13 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
Apache Kafka documentation
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 "Leadership approves a move to Flink SQL. Plan the migration so that consumers of the 40...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-streams-connect/42f7f8f87a/