Big Data Analytics

Chapter 9
Streaming and Real-Time Analytics

Bachelors Program in Business Analytics
Big Data Analytics   •   Bachelor

Streaming and Real-Time Analytics

Introduction

Many business decisions depend on events as they occur rather than after a daily or weekly report. A card authorization, a machine-temperature reading, a website click, a delivery scan, and a cybersecurity alert are all events. Streaming analytics is the continuous collection, transport, processing, and analysis of such events. It supports decisions that are time-sensitive, including blocking payment fraud, shutting down unsafe equipment, rerouting deliveries, and recommending products during a customer session.

Streaming does not simply mean “fast batch processing.” Batch systems collect a bounded dataset and process it as a job. Streaming systems process an effectively unbounded sequence of events and must address arrival order, delayed data, changing state, failures, duplicate messages, and operational latency. Many modern architectures are hybrid: historical data is processed in batch, while recent events are processed continuously.

Learning Objectives

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

  1. Distinguish batch, micro-batch, and continuous streaming architectures.
  2. Explain the roles of message brokers, producers, consumers, partitions, offsets, and delivery guarantees.
  3. Apply tumbling, hopping, sliding, and session windows.
  4. Distinguish event time, processing time, and ingestion time.
  5. Explain watermarks, late data, stateful processing, and checkpointing.
  6. Design basic complex event processing and streaming analytics solutions.
  7. Compare streaming and batch designs for fraud, IoT, monitoring, and personalization.
  8. Evaluate performance, reliability, governance, ethical, and operational trade-offs.

1. The Event-Stream Model

An event is a record that describes something that happened. A useful event normally contains an event identifier, event type, entity identifier, event timestamp, source, payload, and sometimes a schema version. For example:

Event ID: 8f31...
Type: payment_authorized
Customer: C204
Amount: 480.00 USD
Event time: 2025-03-14 10:01:07.420 UTC
Source: mobile_app
Schema version: 3

An event stream is an ordered sequence from a particular source or partition. The global stream may not have one total order because events from independent devices or regions can arrive concurrently. This distinction matters: a broker’s delivery order is not necessarily the order in which business events occurred.

A streaming application commonly contains five stages:

  1. Production: applications, sensors, databases, or services create events.
  2. Transport: a message broker stores and distributes events.
  3. Processing: a stream processor filters, joins, aggregates, enriches, or detects patterns.
  4. Serving: results are written to dashboards, databases, alerts, or operational systems.
  5. Governance: schemas, access controls, retention, quality checks, and audit records are maintained.

A useful performance measure is end-to-end latency:

Le2e = toutput − tevent

Here, tevent is the event-generation time and toutput is the time at which a usable result is produced. The formula assumes both timestamps use the same clock standard and time zone. In practice, clock skew means latency should be interpreted with care. Processing latency, broker latency, and sink latency may be measured separately.

2. Batch, Micro-Batch, and Continuous Processing

Batch analytics reads a finite input, computes a result, and terminates. It is efficient for complete historical recalculation, large joins, regulatory reports, and machine-learning feature generation. Its principal weakness is decision delay.

Micro-batch processing groups events into small batches, such as one-second or one-minute intervals. It can use mature batch engines while providing near-real-time results. Continuous processing treats records individually or in very small groups and can reduce latency, but it generally requires more careful state management and fault handling.

DesignInputTypical strengthTypical limitation
BatchBounded dataHigh throughput and reproducibilityDelayed results
Micro-batchSmall bounded intervalsSimpler operational modelInterval-based latency
Continuous streamingUnbounded event flowLow latencyMore complex correctness and recovery
HybridHistorical plus live dataBalanced freshness and contextMore complex consistency management

The choice should be based on decision latency, data completeness, cost, correctness, and recovery requirements—not on the label “real time.” A fraud decision may require milliseconds, whereas a warehouse utilization report may be adequately refreshed every five minutes.

3. Message Brokers and Stream Architecture

A message broker decouples event producers from consumers. Producers publish events to named topics or streams. A topic may be divided into partitions, allowing parallel processing. Each message typically has an offset, a position within its partition. Consumers record offsets so they can resume after failure.

A consumer group divides partitions among instances. This supports horizontal scaling, but it also creates a design constraint: within a consumer group, a partition is normally assigned to one consumer at a time. A key such as customer ID or device ID is often used to select a partition. Keying related events together helps preserve per-entity order and makes stateful processing easier.

