Internet of Things (IoT) and AI

Chapter 5
IoT Data Engineering and Stream Processing

Bachelors Program in AI & ML
Internet of Things (IoT) and AI   •   Bachelor

IoT Data Engineering and Stream Processing

Introduction and Context

Internet of Things (IoT) systems transform physical activity into digital data. Sensors measure temperature, vibration, position, pressure, energy consumption, biological signals, and many other variables. Actuators may then respond to those measurements. However, sensing alone does not create useful intelligence. IoT data must be acquired reliably, transported efficiently, synchronized in time, stored appropriately, cleaned, enriched with context, and processed at a speed suitable for the application.

IoT data engineering is the discipline concerned with designing this complete data lifecycle. It connects embedded systems, networks, cloud platforms, databases, distributed processing, and artificial intelligence. A predictive-maintenance model, for example, is only as dependable as the timestamps, calibration records, machine identities, and historical observations supplied to it.

IoT data differs from conventional business data in several ways. It is often high-volume, continuous, time-indexed, geographically distributed, incomplete, noisy, and generated by devices with limited memory and computing power. The system must therefore address both data correctness and operational constraints such as bandwidth, latency, battery life, fault tolerance, and storage cost.

Learning Objectives

By the end of this chapter, students should be able to:

1. Describe an end-to-end IoT data pipeline.

2. Distinguish event time, processing time, and ingestion time.

3. Select suitable ingestion, storage, and stream-processing technologies.

4. Explain synchronization, windowing, watermarks, and late-arriving data.

5. Design procedures for cleaning, contextualization, provenance, and retention.

6. Calculate basic throughput, storage, and latency requirements.

7. Evaluate data quality and reliability in an IoT application.

8. Identify governance, security, privacy, and ethical concerns.

1. The IoT Data Lifecycle

A practical IoT data architecture usually contains the following stages:

Acquisition: Sensors sample physical phenomena and produce measurements. Acquisition includes calibration, local filtering, device identification, and conversion from electrical signals to engineering units.

Ingestion: Data is transferred from devices through gateways, brokers, APIs, or message queues. Ingestion should support authentication, buffering, acknowledgement, and controlled retry behaviour.

Synchronization: Measurements from different devices are aligned using clocks, timestamps, sequence numbers, or correlation identifiers.

Storage: Raw and processed data is retained in stores chosen for access patterns, durability, query performance, and cost.

Cleaning and validation: Invalid values, duplicates, gaps, outliers, unit inconsistencies, and sensor faults are detected and treated.

Contextualization: Measurements are linked to assets, locations, operating states, units, maintenance events, and business processes.

Processing and analytics: Stream processors calculate aggregates, detect events, trigger alerts, and prepare data for machine-learning models. Batch processing supports historical analysis and model training.

Serving and action: Results are delivered to dashboards, applications, control systems, operators, or actuators.

A robust architecture commonly separates a raw layer from a curated layer. The raw layer preserves the original observation and metadata for auditability and reprocessing. The curated layer contains validated, standardized, and context-rich records intended for analytics.

2. Data Acquisition and Ingestion

2.1 Sampling and measurement

A sensor does not observe a continuous physical reality perfectly. It samples at a particular frequency, has finite resolution, introduces noise, and may drift over time. The sampling frequency should be selected according to the dynamics of the phenomenon and the purpose of analysis. A slowly changing room temperature may require one sample every minute, whereas vibration monitoring may require thousands of samples per second.

Sampling too slowly can hide rapid changes or produce aliasing. Sampling too rapidly increases storage, energy, and communication requirements. Edge aggregation, such as sending a mean, maximum, minimum, and standard deviation over a short interval, can reduce bandwidth while retaining useful information. The choice should be validated against the application; aggregation can destroy information required for anomaly detection.

2.2 Device and gateway responsibilities

