The warehouse's daily event count runs 2–3% above the source after every deploy — find and fix the cause
A Kafka consumer group (six members) loads the events topic into dwh.events. For every batch it runs the SQL below and then commits offsets. On days with a deploy (a rolling restart of the six pods) the daily count in dwh.events is 2–3% above the source system; on quiet days it matches.
Constraints: prove the cause with a query against dwh.events before changing anything; the fix must survive any future restart, not just deploys.
-- runs for every consumed batch; the consumer commits offsets afterwards
INSERT INTO dwh.events
(event_id, user_id, event_type, event_ts,
kafka_partition, kafka_offset, loaded_at)
SELECT event_id, user_id, event_type, event_ts,
kafka_partition, kafka_offset, now()
FROM staging.events_batch;Diagnose the cause, write the query that proves it, and fix the load.
A deploy restarts consumers and forces rebalances; batches inserted but not yet committed are re-read by the partition's new owner and inserted again. Copies per (partition, offset) cluster at deploy times. Fix it at the sink — a unique key plus upsert or MERGE.
- Expecting a commit in the partition-revoke callback to close the window, when a crash or a killed pod still skips it
- Blaming producer retries, when a retried send lands under a new offset and these copies share one (partition, offset)
- Deduplicating in a dashboard filter instead of making the load itself reject or overwrite repeats
- Why does committing in the partition-revoke callback narrow the duplicate window but not close it?
- How does Kafka's static group membership (
group.instance.id) change the number of rebalances per deploy?
The cause is rebalancing during the rolling restart. The consumer inserts a batch and only then commits the offset. When a pod stops (or is killed) between the insert and the commit, its partitions move to another member, which resumes from the last committed offset — so it re-reads and re-inserts a batch that is already loaded. A deploy of six pods rebalances the group several times, hence a steady 2–3%.
Proof: a duplicate carries the same (kafka_partition, kafka_offset) as the original, with a load time around the deploy.
-- 1. copies of one Kafka position, by load hour
SELECT date_trunc('hour', loaded_at) AS load_hour,
count(*) AS extra_rows
FROM (
SELECT loaded_at,
row_number() OVER (PARTITION BY kafka_partition, kafka_offset
ORDER BY loaded_at) AS copy_no
FROM dwh.events
WHERE event_ts >= now() - interval '14 days'
) t
WHERE copy_no > 1
GROUP BY 1
ORDER BY 1;
-- the extra rows cluster in the deploy hours
-- 2. remove the accumulated copies, keeping one per position (PostgreSQL: the lowest ctid)
DELETE FROM dwh.events e
USING dwh.events d
WHERE e.kafka_partition = d.kafka_partition
AND e.kafka_offset = d.kafka_offset
AND e.ctid > d.ctid;
-- 3. a load that is safe to repeat
ALTER TABLE dwh.events
ADD CONSTRAINT events_kafka_pos_uq UNIQUE (kafka_partition, kafka_offset);
INSERT INTO dwh.events
(event_id, user_id, event_type, event_ts,
kafka_partition, kafka_offset, loaded_at)
SELECT event_id, user_id, event_type, event_ts,
kafka_partition, kafka_offset, now()
FROM staging.events_batch
ON CONFLICT (kafka_partition, kafka_offset) DO NOTHING;The (kafka_partition, kafka_offset) key catches a re-read of the same message; if the producer itself can send an event twice under different offsets, key on event_id instead (with MERGE / ON CONFLICT DO UPDATE). Committing in the partition-revoke callback cuts the repeats on a clean shutdown but does nothing for a crash or a SIGKILL, so the fix stays at the sink.