Important broker properties include:

  • Retention: how long messages remain available.
  • Replication: how many broker nodes maintain copies.
  • Ordering: usually guaranteed within a partition, not across all partitions.
  • Delivery semantics: at-most-once, at-least-once, or effectively exactly-once under specified conditions.
  • Backpressure: mechanisms that prevent a fast producer from overwhelming a slower consumer.

At-most-once delivery may lose records but avoids duplicates. At-least-once delivery prevents loss under normal recovery but may deliver a record more than once. Exactly-once processing is not a universal property of a system; it requires coordinated state, offsets, and outputs, and the external destination must also support suitable transactional or idempotent behavior.

An idempotent operation can be safely repeated. For example, setting an account status to “blocked” is usually idempotent, while charging a credit card is not unless a unique transaction key prevents duplicate charging. Production designs should therefore include event IDs, deduplication, and idempotent sinks.

4. Time in Streaming Systems

Three notions of time must be distinguished:

  • Event time: when the event occurred at its source.
  • Ingestion time: when the platform received the event.
  • Processing time: when a processing operator handled it.

Processing time is easy to obtain but can produce incorrect business results when networks delay events. Event time usually expresses the business meaning more accurately, but it requires timestamp quality, clock synchronization, and late-data policies.

Consider a sensor reading generated at 10:00:02, received at 10:00:10, and processed at 10:00:11. Its event time is 10:00:02, ingestion time is 10:00:10, and processing time is 10:00:11. A five-second event-time window should place the reading in the interval based on 10:00:02, not based on its later arrival.

Windows

Because a stream is unbounded, an aggregation such as “average temperature” requires a boundary. A window defines which events are considered together.

  • Tumbling window: fixed, non-overlapping intervals. A ten-minute window creates 10:00–10:10, 10:10–10:20, and so on.
  • Hopping window: fixed-length windows that start at regular intervals and may overlap. A ten-minute window that advances every five minutes overlaps by five minutes.
  • Sliding window: commonly describes a continuously moving interval, often maintained by removing expired events as new events arrive.
  • Session window: a dynamic period of activity that closes after a specified inactivity gap.

For a count in a window W, the conceptual result is:

count(W) = ∑i=1n 1(eventi ∈ W)

Here, 1(·) equals 1 when the event belongs to the window and 0 otherwise. The equation assumes a defined inclusion rule for boundary timestamps, such as including the start and excluding the end. That rule avoids counting an event twice at adjacent boundaries.

A ten-minute tumbling window beginning at 12:00 includes events with timestamps 12:00:00 ≤ t < 12:10:00. A hopping design may generate multiple results for one event; this is appropriate when overlapping context is analytically meaningful.

Watermarks and Late Data

A watermark is an estimate that the system has probably observed events up to a particular event-time position. If the watermark is 12:10, an operator may consider windows ending before 12:10 ready to finalize, subject to the configured lateness policy. Watermarks do not prove that no earlier event will arrive; they express a trade-off between completeness and timeliness.

A common policy allows events to arrive up to a lateness threshold δ. An event with event time te arriving at processing time tp has observed lateness approximately:

lateby = tp − te

This calculation assumes comparable clocks and is an operational approximation. Events later than δ may be discarded, sent to a late-data stream, or used to revise previously published results. The correct policy depends on business cost. A financial report may require correction; a low-value dashboard may prefer stable, prompt output.

5. Stateful Stream Processing

A stateless operation depends only on the current event—for example, filtering payments above zero. A stateful operation remembers information from previous events, such as a customer’s transaction count in the last hour or a device’s current operating mode.

Typical state includes:

  • keyed aggregates, such as total spending by card;
  • window contents or incremental summaries;
  • last-known values, such as device status;
  • timers for inactivity or deadlines;
  • pattern-matching progress;
  • join buffers for events from different streams.

State must be partitioned, persisted, and recovered. Checkpointing periodically stores operator state and input positions. After a failure, a processor restores a checkpoint and replays messages. Checkpoint frequency involves a trade-off: frequent checkpoints reduce recovery work but increase overhead; infrequent checkpoints improve normal throughput but increase recovery time.

State growth is a major risk. A design should use bounded windows, time-to-live policies, compact aggregates, and explicit deletion rules. A count can often be maintained incrementally rather than retaining every event. For a running sum:

St = St−1 + xt