Devices commonly perform measurement, basic validation, timestamping, local buffering, and secure communication. Gateways may translate protocols, aggregate records, perform edge analytics, and continue operating during temporary cloud disconnection. A gateway is especially valuable when devices use constrained protocols or when raw data should not leave a site.

A message should normally include a device or asset identifier, measurement name, value, unit, timestamp, quality indicator, sequence number, and schema version. A conceptual record is:

`{device_id, asset_id, metric, value, unit, event_time, sequence_no, quality, schema_version}`

The distinction between a device and an asset is important. A device is the sensing or communication component; an asset is the physical or organizational object being monitored. Devices may be replaced while the asset remains the same.

2.3 Messaging patterns

Publish-subscribe systems use topics or streams to decouple producers from consumers. A producer publishes a message, while one or more consumers subscribe to it. This supports independent applications such as monitoring, storage, alerting, and model inference.

Common delivery semantics are:

  • At-most-once: a message may be lost, but it is not redelivered.
  • At-least-once: a message is retried until acknowledgement, so duplicates are possible.
  • Exactly-once effect: the final business operation behaves as though each message were processed once. This normally requires idempotent operations, transaction support, or deduplication; it is not guaranteed merely by claiming “exactly once” at the transport layer.

For many IoT systems, at-least-once delivery with idempotent consumers is a practical design. A consumer can use a unique event identifier or device sequence number to recognize duplicates.

Back-pressure occurs when producers generate data faster than a consumer can process it. Solutions include bounded queues, rate limiting, autoscaling, sampling, prioritization, and edge aggregation. Unbounded buffering can cause memory exhaustion and severe recovery delays.

3. Time, Synchronization, and Event Ordering

Time is a central engineering problem in IoT. Three timestamps should be distinguished:

  • Event time: when the physical measurement occurred.
  • Ingestion time: when a platform received the record.
  • Processing time: when an application processed the record.

Event-time analysis is usually the most meaningful for physical processes, but it is difficult because networks delay messages and devices may have inaccurate clocks. Processing-time windows are easier to implement but can produce incorrect results when data is delayed.

Devices can synchronize clocks using protocols such as Network Time Protocol. High-precision industrial systems may use more precise clock-distribution methods. Nevertheless, synchronization is never perfect. A record should therefore include clock quality or estimated uncertainty where timing is important.

Sequence numbers help identify missing or duplicated observations. If a device reports sequence numbers 100, 101, 103, the system can flag a possible missing record. Sequence gaps do not prove data loss if the device deliberately suppresses messages, so interpretation requires device knowledge.

Watermarks and late data

A stream processor cannot wait forever for an earlier event. A watermark is an operational assertion that events earlier than a specified event-time boundary are unlikely to arrive. For example, a five-minute watermark delay means the system waits for a period before finalizing an event-time window.

A late event is an event that arrives after the processor has advanced beyond its event-time position. Systems may drop it, update a previously emitted result, route it to a correction stream, or store it for offline reconciliation. The correct choice depends on whether the application values immediate results or historical completeness.

4. Stream-Processing Concepts

A stream is an unbounded sequence of records. Stream processing operates incrementally rather than waiting for a complete dataset. Typical operations include filtering, mapping, joining, aggregation, enrichment, pattern detection, and alert generation.

Windows

Because an unbounded stream has no natural end, aggregation requires windows:

  • Tumbling window: fixed, adjacent, non-overlapping intervals.
  • Hopping window: fixed intervals that overlap according to a specified advance.
  • Sliding window: records are retained according to a continuously moving time or count boundary.
  • Session window: a group of events separated by gaps longer than a defined inactivity period.

For a tumbling window containing values x1 through xn, the arithmetic mean is:

`mean = (x1 + x2 + ... + xn) / n`

where n is the number of valid observations in the window. This calculation assumes that all included observations represent the intended population and that missing or invalid values have been handled explicitly.

A count-based window may be appropriate for fixed event quantities, whereas an event-time window is generally better when physical intervals matter. For example, “average temperature during each minute” should usually be based on event timestamps rather than arrival order.

