The SCADA export that quietly loses events
A green scheduler icon does not mean every event arrived. Stable keys, checkpoint order, idempotent inserts and the one crash test everyone skips.
A SCADA event export can look completely healthy on a dashboard while silently losing production records. The scheduled job runs every ten seconds, the service status is green, the error log is empty, and nobody notices that 180 events vanished during a brief SQL reboot on Thursday night—until Quality opens the batch review on Monday morning.
When that happens, the instinctive reaction is to shorten the timer or add a restart script. That treats the symptom. A schedule only proves your code woke up; it proves nothing about whether every eligible event actually arrived intact.
Write down what the pipeline guarantees
Before changing a single query, I write down the delivery rule in plain English. For most SCADA-to-MES or SCADA-to-archive pipelines, the realistic target is at-least-once delivery combined with idempotent insertion on the receiving side:
- Every eligible source event is eventually written to the destination with its identity and required payload intact.
- Re-sending the same event never creates a duplicate row or double-counts production.
- Any malformed or rejected record is quarantined with a visible reason rather than dropped silently.
“Exactly once” mostly lives in architecture slide decks, parked right next to flying cars. In real distributed systems, you get its effect by pairing durable unique keys, careful checkpoint ordering and periodic reconciliation.
Start with a stable event key
Never use a timestamp as your primary watermark or uniqueness key. Two alarms can land in the same millisecond, a PLC clock can step backwards after an NTP resync, and an out-of-order late arrival from a buffered gateway can have an older timestamp than the last row you already exported.
Use a monotonically increasing source ID (or a composite key such as SourceSystem + SourceSequenceId) as the immutable identity of every event. Keep both OccurredUtc (when the equipment saw the event) and IngestedUtc (when the export pipeline picked it up). Never overwrite the source timestamp to make a query sort neatly.
Order writes and checkpoints the right way round
Almost every silent data-loss bug comes from advancing the checkpoint before the destination write is durable. If your worker reads rows 1001 to 1050, updates LastExportedId = 1050, and then crashes while writing to the target database, rows 1001 to 1050 are skipped forever on restart.
Flip the order: write to the destination first, wait for durable confirmation, and only then advance the checkpoint. Now look at the opposite crash: the worker writes rows 1001 to 1050, the target commits, and the worker crashes right before saving the checkpoint. On restart, it will re-send 1001 to 1050. Because the receiver enforces a unique constraint on SourceEventKey, the duplicate attempt is absorbed cleanly instead of doubling the batch count.
-- Receiver side: enforce uniqueness in the schema, not just in app code
ALTER TABLE dbo.MesEventInbox
ADD CONSTRAINT UQ_MesEventInbox_SourceKey
UNIQUE (SourceSystem, SourceEventId);
-- Idempotent insert pattern (SQL Server)
INSERT INTO dbo.MesEventInbox
(SourceSystem, SourceEventId, OccurredUtc, EquipmentCode, PayloadJson)
SELECT s.SourceSystem, s.SourceEventId, s.OccurredUtc, s.EquipmentCode, s.PayloadJson
FROM @Batch AS s
WHERE NOT EXISTS (
SELECT 1
FROM dbo.MesEventInbox AS d WITH (UPDLOCK, HOLDLOCK)
WHERE d.SourceSystem = s.SourceSystem
AND d.SourceEventId = s.SourceEventId
);
If source and destination sit inside the same SQL instance, commit the destination insert and the checkpoint update inside one transaction. Across two separate servers, avoid brittle distributed transactions (DTC); rely on the unique constraint at the receiver plus a small checkpoint table on the worker side, and verify with reconciliation.
Watch completeness, not just liveness
A heartbeat tells you the exporter process is alive (I wrote separately about watchdogs and heartbeats). It does not tell you whether the data is complete. Alongside basic liveness, expose four operational numbers that maintenance or support can check in ten seconds:
- Age of the oldest unexported eligible event (much more useful than “last run time”).
- Current backlog count and recent retry volume.
- Count of quarantined (malformed or rejected) events awaiting review.
- A periodic reconciliation diff comparing eligible source IDs against received destination IDs over a rolling window (say, the last 24 hours, so differing retention policies do not trigger false mismatches).
Test the awkward crash on purpose
During commissioning, happy-path testing is too polite. Run a short fault-injection matrix before signing off the interface:
| Fault | Expected behaviour | How to verify |
|---|---|---|
| Kill worker before destination commit | Batch remains pending and exports on restart | Source vs destination ID reconciliation |
| Kill worker after destination commit, before checkpoint | Retry re-sends batch with zero duplicates | Row count equals unique source IDs |
| Destination SQL offline for 15 minutes | Backlog grows visibly; drains cleanly when DB returns | Oldest-pending age alert fires and clears |
| Malformed payload row injected | Row quarantined with reason; healthy rows keep flowing | Quarantine table entry + remaining batch delivered |
If nobody has deliberately killed the process in the awkward half-second between destination write and checkpoint update, recovery is still an assumption. Testing that single moment once in a lab beats doing database archaeology on a Monday morning after a regulated production run.