where St is the sum after event t and xt is the new value. This recurrence is valid for an append-only sequence; updates, retractions, and late corrections require additional logic.

6. Joins and Complex Event Processing

Streaming joins combine events from two or more streams. A payment may be joined with customer risk data; a machine alert may be joined with maintenance records. Because streams are unbounded, joins generally require a time constraint or retained state. A temporal join might match events for the same device when their timestamps fall within a specified interval.

Complex event processing (CEP) identifies meaningful patterns across events rather than evaluating each event independently. Examples include:

  • login failure followed by a successful login from a distant location;
  • temperature above a threshold followed by vibration escalation;
  • repeated small purchases followed by a high-value purchase;
  • a shipment scan missing after an expected time interval.

CEP commonly uses sequences, conditions, repetition, absence, correlation keys, and time constraints. A pattern should specify what happens if events are duplicated, arrive out of order, or never complete. Alert suppression and cooldown periods are important because repeated alerts can overwhelm operators.

7. Reliability, Quality, and Observability

A dependable streaming system monitors both infrastructure and analytical behavior. Useful metrics include throughput, consumer lag, processing latency, watermark delay, error rate, duplicate rate, state size, checkpoint duration, and sink success rate.

Consumer lag is the distance between the newest available broker position and the consumer’s current position. A growing lag indicates that processing capacity is below incoming demand, although temporary bursts may be harmless. Scaling consumers is effective only when sufficient partitions and downstream capacity exist.

Data-quality controls should check schema validity, required fields, timestamp plausibility, unit consistency, duplicate IDs, impossible values, and referential integrity. A malformed event should be quarantined or routed to a dead-letter stream rather than silently discarded.

A simple capacity relationship is:

ρ = λ / μ

Here, λ is the incoming event rate in events per second, μ is the sustainable processing rate in events per second, and ρ is utilization. The relationship is a planning indicator, not a complete queueing model. If ρ approaches or exceeds 1 for a sustained period, backlog is likely to grow. Real systems also require headroom for bursts, failures, network delay, and state operations.

8. Applications

Fraud Detection

A streaming fraud system can score a payment using amount, merchant, location, device, prior activity, and current velocity. A rule might flag five transactions from the same card in ten minutes, while a machine-learning model estimates risk from a richer feature set. Streaming provides immediate intervention; batch processing remains valuable for model training, historical investigation, and periodic recalibration.

A robust design separates authorization from later investigation. A low-latency path may use compact state and a cached feature store. A slower analytical path can join transactions with broader customer history. False positives have financial and reputational costs, so threshold performance should be evaluated using precision, recall, approval rate, customer friction, and monetary loss—not accuracy alone.

IoT and Predictive Maintenance

Sensors generate high-volume measurements. Edge filtering can remove impossible values or summarize high-frequency signals before transmission. The stream processor can detect threshold breaches, rate of change, and combinations such as high temperature plus abnormal vibration. Batch analytics can train failure-prediction models using historical maintenance outcomes.

Unit metadata is essential. A temperature of 80 may mean Celsius or Fahrenheit; without a unit, the value is not analytically safe. Device identity, calibration status, and firmware version should accompany measurements.

Operational Monitoring

Applications emit logs, metrics, and traces. Streaming aggregation can calculate error rates, latency percentiles, and service availability over short windows. Alerts should reflect severity, duration, and dependency context. A five-second spike may not justify paging an engineer, whereas a sustained error rate combined with failed health checks may require immediate action.

Personalization

A recommendation service can use current-session clicks, searches, and cart events to update recommendations immediately. Streaming features capture intent while batch features provide longer-term preferences. Personalization must address consent, sensitive inferences, explainability, and the risk of creating filter bubbles or unfairly differentiated offers.

9. Illustrative Case: MetroPay Real-Time Fraud Detection

Background

MetroPay, a fictional digital-payments provider, processes card and mobile-wallet transactions. Its existing nightly batch report identifies suspicious accounts after losses have occurred. The company wants to reduce fraud without rejecting legitimate customers.

Problem

Transactions arrive from mobile applications, merchants, and automated payment services. Some mobile events are delayed by weak connectivity. The system must produce a preliminary decision within 500 milliseconds, tolerate duplicate delivery, and preserve an auditable history.

Analysis