Stateful processing

Many operations require state: a running count, the last known location, a device’s recent values, or an open machine-maintenance session. State must be checkpointed or reconstructed so that processing can recover after failure. State growth must be bounded through expiration, compaction, or retention policies.

Stream joins

A stream may be joined with another stream, such as joining sensor readings with maintenance events. Since both streams are continuous, the join normally uses a key and a time constraint. An unconstrained join can require unlimited state and is therefore unsafe in production.

5. Storage Architecture and Time-Series Databases

No single store is optimal for all IoT workloads. A layered architecture may include:

  • Object storage or data lake: inexpensive, durable storage for raw and historical data.
  • Time-series database: efficient storage and queries for timestamped measurements.
  • Relational database: transactional metadata, asset hierarchies, users, and maintenance records.
  • Search or log store: operational diagnosis and text-oriented investigation.
  • Stream state store or cache: low-latency current values and intermediate results.

A time-series database generally organizes data around timestamps, measurements, tags, and fields. Tags such as site or device type are useful for filtering, but excessive high-cardinality tags can harm indexing and storage efficiency. A design should distinguish stable dimensions from rapidly changing values.

Important time-series features include compression, time-based partitioning, retention policies, downsampling, interpolation, and efficient range queries. Downsampling replaces high-resolution history with summaries such as hourly averages or maxima. It reduces cost but permanently loses fine-grained information unless the original data is retained elsewhere.

Data modeling should reflect query patterns. If common queries ask for all temperature readings for one machine during a day, the schema should support device-and-time filtering efficiently. If queries compare all machines at one site, site-oriented access should also be considered.

Retention and tiering

Retention is the period for which data remains available in a particular form. Hot data supports rapid operational queries; warm data may be cheaper and slower; cold data is retained mainly for compliance, research, or incident investigation. Retention should be based on operational value, legal requirements, privacy, model-training needs, and cost—not merely on storage capacity.

6. Data Cleaning, Quality, and Contextualization

IoT data quality is multidimensional. Relevant dimensions include:

  • Completeness: required observations are present.
  • Validity: values conform to type, range, unit, and schema rules.
  • Accuracy: values represent the physical quantity adequately.
  • Timeliness: data arrives within the required time.
  • Consistency: related systems do not contradict one another.
  • Uniqueness: duplicate events are controlled.
  • Traceability: the origin and transformations are known.

Quality rules should be explicit. A temperature reading of 25 °C may be valid in one environment but implausible for a freezer. A sudden change may indicate a real event, a sensor fault, or a unit conversion error. Automated rules should flag questionable records rather than silently deleting them.

Common treatments include:

  • range validation;
  • unit normalization;
  • duplicate detection;
  • missing-value flags;
  • interpolation for suitable, slowly varying signals;
  • robust outlier detection;
  • sensor calibration and drift correction;
  • quarantine of records that require investigation.

Interpolation must not be used automatically. It may be reasonable for a short gap in a slowly changing process, but it can be misleading for discrete events, alarms, or rapidly changing signals. Every imputed value should be marked as imputed.

Contextualization gives meaning to an observation. A value becomes more useful when linked to an asset hierarchy, location, operating mode, product batch, environmental conditions, maintenance history, and calibration state. Context can be added through reference-data joins, digital-twin models, metadata services, or event correlation.

Provenance

Data provenance records where data came from and how it changed. It may include the originating device, firmware version, gateway, ingestion time, validation rules, transformations, model version, and responsible operator. Provenance supports debugging, regulatory audits, scientific reproducibility, and trustworthy AI. It also enables lineage queries such as: “Which raw observations contributed to this alert?”

7. Throughput, Storage, and Latency Planning

Capacity planning begins with rates and record sizes. If N devices each produce r records per second, the nominal event rate is:

`R = N × r`

where R is measured in records per second. If the average encoded record size is s bytes, the uncompressed data rate is:

`B = R × s`

