|
| 1 | +# Fault Tolerance and Data Consistency |
| 2 | + |
| 3 | +DataFlow Operator processes messages with **at-least-once** delivery semantics. When the processor pod crashes or restarts, some messages may be re-read and written again. This document explains the behavior, risks of data desynchronization, and how to configure idempotent sinks to prevent duplicates. |
| 4 | + |
| 5 | +## Delivery Semantics |
| 6 | + |
| 7 | +- **At-least-once**: Each message is delivered at least once. Duplicates are possible on processor restart or crash. |
| 8 | +- **Exactly-once**: Not supported natively. Use idempotent sinks to achieve effectively-once semantics. |
| 9 | + |
| 10 | +## Source Behavior on Restart |
| 11 | + |
| 12 | +| Source | State storage | On restart | |
| 13 | +|--------|---------------|------------| |
| 14 | +| **Kafka** | Consumer group (Kafka) | Resumes from last committed offset. No duplicates if offset was committed after sink write. | |
| 15 | +| **PostgreSQL** | In-memory (lastReadChangeTime) | State lost. Re-reads from beginning. Duplicates or gaps possible. | |
| 16 | +| **ClickHouse** | In-memory (lastReadID, lastReadTime) | State lost. Re-reads from beginning. Duplicates possible. | |
| 17 | +| **Trino** | In-memory (lastReadID) | State lost. Re-reads from beginning. Duplicates possible. | |
| 18 | + |
| 19 | +### Kafka Source |
| 20 | + |
| 21 | +The Kafka consumer commits offset **only after** the message is successfully written to the sink (via `msg.Ack()`). If the processor crashes: |
| 22 | + |
| 23 | +- **Before sink write**: Offset not committed. On restart, message is re-read. No duplicate in sink. |
| 24 | +- **After sink write, before Ack**: Data may be in sink, offset not committed. On restart, re-read → duplicate in sink. |
| 25 | +- **After Ack**: Offset committed. On restart, resume from next message. No duplicate. |
| 26 | + |
| 27 | +### Polling Sources (PostgreSQL, ClickHouse, Trino) |
| 28 | + |
| 29 | +Read position (lastReadID, lastReadChangeTime) is stored **only in memory**. On pod crash: |
| 30 | + |
| 31 | +- State is lost. |
| 32 | +- On restart, the source re-reads from the beginning (or from a wrong position). |
| 33 | +- **Duplicates** or **gaps** are possible depending on when the crash occurred. |
| 34 | + |
| 35 | +!!! warning "Idempotent sink required" |
| 36 | + For polling sources, always configure an **idempotent sink** (UPSERT, ReplacingMergeTree) to handle duplicates safely. |
| 37 | + |
| 38 | +## Batch Sink Behavior |
| 39 | + |
| 40 | +PostgreSQL, ClickHouse, and Trino sinks write in batches. The flow is: |
| 41 | + |
| 42 | +1. Accumulate messages in batch |
| 43 | +2. Execute `Commit` (transaction) |
| 44 | +3. Call `Ack()` for each message (commits Kafka offset, if applicable) |
| 45 | + |
| 46 | +If the processor crashes **between Commit and the last Ack**: |
| 47 | + |
| 48 | +- Data is already in the sink |
| 49 | +- Kafka offset may not be committed |
| 50 | +- On restart: re-read from Kafka → **duplicate writes to sink** |
| 51 | + |
| 52 | +!!! tip "Reduce duplicate window" |
| 53 | + Use a smaller `batchSize` to reduce the number of messages at risk of duplication on crash. |
| 54 | + |
| 55 | +## Idempotent Sink Configuration |
| 56 | + |
| 57 | +### PostgreSQL Sink |
| 58 | + |
| 59 | +Enable UPSERT mode so that duplicate inserts update existing rows instead of failing: |
| 60 | + |
| 61 | +```yaml |
| 62 | +sink: |
| 63 | + type: postgresql |
| 64 | + postgresql: |
| 65 | + connectionString: "postgres://..." |
| 66 | + table: output_table |
| 67 | + upsertMode: true |
| 68 | + conflictKey: ["id"] # Optional; defaults to PRIMARY KEY |
| 69 | +``` |
| 70 | +
|
| 71 | +Requires the table to have a PRIMARY KEY or UNIQUE constraint on the conflict columns. |
| 72 | +
|
| 73 | +### ClickHouse Sink |
| 74 | +
|
| 75 | +Use `ReplacingMergeTree` engine for automatic deduplication by a version column: |
| 76 | + |
| 77 | +```sql |
| 78 | +CREATE TABLE output_table ( |
| 79 | + id UInt64, |
| 80 | + data String, |
| 81 | + created_at DateTime DEFAULT now() |
| 82 | +) ENGINE = ReplacingMergeTree(created_at) |
| 83 | +ORDER BY id; |
| 84 | +``` |
| 85 | + |
| 86 | +Or create the table with `autoCreateTable: true` and `rawMode: false` — the connector infers column types. For deduplication, create the table manually with `ReplacingMergeTree(version_column)` and `ORDER BY` on the deduplication key. |
| 87 | + |
| 88 | +### Kafka Sink |
| 89 | + |
| 90 | +The Kafka producer uses `RequiredAcks = WaitForAll` and `Producer.Idempotent = true` for durability and to prevent duplicate messages on retry. Consumers should still handle potential duplicates (e.g., by idempotent processing or deduplication by key) for end-to-end exactly-once semantics. |
| 91 | + |
| 92 | +## Best Practices |
| 93 | + |
| 94 | +1. **Use idempotent sinks** for PostgreSQL (UPSERT) and ClickHouse (ReplacingMergeTree) when using polling sources or when duplicates are possible. |
| 95 | +2. **Kafka source**: Consumer group stores offset; at-least-once is preserved. Idempotent sink recommended for batch sinks. |
| 96 | +3. **batchSize**: Smaller batches reduce the duplicate window on crash. Balance with throughput. |
| 97 | +4. **batchFlushIntervalSeconds**: Shorter intervals flush more frequently, reducing in-flight data at risk. |
| 98 | +5. **Error sink**: Configure `spec.errors` to capture failed messages for replay or analysis. |
| 99 | + |
| 100 | +## Graceful Shutdown |
| 101 | + |
| 102 | +On SIGTERM (e.g., pod eviction, node drain): |
| 103 | + |
| 104 | +1. The processor receives the signal and cancels the context. |
| 105 | +2. Sinks flush in-flight batches before exiting. |
| 106 | +3. `PreStop: sleep 5` gives time for the load balancer to stop routing traffic. |
| 107 | + |
| 108 | +Ensure `terminationGracePeriodSeconds` is sufficient for large batches to flush (default: 600 seconds). |
| 109 | + |
| 110 | +## Checkpoint Persistence (Future) |
| 111 | + |
| 112 | +Persisting source checkpoint (lastReadID, lastReadChangeTime) to external storage (ConfigMap or sink table) would allow polling sources to resume from the last committed position after a processor restart, reducing duplicates. This is planned for a future release. Until then, use idempotent sinks to handle duplicates safely. |
| 113 | + |
| 114 | +## Summary Checklist |
| 115 | + |
| 116 | +| Scenario | Recommendation | |
| 117 | +|----------|-----------------| |
| 118 | +| PostgreSQL sink | Enable `upsertMode: true` with PRIMARY KEY or `conflictKey` | |
| 119 | +| ClickHouse sink | Use `ReplacingMergeTree` with `ORDER BY` on deduplication key | |
| 120 | +| Kafka source | Consumer group persists offset; idempotent sink recommended | |
| 121 | +| Polling sources | **Always** use idempotent sink; state is lost on crash | |
| 122 | +| batchSize | Consider smaller values to reduce duplicate window | |
0 commit comments