The team identifies three time-sensitive signals: transaction velocity, geographic inconsistency, and device novelty. It also identifies two slower signals: historical customer risk and merchant risk. A single batch design cannot meet the intervention requirement, while a purely streaming design would make historical retraining and reconciliation unnecessarily difficult.

Approach

MetroPay adopts a hybrid architecture. A broker receives payment, login, device, and account events. Events are keyed by account or card token so related state is colocated. A streaming processor maintains ten-minute and one-hour counts, the last known device, and recent approximate location. It uses event time with a two-minute lateness allowance. Events beyond that allowance enter a correction stream and are retained for investigation.

Each event carries a unique transaction ID. The processor writes a decision using an idempotent key so replay does not create a second authorization action. Checkpoints store state and source offsets. A batch platform trains a risk model each night and publishes a versioned model artifact. The live processor loads the approved model and records the model version with each decision.

A simplified decision policy is:

  • approve when risk is low and no blocking rule is active;
  • request additional authentication when risk is moderate;
  • decline or hold when high risk is supported by multiple signals;
  • route uncertain or late cases for review.

Outcome

In an illustrative pilot, intervention time falls from hours to under one second for events in the normal path. The organization also discovers that its principal operational challenge is not model computation but late data, duplicate events, and customer-service handling of false positives. It therefore adds replay testing, event-quality dashboards, and an appeal process.

Learning Points

The case demonstrates that streaming analytics is an organizational system, not only a processing engine. Event contracts, state design, latency budgets, model governance, recovery procedures, and human escalation all influence business value.

10. Ethical, Security, and Regulatory Considerations

Real-time systems can make consequential decisions before a person can review the evidence. Organizations should document the purpose of each feature, limit collection to what is necessary, and protect personal and operational data in transit and at rest. Access controls should distinguish production operators, analysts, model developers, and auditors.

Important controls include encryption, tokenization of payment identifiers, retention limits, audit logs, schema ownership, incident response, and data-subject rights where applicable. A streaming system should support deletion or correction requirements when legally necessary, although immutable logs can make this technically difficult. Retaining a raw event forever is not automatically better for auditability.

Models and rules should be tested for disparate error rates across relevant groups. Explainable reason codes help customers and investigators understand a decision. Automated decisions should have an escalation or appeal pathway where their consequences are significant.

11. Best Practices and Contemporary Developments

Effective streaming projects commonly follow these practices:

  1. Define business latency and correctness requirements before selecting technology.
  2. Establish versioned event schemas and ownership responsibilities.
  3. Choose partition keys that support ordering, parallelism, and state locality.
  4. Specify event-time, watermark, lateness, correction, and boundary policies explicitly.
  5. Design for duplicates, retries, replay, failure, and disaster recovery.
  6. Make sinks idempotent or transactional where consequences matter.
  7. Bound state and monitor its growth.
  8. Separate urgent operational paths from exploratory analytical paths.
  9. Test with out-of-order, duplicated, missing, bursty, and malformed events.
  10. Measure business outcomes as well as technical metrics.

Contemporary platforms increasingly combine stream processing with lakehouse storage, feature stores, vector or semantic search, and online machine learning. Event-driven architectures allow services to react to changes rather than repeatedly polling databases. Edge analytics reduces bandwidth and latency for industrial and mobile applications. However, newer tools do not remove fundamental responsibilities: time semantics, data quality, governance, and reliable recovery remain central.

Chapter Summary

Streaming and real-time analytics process unbounded event flows to support timely decisions. Message brokers provide durable, scalable transport through topics, partitions, offsets, and consumer groups. Event time is usually more meaningful than processing time, but delayed and out-of-order data require watermarks and lateness policies. Windows make unbounded streams computable; tumbling, hopping, sliding, and session windows serve different analytical purposes.

Stateful processing maintains aggregates, histories, timers, and pattern progress. Checkpointing, replay, deduplication, and idempotent outputs support recovery and reliable results. Complex event processing identifies sequences and combinations such as fraud patterns or equipment failures. Batch and streaming designs are complementary: streaming supports immediate action, while batch supports historical analysis, training, reconciliation, and reporting. Successful systems also require observability, security, privacy, fairness, schema governance, and clear operational ownership.

Key Terms

Event: A record describing something that happened.

Event stream: An unbounded sequence of events.

Message broker: Infrastructure that stores and distributes messages between producers and consumers.

Topic: A named stream of related messages.

Partition: An ordered subdivision of a topic used for storage and parallelism.

Offset: A position identifying a message within a partition.