where B is bytes per second. Actual infrastructure capacity must additionally account for protocol overhead, replication, indexes, metadata, retries, and peak load.

For a constant rate over T seconds, raw storage is approximately:

`S = B × T`

This estimate assumes a constant rate and excludes compression and system overhead. For example, 1,000 devices producing 1 record per second, with an average payload of 200 bytes, generate 200,000 bytes per second. Over one day, the payload volume is 17,280,000,000 bytes, approximately 17.28 GB using decimal units, before overhead and replication.

Latency is not one number. Engineers should distinguish sensor-to-gateway latency, network latency, broker latency, processing latency, storage latency, and end-to-end action latency. A safety application may require a strict upper bound, while a reporting dashboard may tolerate seconds or minutes.

Capacity should be tested under bursts, network recovery, duplicated messages, delayed events, and consumer failure. Average throughput alone is inadequate.

8. AI and Analytics in the IoT Pipeline

AI systems depend on carefully engineered features. A stream processor may compute rolling averages, rates of change, vibration bands, counts of alarms, or deviations from a baseline. Feature definitions must specify time basis, units, missing-value treatment, and leakage controls.

Training data should be created using event time and realistic information availability. A model must not use a value that would not have been available when the prediction was made. This is a form of temporal leakage. For example, using a later maintenance outcome as an input to an earlier prediction produces deceptively strong but invalid performance.

Online inference introduces additional requirements: model versioning, feature consistency between training and serving, monitoring of prediction latency, drift detection, and fallback behaviour. When input quality is poor or the model is unavailable, the system should degrade safely rather than silently produce confident decisions.

9. Illustrative Case: Predictive Maintenance in a Bottling Plant

Background

This illustrative case concerns a bottling plant with 120 motors. Each motor has vibration, temperature, current, and rotational-speed sensors. The objective is to identify bearing degradation before a production stoppage.

Problem

The original system uploaded daily files. Operators received delayed reports, timestamps from different devices were inconsistent, and replacement sensors occasionally reused device identifiers. A machine-learning model produced false alarms because missing readings and maintenance periods were not represented correctly.

Analysis

The engineering team identified four principal causes: unreliable clock synchronization, ambiguous identity management, insufficient event context, and a lack of data-quality states. The plant also generated bursts of vibration data during high-speed operation, creating uneven network load.

Approach and solution

Sensors were assigned immutable device identities, while asset identities were maintained separately. Gateways added event-time and ingestion-time fields and buffered data during network outages. A publish-subscribe broker accepted at-least-once delivery. Consumers used event identifiers and sequence numbers for deduplication.

A stream processor calculated five-minute event-time features, including mean temperature, maximum vibration amplitude, and the proportion of missing samples. A watermark allowed two minutes for delayed events. Corrections arriving later were sent to a reconciliation process rather than silently discarded.

Raw messages were retained in object storage. Curated features were stored in a time-series database, while asset, maintenance, and production metadata were stored relationally. Maintenance events were joined with sensor features using asset identity and time intervals. Data-quality flags distinguished measured, interpolated, unavailable, and quarantined values.

The model-serving process recorded model version, feature timestamps, and prediction provenance. Alerts were ranked by confidence and suppressed during planned maintenance windows.

Outcome

The redesigned pipeline reduced duplicate records, made delayed observations visible, and allowed engineers to reproduce alerts from source data. The case does not claim a universal numerical improvement; its main outcome is a more auditable and operationally reliable basis for predictive maintenance.

Learning points

1. Correct asset identity is as important as correct measurement values.

2. Event-time processing is necessary when network delays are significant.

3. Data-quality flags are preferable to silent deletion or unmarked imputation.

4. AI performance depends on context, provenance, and temporal correctness.

5. Raw retention enables investigation and future reprocessing.

10. Governance, Security, and Ethical Considerations