Consumer group: Consumers cooperating to process partitions of a topic.

Event time: The time at which an event occurred at its source.

Processing time: The time at which a processor handles an event.

Window: A bounded subset of a stream used for computation.

Tumbling window: A fixed, non-overlapping window.

Hopping window: A fixed window that advances at a regular interval and may overlap.

Session window: A window defined by periods of activity separated by inactivity.

Watermark: An estimate of event-time progress used to finalize results.

Late data: An event that arrives after the expected event-time progress.

Stateful processing: Processing that retains information across events.

Checkpoint: Persisted operator state and input position used for recovery.

Idempotence: The property that repeating an operation has the same intended effect as performing it once.

Complex event processing: Detection of meaningful patterns across multiple events.

Consumer lag: The distance between available input and a consumer’s processing position.

Review Questions

Short Answer Questions

  1. What is the difference between event time and processing time?
  2. Why are partitions used in message brokers?
  3. Define a tumbling window and a session window.
  4. What problem does checkpointing address?
  5. Why is idempotence important in at-least-once processing?

Long Answer Questions

  1. Compare batch, micro-batch, continuous streaming, and hybrid architectures.
  2. Explain how watermarks and lateness policies affect streaming correctness.
  3. Discuss the design of a stateful streaming application, including state growth and recovery.
  4. Explain how complex event processing can support fraud and predictive maintenance.
  5. Evaluate the ethical and governance issues involved in real-time automated decisions.

Analytical / Application Questions

  1. A retailer receives delayed click events and needs accurate ten-minute session metrics. Choose a time basis, window type, watermark policy, and late-data treatment, and justify each choice.
  2. Design a streaming architecture for detecting unsafe factory conditions using temperature and vibration events. Specify keys, state, windows, alerts, recovery, and batch complements.
  3. A payment processor’s consumer lag grows during bursts and duplicate authorizations appear after recovery. Diagnose likely causes and propose technical and operational remedies.

Multiple Choice Questions with Answers

Question 1

Which time notion most directly represents when a business event actually occurred?

A. Processing time

B. Event time

C. Checkpoint time

D. Retention time

Correct Answer: B. Event time

Question 2

What is the primary purpose of a watermark?

A. To encrypt messages

B. To assign partitions

C. To estimate event-time progress and finalize windows

D. To increase sensor precision

Correct Answer: C. To estimate event-time progress and finalize windows

Question 3

Which window consists of fixed, non-overlapping intervals?

A. Session window

B. Sliding window

C. Hopping window

D. Tumbling window

Correct Answer: D. Tumbling window

Question 4

What does an offset usually identify?

A. A model’s accuracy

B. A message’s position within a partition

C. A customer’s risk score

D. A window’s lateness threshold

Correct Answer: B. A message’s position within a partition

Question 5

Which delivery mode may produce duplicate messages but is designed to avoid loss during normal recovery?

A. At-least-once

B. At-most-once

C. Never-once

D. Batch-only

Correct Answer: A. At-least-once

Question 6

Why is an event key such as customer ID useful in stateful processing?

A. It removes the need for timestamps

B. It places related events together for ordering and state locality

C. It guarantees global ordering

D. It prevents all late data

Correct Answer: B. It places related events together for ordering and state locality

Question 7

If incoming rate λ is 800 events per second and sustainable processing rate μ is 1,000 events per second, what is utilization ρ = λ ÷ μ?

A. 0.08

B. 0.80

C. 1.25

D. 1,800

Correct Answer: B. 0.80

Question 8

Which operation is most clearly idempotent?

A. Charging a card without a transaction key

B. Appending a new payment record on every retry

C. Setting an account status to blocked

D. Sending a promotional message on every retry

Correct Answer: C. Setting an account status to blocked

Question 9

Which application best illustrates complex event processing?

A. Sorting a static customer table

B. Detecting a failed login followed by an unusual successful login

C. Formatting a monthly spreadsheet

D. Compressing an archived image

Correct Answer: B. Detecting a failed login followed by an unusual successful login

Question 10

Why do organizations commonly combine batch and streaming analytics?

A. Batch systems always have lower latency

B. Streaming systems cannot process current events

C. Batch supports historical computation while streaming supports timely decisions

D. The two systems use identical processing semantics

Correct Answer: C. Batch supports historical computation while streaming supports timely decisions

Big Data Analytics
Chapter 9 of 14