IoT data may reveal employee activity, occupancy, health conditions, production volumes, or household behaviour. Collection should therefore follow purpose limitation, data minimization, access control, and appropriate retention principles. Sensitive data should be encrypted in transit and at rest. Device credentials should be unique and rotated; firmware and gateways should support secure updates.

Governance should define data ownership, stewardship responsibilities, schema management, quality thresholds, incident response, and deletion procedures. Regulatory duties vary by sector and jurisdiction. Safety-critical systems may require formal validation, documented change control, and separation between monitoring and control functions.

AI-based alerts can affect workers, maintenance schedules, or access to services. Organizations should assess false positives, false negatives, explainability, human oversight, and possible bias caused by unequal sensor coverage. An automated alert should not be treated as unquestionable truth.

11. Challenges, Limitations, and Best Practices

Major challenges include unreliable connectivity, device heterogeneity, clock drift, schema evolution, high-cardinality data, storage cost, privacy risk, and operational complexity. Edge processing can reduce bandwidth and latency, but it introduces distributed deployment and update problems. Cloud processing offers elasticity, but connectivity and data-sovereignty concerns remain.

Recommended practices are:

  • define data contracts and schemas before deployment;
  • use immutable identifiers and explicit units;
  • record event, ingestion, and processing times;
  • design consumers to tolerate duplicates;
  • bound state and queue sizes;
  • preserve raw data when audit or reprocessing matters;
  • attach quality flags and provenance;
  • test late, missing, duplicated, and out-of-order events;
  • monitor throughput, lag, error rates, freshness, and storage growth;
  • version transformations and machine-learning models;
  • apply least-privilege security and encryption;
  • establish retention policies based on value and regulation;
  • separate safety controls from experimental analytics;
  • document assumptions and failure behaviour.

Contemporary Developments

Current IoT architectures increasingly combine edge computing, cloud-native stream platforms, digital twins, semantic metadata, and machine learning. Event-driven architectures are being extended with lakehouse designs that support both streaming and historical analytics. Open table formats and schema registries improve interoperability, while federated and privacy-preserving approaches seek to reduce movement of sensitive data.

Emerging systems also emphasize observability: not only monitoring infrastructure but measuring data freshness, lineage, quality, and model behaviour. This broader view is essential because a pipeline may be technically available while producing data that is too late, incomplete, or contextually incorrect for decision-making.

Chapter Summary

IoT data engineering creates the foundation on which real-time analytics and AI depend. An effective pipeline moves from acquisition and secure ingestion through synchronization, storage, cleaning, contextualization, stream processing, and action. Event time, late data, watermarks, windows, state, and delivery semantics are central to correct stream computation. Time-series databases support timestamped queries, while object, relational, and state stores serve complementary purposes.

Data quality must be treated as a measurable engineering property involving completeness, validity, accuracy, timeliness, consistency, uniqueness, and traceability. Provenance and retention enable auditability and reprocessing. Capacity planning uses event rates, record sizes, time horizons, and system overhead. In AI-enabled IoT, temporal leakage, feature consistency, model versioning, and safe fallback behaviour are essential. Security, privacy, governance, and human oversight must be designed into the pipeline rather than added afterward.

Key Terms

Event time: The time at which a physical event or measurement occurred.

Ingestion time: The time at which a platform receives a record.

Processing time: The time at which an application processes a record.

Publish-subscribe: A messaging pattern in which producers publish messages and consumers subscribe to topics or streams.

At-least-once delivery: Delivery semantics that may produce duplicate messages but aim not to lose acknowledged data.

Idempotence: The property that repeating an operation produces the same effective result as performing it once.

Back-pressure: A mechanism that slows or controls producers when consumers cannot keep up.

Watermark: A progress indicator used to handle incomplete and late event-time data.

Tumbling window: A fixed, adjacent, non-overlapping processing interval.

Hopping window: An overlapping window that advances by a specified interval.

Session window: A window formed by activity separated by inactivity gaps.

Stateful processing: Stream processing that maintains information across records.

Time-series database: A database optimized for timestamped observations and temporal queries.

Downsampling: Replacing high-resolution data with lower-resolution summaries.

Data provenance: A record of data origin, transformations, and processing history.

Data quality: The degree to which data is fit for its intended purpose.

Contextualization: Linking measurements with asset, location, operational, and business information.

Schema evolution: Controlled change to the structure or meaning of data records.

Temporal leakage: Use of information in model development that would not have been available at prediction time.

Retention policy: A rule specifying how long data is kept and in which storage tier.

Review Questions

Short Answer Questions

1. Distinguish event time from processing time.

2. What is the purpose of a watermark?

3. Why are immutable device identifiers useful?

4. Name four dimensions of IoT data quality.

5. What is the difference between raw and curated data layers?

Long Answer Questions

1. Explain the stages of an end-to-end IoT data pipeline and the engineering risks at each stage.

2. Compare at-most-once, at-least-once, and exactly-once-effect processing.

3. Explain how time-series databases, object storage, and relational databases can work together.

4. Discuss the role of provenance and retention in trustworthy IoT analytics.

5. Describe how stream processing supports AI-based predictive maintenance.

Analytical / Application Questions

1. A site has 500 devices, each sending 2 records per second, with an average payload of 250 bytes. Calculate the nominal record rate and uncompressed payload rate. State two additional factors that must be included in capacity planning.

2. Design an event-time processing strategy for sensors whose messages may arrive up to three minutes late. Discuss watermark choice, corrections, and operational trade-offs.

3. A temperature model performs poorly after a sensor replacement. Propose an investigation using identity management, provenance, calibration records, data-quality metrics, and model monitoring.

Multiple Choice Questions with Answers

Question 1

Which timestamp represents when the physical measurement occurred?

A. Event time

B. Ingestion time

C. Processing time

D. Retention time

Correct Answer: A. Event time

Question 2

What is a primary purpose of a watermark in stream processing?

A. Encrypting messages

B. Indicating progress in event-time processing

C. Compressing sensor payloads

D. Assigning device identities

Correct Answer: B. Indicating progress in event-time processing

Question 3

Which delivery model permits duplicate messages?

A. At-most-once

B. At-least-once

C. Never-once

D. Single-pass-only

Correct Answer: B. At-least-once

Question 4

Which window is fixed, adjacent, and non-overlapping?

A. Session window

B. Sliding window

C. Tumbling window

D. Unbounded window

Correct Answer: C. Tumbling window

Question 5

What is the main benefit of retaining raw IoT data?

A. It eliminates all storage costs

B. It enables auditing and reprocessing

C. It guarantees accurate sensors

D. It prevents late events

Correct Answer: B. It enables auditing and reprocessing

Question 6

If 100 devices each send 4 records per second, what is the nominal event rate?

A. 25 records per second

B. 104 records per second

C. 400 records per second

D. 4,000 records per second

Correct Answer: C. 400 records per second

Question 7

Which practice helps an IoT consumer handle at-least-once delivery safely?

A. Ignoring event identifiers

B. Using idempotent operations or deduplication

C. Deleting all retries

D. Using processing time only

Correct Answer: B. Using idempotent operations or deduplication

Question 8

What does contextualization add to a sensor measurement?

A. Only a larger file size

B. Asset, location, and operational meaning

C. A guarantee of privacy

D. A replacement for calibration

Correct Answer: B. Asset, location, and operational meaning

Question 9

Which issue occurs when a model uses information unavailable at prediction time?

A. Temporal leakage

B. Clock synchronization

C. Back-pressure

D. Downsampling

Correct Answer: A. Temporal leakage

Question 10

A stream produces 200 records per second, each averaging 100 bytes. What is the nominal payload rate?

A. 2,000 bytes per second

B. 20,000 bytes per second

C. 200,000 bytes per second

D. 2,000,000 bytes per second

Correct Answer: B. 20,000 bytes per second

Internet of Things (IoT) and AI
Chapter 5 of 14