Back to Kafka Streams
Q20Question 20 of 27

What are the different window types in Kafka Streams

Answer: Windowing in Kafka Streams groups records into time-based buckets so you can perform aggregations like counting transactions per minute, where tumbling windows are non-overlapping fixed buckets, hopping windows are overlapping fixed buckets, sliding windows move with each record, and session windows close after an inactivity gap.

Key Concepts
  • Windowing groups records into time-based buckets for aggregation operations like count, reduce, and aggregate.
  • Tumbling windows are fixed-size, non-overlapping windows where each record belongs to exactly one window.
  • Hopping windows are fixed-size but overlap, so a record can belong to multiple windows.
  • Sliding windows emit a result whenever a new record enters or an old record leaves the window boundary.
  • Session windows group records by activity, closing a window after a defined inactivity gap.
  • Each window type creates a windowed state store backed by RocksDB and a changelog topic for fault tolerance.
  • Use tumbling for periodic reports, hopping for moving averages, sliding for joins, and session for user activity tracking.

Definition

Windowing in Kafka Streams groups records into time-based buckets so you can perform aggregations like counting transactions per minute, where tumbling windows are non-overlapping fixed buckets, hopping windows are overlapping fixed buckets, sliding windows move with each record, and session windows close after an inactivity gap.
  • Tumbling window divides time into fixed-size, non-overlapping intervals; each record falls into exactly one window (e.g., every 5 minutes).
  • Hopping window divides time into fixed-size intervals that overlap; the advance interval determines how often a new window starts (e.g., 5-minute window advancing every 1 minute).
  • Sliding window has no fixed alignment; a new window is evaluated whenever a record enters or exits the window boundary, used primarily in joins.
  • Session window groups records by activity; a new record extends the session, and the window closes when no record arrives within the inactivity gap.
  • Windowed state store is the RocksDB-backed store that holds aggregated results per window key, enabling lookup of results by key and time range.

Think of National Bank of India counting ATM withdrawals for fraud detection. A tumbling window is like counting withdrawals every hour on the hour: 10:00 to 11:00, 11:00 to 12:00, each hour is separate. A hopping window is like counting withdrawals in the last hour but checking every 15 minutes, so the counts overlap. A sliding window is like an alarm that fires whenever 5 withdrawals happen within any 10-minute span, regardless of clock alignment. A session window is like tracking a customer's visit: the session starts with the first withdrawal and ends after 30 minutes of no activity.

Understanding the Concepts

What is windowing in Kafka Streams?

Windowing divides a continuous stream of records into finite time-based groups called windows. You apply windowing before an aggregation operation (count, reduce, aggregate) to compute results per time bucket. Without windowing, an aggregation would produce a single running total for all time.

Windowed aggregations produce a KTable where each key is a Windowed containing the original key plus the window start and end time.

JAVA
package com.bank.streams;

public class WindowingOverview {
    public static void main(String[] args) {
        System.out.println("Windowing in Kafka Streams:");
        System.out.println("  Purpose: group records into time buckets for aggregation");
        System.out.println("  Without windowing: count() gives a single running total");
        System.out.println("  With windowing: count() gives a total per time bucket");
        System.out.println();
        System.out.println("  Example: count ATM withdrawals per account per hour");
        System.out.println("    groupByKey()");
        System.out.println("    .windowedBy(TimeWindows.ofSizeWithNoGrace(Duration.ofHours(1)))");
        System.out.println("    .count()");
        System.out.println();
        System.out.println("  Result key: Windowed<SAV2001> [10:00, 11:00)");
        System.out.println("  Result value: 7 (withdrawals in that hour)");
    }
}
TEXT
Expected output:
Windowing in Kafka Streams:
  Purpose: group records into time buckets for aggregation
  Without windowing: count() gives a single running total
  With windowing: count() gives a total per time bucket

  Example: count ATM withdrawals per account per hour
    groupByKey()
    .windowedBy(TimeWindows.ofSizeWithNoGrace(Duration.ofHours(1)))
    .count()

  Result key: Windowed<SAV2001> [10:00, 11:00)
  Result value: 7 (withdrawals in that hour)

How does a tumbling window work?

A tumbling window is a fixed-size, non-overlapping window. Time is divided into equal intervals. Each record falls into exactly one window based on its timestamp.

For a 5-minute tumbling window starting at 10:00, the windows are [10:00, 10:05), [10:05, 10:10), [10:10, 10:15), and so on. A record with timestamp 10:03 goes into the first window. A record with timestamp 10:06 goes into the second window.

JAVA
package com.bank.streams;

import java.time.Duration;
import java.time.Instant;

public class TumblingWindowExample {
    public static void main(String[] args) {
        Duration windowSize = Duration.ofMinutes(5);
        Instant[] timestamps = {
            Instant.parse("2024-01-15T10:01:00Z"),
            Instant.parse("2024-01-15T10:03:00Z"),
            Instant.parse("2024-01-15T10:06:00Z"),
            Instant.parse("2024-01-15T10:09:00Z"),
            Instant.parse("2024-01-15T10:11:00Z")
        };

        System.out.println("Tumbling window: 5 minutes, non-overlapping");
        System.out.println();
        for (Instant ts : timestamps) {
            long epochMinutes = ts.getEpochSecond() / 60;
            long windowStart = (epochMinutes / 5) * 5;
            long windowEnd = windowStart + 5;
            System.out.println("  Record at " + ts
                + " -> window [" + windowStart % 60 + ":00, "
                + windowEnd % 60 + ":00)");
        }
    }
}
TEXT
Expected output:
Tumbling window: 5 minutes, non-overlapping

  Record at 2024-01-15T10:01:00Z -> window [0:00, 5:00)
  Record at 2024-01-15T10:03:00Z -> window [0:00, 5:00)
  Record at 2024-01-15T10:06:00Z -> window [5:00, 10:00)
  Record at 2024-01-15T10:09:00Z -> window [5:00, 10:00)
  Record at 2024-01-15T10:11:00Z -> window [10:00, 15:00)

How does a hopping window work?

A hopping window has a fixed size and an advance interval (hop). A new window starts every advance interval, and windows overlap. A record can fall into multiple windows.

For a 10-minute window with a 5-minute hop, the windows are [10:00, 10:10), [10:05, 10:15), [10:10, 10:20). A record at 10:07 belongs to both the first and second window.

JAVA
package com.bank.streams;

import java.time.Duration;
import java.time.Instant;

public class HoppingWindowExample {
    public static void main(String[] args) {
        long windowSizeMin = 10;
        long hopMin = 5;
        Instant record = Instant.parse("2024-01-15T10:07:00Z");
        long recordMin = (record.getEpochSecond() / 60) % 60;

        System.out.println("Hopping window: size=10min, hop=5min");
        System.out.println("Record timestamp: " + record);
        System.out.println();
        System.out.println("Windows this record belongs to:");

        // Simulating window assignment
        for (long start = 0; start <= recordMin; start += hopMin) {
            long end = start + windowSizeMin;
            if (recordMin >= start && recordMin < end) {
                System.out.println("  [" + start + ":00, " + end + ":00)");
            }
        }
        System.out.println();
        System.out.println("The record at 10:07 falls into 2 overlapping windows.");
    }
}
TEXT
Expected output:
Hopping window: size=10min, hop=5min
Record timestamp: 2024-01-15T10:07:00Z

Windows this record belongs to:
  [0:00, 10:00)
  [5:00, 15:00)

The record at 10:07 falls into 2 overlapping windows.

How does a sliding window work?

A sliding window does not have fixed alignment to the clock. Instead, a new window is created whenever a record enters or leaves the window boundary. Sliding windows are primarily used in join operations (JoinWindows) and in SlidingWindows for aggregation (Kafka 2.7+).

For a 10-minute sliding window, if records arrive at 10:01, 10:05, and 10:12, windows are created around each record: [09:51, 10:01], [09:55, 10:05], [10:02, 10:12], and so on.

JAVA
package com.bank.streams;

import java.time.Instant;
import java.time.Duration;

public class SlidingWindowExample {
    public static void main(String[] args) {
        Duration windowSize = Duration.ofMinutes(10);
        Instant[] records = {
            Instant.parse("2024-01-15T10:01:00Z"),
            Instant.parse("2024-01-15T10:05:00Z"),
            Instant.parse("2024-01-15T10:12:00Z")
        };

        System.out.println("Sliding window: 10 minutes");
        System.out.println("Unlike tumbling/hopping, windows are not clock-aligned.");
        System.out.println();

        for (Instant record : records) {
            Instant windowStart = record.minus(windowSize);
            Instant windowEnd = record;
            System.out.println("  Record at " + record);
            System.out.println("    Window: [" + windowStart + ", " + windowEnd + "]");
        }
        System.out.println();
        System.out.println("Sliding windows recalculate whenever a record enters or exits.");
        System.out.println("Used for: SlidingWindows aggregation, JoinWindows.");
    }
}
TEXT
Expected output:
Sliding window: 10 minutes
Unlike tumbling/hopping, windows are not clock-aligned.

  Record at 2024-01-15T10:01:00Z
    Window: [2024-01-15T09:51:00Z, 2024-01-15T10:01:00Z]
  Record at 2024-01-15T10:05:00Z
    Window: [2024-01-15T09:55:00Z, 2024-01-15T10:05:00Z]
  Record at 2024-01-15T10:12:00Z
    Window: [2024-01-15T10:02:00Z, 2024-01-15T10:12:00Z]

Sliding windows recalculate whenever a record enters or exits.
Used for: SlidingWindows aggregation, JoinWindows.

How does a session window work?

A session window groups records by activity. There is no fixed window size. Instead, you define an inactivity gap. If no record arrives for a key within the gap duration, the session closes. If a new record arrives before the gap expires, the session extends.

This is useful for tracking user sessions. For example, tracking how many ATM transactions a customer makes in one visit to the bank.

JAVA
package com.bank.streams;

import java.time.Instant;
import java.time.Duration;

public class SessionWindowExample {
    public static void main(String[] args) {
        Duration gap = Duration.ofMinutes(5);

        Instant[] withdrawals = {
            Instant.parse("2024-01-15T10:00:00Z"),
            Instant.parse("2024-01-15T10:02:00Z"),
            Instant.parse("2024-01-15T10:04:00Z"),
            // 8 minute gap here
            Instant.parse("2024-01-15T10:12:00Z"),
            Instant.parse("2024-01-15T10:14:00Z")
        };

        System.out.println("Session window with 5-minute inactivity gap");
        System.out.println("Account: SAV2001 (Priya's ATM visits)");
        System.out.println();

        System.out.println("Session 1:");
        System.out.println("  " + withdrawals[0] + " withdrawal 1");
        System.out.println("  " + withdrawals[1] + " withdrawal 2 (2 min later, session extends)");
        System.out.println("  " + withdrawals[2] + " withdrawal 3 (2 min later, session extends)");
        System.out.println("  Session: [10:00, 10:04] count=3");
        System.out.println();
        System.out.println("  <8-minute gap, session closes>");
        System.out.println();
        System.out.println("Session 2:");
        System.out.println("  " + withdrawals[3] + " withdrawal 4 (new session)");
        System.out.println("  " + withdrawals[4] + " withdrawal 5 (2 min later, session extends)");
        System.out.println("  Session: [10:12, 10:14] count=2");
    }
}
TEXT
Expected output:
Session window with 5-minute inactivity gap
Account: SAV2001 (Priya's ATM visits)

Session 1:
  2024-01-15T10:00:00Z withdrawal 1
  2024-01-15T10:02:00Z withdrawal 2 (2 min later, session extends)
  2024-01-15T10:04:00Z withdrawal 3 (2 min later, session extends)
  Session: [10:00, 10:04] count=3

  <8-minute gap, session closes>

Session 2:
  2024-01-15T10:12:00Z withdrawal 4 (new session)
  2024-01-15T10:14:00Z withdrawal 5 (2 min later, session extends)
  Session: [10:12, 10:14] count=2

How do the four window types compare?

FeatureTumblingHoppingSlidingSession
SizeFixedFixedFixedVariable
OverlapNoYesYesNo (merges)
AlignmentClock-alignedClock-alignedRecord-alignedActivity-based
Record belongs to1 windowMultiple windowsMultiple windows1 session
Advance= window size< window sizePer recordPer activity
Use casePeriodic reportsMoving averagesJoin windowsUser sessions
API classTimeWindowsTimeWindowsSlidingWindowsSessionWindows
State sizeLowMediumMediumVariable
JAVA
package com.bank.streams;

public class WindowComparisonDemo {
    public static void main(String[] args) {
        System.out.println("Window type comparison for banking:");
        System.out.println();
        System.out.println("Tumbling (1 hour):");
        System.out.println("  Count transactions per hour for daily report");
        System.out.println("  [10:00-11:00) [11:00-12:00) [12:00-13:00)");
        System.out.println();
        System.out.println("Hopping (1 hour, advance 15 min):");
        System.out.println("  Rolling average balance updated every 15 min");
        System.out.println("  [10:00-11:00) [10:15-11:15) [10:30-11:30)");
        System.out.println();
        System.out.println("Sliding (10 min):");
        System.out.println("  Alert if 5+ withdrawals in any 10-min span");
        System.out.println("  Windows created per record, not clock-aligned");
        System.out.println();
        System.out.println("Session (30 min gap):");
        System.out.println("  Track online banking sessions per customer");
        System.out.println("  Session ends after 30 min of inactivity");
    }
}
TEXT
Expected output:
Window type comparison for banking:

Tumbling (1 hour):
  Count transactions per hour for daily report
  [10:00-11:00) [11:00-12:00) [12:00-13:00)

Hopping (1 hour, advance 15 min):
  Rolling average balance updated every 15 min
  [10:00-11:00) [10:15-11:15) [10:30-11:30)

Sliding (10 min):
  Alert if 5+ withdrawals in any 10-min span
  Windows created per record, not clock-aligned

Session (30 min gap):
  Track online banking sessions per customer
  Session ends after 30 min of inactivity

What is a complete banking project for fraud detection using windowing?

This project counts ATM withdrawals per account in 5-minute tumbling windows. If the count exceeds a threshold (3 withdrawals in 5 minutes), it writes a fraud alert to an output topic.

Project structure

DIAGRAM
fraud-detection-service/
├── pom.xml
└── src/
    ├── main/
    │   └── java/
    │       └── com/
    │           └── bank/
    │               └── streams/
    │                   ├── FraudDetectionApp.java
    │                   ├── FraudDetectionTopology.java
    │                   └── AtmWithdrawal.java
    └── test/
        └── java/
            └── com/
                └── bank/
                    └── streams/
                        └── FraudDetectionTopologyTest.java

pom.xml

XML
<?xml version="1.0" encoding="UTF-8"?syn-kw">>
syn-kw"><project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
         http://maven.apache.org/xsd/maven-4.0.0.xsd"syn-kw">>
    syn-kw"><modelVersionsyn-kw">>4.0.0syn-kw"></modelVersionsyn-kw">>
    syn-kw"><groupIdsyn-kw">>com.banksyn-kw"></groupIdsyn-kw">>
    syn-kw"><artifactIdsyn-kw">>fraud-detection-servicesyn-kw"></artifactIdsyn-kw">>
    syn-kw"><versionsyn-kw">>1.0.0syn-kw"></versionsyn-kw">>
    syn-kw"><packagingsyn-kw">>jarsyn-kw"></packagingsyn-kw">>
    syn-kw"><propertiessyn-kw">>
        syn-kw"><java.versionsyn-kw">>25syn-kw"></java.versionsyn-kw">>
        syn-kw"><kafka.versionsyn-kw">>4.2.0syn-kw"></kafka.versionsyn-kw">>
        syn-kw"><maven.compiler.sourcesyn-kw">>25syn-kw"></maven.compiler.sourcesyn-kw">>
        syn-kw"><maven.compiler.targetsyn-kw">>25syn-kw"></maven.compiler.targetsyn-kw">>
    syn-kw"></propertiessyn-kw">>
    syn-kw"><dependenciessyn-kw">>
        syn-kw"><dependencysyn-kw">>
            syn-kw"><groupIdsyn-kw">>org.apache.kafkasyn-kw"></groupIdsyn-kw">>
            syn-kw"><artifactIdsyn-kw">>kafka-streamssyn-kw"></artifactIdsyn-kw">>
            syn-kw"><versionsyn-kw">>${kafka.version}syn-kw"></versionsyn-kw">>
        syn-kw"></dependencysyn-kw">>
        syn-kw"><dependencysyn-kw">>
            syn-kw"><groupIdsyn-kw">>org.apache.kafkasyn-kw"></groupIdsyn-kw">>
            syn-kw"><artifactIdsyn-kw">>kafka-streams-test-utilssyn-kw"></artifactIdsyn-kw">>
            syn-kw"><versionsyn-kw">>${kafka.version}syn-kw"></versionsyn-kw">>
            syn-kw"><scopesyn-kw">>testsyn-kw"></scopesyn-kw">>
        syn-kw"></dependencysyn-kw">>
        syn-kw"><dependencysyn-kw">>
            syn-kw"><groupIdsyn-kw">>org.slf4jsyn-kw"></groupIdsyn-kw">>
            syn-kw"><artifactIdsyn-kw">>slf4j-simplesyn-kw"></artifactIdsyn-kw">>
            syn-kw"><versionsyn-kw">>2.0.12syn-kw"></versionsyn-kw">>
        syn-kw"></dependencysyn-kw">>
    syn-kw"></dependenciessyn-kw">>
syn-kw"></projectsyn-kw">>

AtmWithdrawal.java

JAVA
package com.bank.streams;

import java.math.BigDecimal;

public class AtmWithdrawal {
    private String accountId;
    private String atmId;
    private BigDecimal amount;
    private String location;

    public AtmWithdrawal() {}

    public AtmWithdrawal(String accountId, String atmId,
                         BigDecimal amount, String location) {
        this.accountId = accountId;
        this.atmId = atmId;
        this.amount = amount;
        this.location = location;
    }

    public String getAccountId() { return accountId; }
    public String getAtmId() { return atmId; }
    public BigDecimal getAmount() { return amount; }
    public String getLocation() { return location; }

    @Override
    public String toString() {
        return accountId + "|" + atmId + "|" + amount + "|" + location;
    }

    public static AtmWithdrawal fromString(String value) {
        String[] parts = value.split("\\|");
        return new AtmWithdrawal(
            parts[0], parts[1], new BigDecimal(parts[2]), parts[3]);
    }
}

FraudDetectionTopology.java

JAVA
package com.bank.streams;

import org.apache.kafka.streams.StreamsBuilder;
import org.apache.kafka.streams.kstream.KStream;
import org.apache.kafka.streams.kstream.KTable;
import org.apache.kafka.streams.kstream.TimeWindows;
import org.apache.kafka.streams.kstream.Windowed;
import org.apache.kafka.streams.kstream.Materialized;
import org.apache.kafka.streams.Topology;
import org.apache.kafka.common.serialization.Serdes;

import java.time.Duration;

public class FraudDetectionTopology {

    private static final long ALERT_THRESHOLD = 3;

    public static Topology build() {
        StreamsBuilder builder = new StreamsBuilder();

        KStream<String, String> withdrawals = builder.stream("atm-withdrawals");

        // Count withdrawals per account in 5-minute tumbling windows
        KTable<Windowed<String>, Long> counts = withdrawals
            .groupByKey()
            .windowedBy(TimeWindows.ofSizeWithNoGrace(Duration.ofMinutes(5)))
            .count(Materialized.as("withdrawal-counts"));

        // Filter windows where count exceeds threshold
        counts
            .toStream()
            .filter((windowedKey, count) -> count >= ALERT_THRESHOLD)
            .mapValues((windowedKey, count) ->
                "FRAUD_ALERT|account=" + windowedKey.key()
                + "|count=" + count
                + "|window=" + windowedKey.window().startTime()
                + "-" + windowedKey.window().endTime())
            .selectKey((windowedKey, value) -> windowedKey.key())
            .to("fraud-alerts");

        return builder.build();
    }
}

FraudDetectionApp.java

JAVA
package com.bank.streams;

import org.apache.kafka.streams.KafkaStreams;
import org.apache.kafka.streams.StreamsConfig;
import org.apache.kafka.common.serialization.Serdes;

import java.util.Properties;
import java.util.concurrent.CountDownLatch;

public class FraudDetectionApp {
    public static void main(String[] args) {
        Properties props = new Properties();
        props.put(StreamsConfig.APPLICATION_ID_CONFIG, "fraud-detection-service");
        props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
        props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG,
                  Serdes.String().getClass());
        props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG,
                  Serdes.String().getClass());

        KafkaStreams streams = new KafkaStreams(
            FraudDetectionTopology.build(), props);
        CountDownLatch latch = new CountDownLatch(1);

        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            streams.close();
            latch.countDown();
        }));

        try {
            streams.start();
            System.out.println("Fraud detection service started");
            System.out.println("  Input topic:     atm-withdrawals");
            System.out.println("  Output topic:    fraud-alerts");
            System.out.println("  Window:          5-minute tumbling");
            System.out.println("  Alert threshold: 3+ withdrawals per window");
            latch.await();
        } catch (Exception e) {
            System.exit(1);
        }
    }
}

FraudDetectionTopologyTest.java

JAVA
package com.bank.streams;

import org.apache.kafka.streams.TopologyTestDriver;
import org.apache.kafka.streams.TestInputTopic;
import org.apache.kafka.streams.TestOutputTopic;
import org.apache.kafka.streams.StreamsConfig;
import org.apache.kafka.common.serialization.Serdes;
import org.apache.kafka.common.serialization.StringSerializer;
import org.apache.kafka.common.serialization.StringDeserializer;

import java.time.Instant;
import java.util.Properties;

public class FraudDetectionTopologyTest {
    public static void main(String[] args) {
        Properties props = new Properties();
        props.put(StreamsConfig.APPLICATION_ID_CONFIG, "test");
        props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "dummy:1234");
        props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG,
                  Serdes.String().getClass());
        props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG,
                  Serdes.String().getClass());

        try (TopologyTestDriver driver =
                 new TopologyTestDriver(FraudDetectionTopology.build(), props)) {

            TestInputTopic<String, String> input = driver.createInputTopic(
                "atm-withdrawals", new StringSerializer(), new StringSerializer());
            TestOutputTopic<String, String> output = driver.createOutputTopic(
                "fraud-alerts", new StringDeserializer(), new StringDeserializer());

            Instant base = Instant.parse("2024-01-15T10:00:00Z");

            // SAV2001: 4 withdrawals in 5 minutes (should trigger alert)
            input.pipeInput("SAV2001", "SAV2001|ATM001|10000|Mumbai", base);
            input.pipeInput("SAV2001", "SAV2001|ATM002|15000|Mumbai", base.plusSeconds(60));
            input.pipeInput("SAV2001", "SAV2001|ATM003|20000|Delhi", base.plusSeconds(120));
            input.pipeInput("SAV2001", "SAV2001|ATM004|5000|Chennai", base.plusSeconds(180));

            // CUR3001: 2 withdrawals in 5 minutes (should NOT trigger)
            input.pipeInput("CUR3001", "CUR3001|ATM001|50000|Mumbai", base);
            input.pipeInput("CUR3001", "CUR3001|ATM002|30000|Mumbai", base.plusSeconds(60));

            System.out.println("Fraud alerts:");
            while (!output.isEmpty()) {
                System.out.println("  " + output.readValue());
            }
            System.out.println();
            System.out.println("SAV2001: 4 withdrawals -> alert triggered at count 3 and 4");
            System.out.println("CUR3001: 2 withdrawals -> no alert");
        }
    }
}
TEXT
Expected output:
Fraud alerts:
  FRAUD_ALERT|account=SAV2001|count=3|window=2024-01-15T10:00:00Z-2024-01-15T10:05:00Z
  FRAUD_ALERT|account=SAV2001|count=4|window=2024-01-15T10:00:00Z-2024-01-15T10:05:00Z

SAV2001: 4 withdrawals -> alert triggered at count 3 and 4
CUR3001: 2 withdrawals -> no alert

How to run the project

BASH
# Step 1: Start Kafka
docker-compose up -d kafka zookeeper

# Step 2: Create topics
kafka-topics.sh --create --topic atm-withdrawals --partitions 3 --replication-factor 1 --bootstrap-server localhost:9092
kafka-topics.sh --create --topic fraud-alerts --partitions 3 --replication-factor 1 --bootstrap-server localhost:9092

# Step 3: Build
cd fraud-detection-service
mvn clean package

# Step 4: Run
java -cp target/fraud-detection-service-1.0.0.jar com.bank.streams.FraudDetectionApp

# Step 5: Produce test withdrawals (key:value format)
kafka-console-producer.sh --topic atm-withdrawals --property "parse.key=true" --property "key.separator=:" --bootstrap-server localhost:9092
# SAV2001:SAV2001|ATM001|10000|Mumbai
# SAV2001:SAV2001|ATM002|15000|Mumbai
# SAV2001:SAV2001|ATM003|20000|Delhi

# Step 6: Consume alerts
kafka-console-consumer.sh --topic fraud-alerts --from-beginning --bootstrap-server localhost:9092

What are common mistakes with windowing?

The most common mistake is using the wrong window type for the use case.

Wrong: tumbling window for moving average

JAVA
package com.bank.streams;

public class WrongWindowType {
    public static void main(String[] args) {
        System.out.println("WRONG: Using tumbling window for a moving average");
        System.out.println();
        System.out.println("  Tumbling window [10:00, 10:05):");
        System.out.println("    Transactions: 3 (count resets at boundary)");
        System.out.println("  Tumbling window [10:05, 10:10):");
        System.out.println("    Transactions: 2 (fresh count)");
        System.out.println();
        System.out.println("Problem: a burst of 4 transactions spanning 10:04 to 10:06");
        System.out.println("  is split across two windows and never triggers the alert.");
        System.out.println("  Window 1 sees 2, Window 2 sees 2. Neither reaches threshold 3.");
    }
}
TEXT
Expected output:
WRONG: Using tumbling window for a moving average

  Tumbling window [10:00, 10:05):
    Transactions: 3 (count resets at boundary)
  Tumbling window [10:05, 10:10):
    Transactions: 2 (fresh count)

Problem: a burst of 4 transactions spanning 10:04 to 10:06
  is split across two windows and never triggers the alert.
  Window 1 sees 2, Window 2 sees 2. Neither reaches threshold 3.

Correct: hopping or sliding window for moving average

JAVA
package com.bank.streams;

public class CorrectWindowType {
    public static void main(String[] args) {
        System.out.println("CORRECT: Using hopping window for a moving average");
        System.out.println();
        System.out.println("  Hopping window size=5min, hop=1min:");
        System.out.println("  Window [10:03, 10:08): sees all 4 transactions");
        System.out.println("  Window [10:04, 10:09): sees all 4 transactions");
        System.out.println();
        System.out.println("The overlapping windows catch the burst that spans");
        System.out.println("a tumbling window boundary.");
        System.out.println();
        System.out.println("Alternative: SlidingWindows for exact per-record windows.");
    }
}
TEXT
Expected output:
CORRECT: Using hopping window for a moving average

  Hopping window size=5min, hop=1min:
  Window [10:03, 10:08): sees all 4 transactions
  Window [10:04, 10:09): sees all 4 transactions

The overlapping windows catch the burst that spans
a tumbling window boundary.

Alternative: SlidingWindows for exact per-record windows.

What should a senior or principal engineer know?

At the senior level, window type selection affects state store size, throughput, and alert accuracy.

  • Tumbling windows create the least state: each record belongs to one window. State per partition = events per window.
  • Hopping windows create more state: each record belongs to (window size / hop) windows. A 1-hour window with a 1-minute hop means each record is in 60 windows.
  • Session windows have unpredictable state: a long session keeps merging, and state grows until the session closes. Set a maximum session duration or alert on state store size.
  • Suppress: by default, every update to a windowed aggregation emits a new result. Use Suppressed.untilWindowCloses() to emit only the final result after the window closes. This reduces downstream traffic but adds latency equal to the grace period.
  • Wall-clock time vs event time: Kafka Streams uses event time (record timestamp) for windowing by default. If producers have clock skew, records may land in unexpected windows. Use a custom TimestampExtractor to control this.
JAVA
package com.bank.streams;

public class SeniorWindowingKnowledge {
    public static void main(String[] args) {
        System.out.println("Senior/Principal windowing checklist:");
        System.out.println("  1. Tumbling = 1 window per record, least state");
        System.out.println("  2. Hopping = (size/hop) windows per record, more state");
        System.out.println("  3. Session = variable state, monitor with JMX");
        System.out.println("  4. Use Suppressed.untilWindowCloses() for final-only output");
        System.out.println("  5. Default uses event time, not wall-clock time");
        System.out.println("  6. Clock skew in producers affects window assignment");
        System.out.println("  7. Window retention must be >= window size + grace period");
        System.out.println("  8. Large windows = large state stores = slow recovery");
    }
}
TEXT
Expected output:
Senior/Principal windowing checklist:
  1. Tumbling = 1 window per record, least state
  2. Hopping = (size/hop) windows per record, more state
  3. Session = variable state, monitor with JMX
  4. Use Suppressed.untilWindowCloses() for final-only output
  5. Default uses event time, not wall-clock time
  6. Clock skew in producers affects window assignment
  7. Window retention must be >= window size + grace period
  8. Large windows = large state stores = slow recovery

What are common interview follow-up questions?

  • What is the default window retention? (By default, Kafka Streams retains windowed state for window size + 24 hours. You can change it with Materialized.withRetention().)
  • Can you query a specific window from a state store? (Yes. Use WindowStore.fetch(key, fromTime, toTime) to get results for a specific key and time range.)
  • What happens when a record arrives after the window closes? (If the grace period has expired, the record is dropped. If grace period is still active, it is included in the window.)
  • How do you handle out-of-order records? (Set a grace period with TimeWindows.ofSizeAndGrace(). Records arriving within the grace period after the window closes are still processed.)
  • Can you combine two window types? (Not directly in one aggregation. You would chain two separate windowed operations.)

Visual Overview

DIAGRAM
 Tumbling Window (5 min)
 ========================

 Time:  10:00    10:05    10:10    10:15
        │        │        │        │
        ├────────┤        │        │
        │Window 1│        │        │
        ├────────┼────────┤        │
                 │Window 2│        │
                 ├────────┼────────┤
                          │Window 3│
                          ├────────┤
   No overlap. Each record in exactly 1 window.


 Hopping Window (size=10min, hop=5min)
 ======================================

 Time:  10:00    10:05    10:10    10:15
        │        │        │        │
        ├────────────────┤        │
        │    Window 1     │        │
        │        ├────────────────┤│
        │        │    Window 2    ││
        │        │        ├───────┼────────┤
        │        │        │  Window 3      │
   Windows overlap. Records can be in multiple windows.


 Session Window (gap=5min)
 ==========================

 Records: R1   R2   R3        R4   R5
          │    │    │         │    │
  ────────┼────┼────┼─────────┼────┼──── time
          │<2m>│<2m>│  <8min> │<2m>│
          │              │         │
          └──Session 1───┘    └─Session 2─┘
   Gap > 5 min closes session. New activity starts new session.

Key Takeaway

Windowing in Kafka Streams groups records into time-based buckets for aggregation. There are four window types, each suited to a different use case. Tumbling windows are fixed-size and non-overlapping, ideal for periodic reports like hourly transaction counts. Hopping windows overlap, catching activity bursts that span tumbling window boundaries. Sliding windows evaluate per record and are used mainly in joins. Session windows group records by activity and close after an inactivity gap.

In our fraud detection project at National Bank of India, we used 5-minute tumbling windows to count ATM withdrawals per account. When the count reached 3 within a single window, a fraud alert was written to the output topic. The TopologyTestDriver confirmed that 4 withdrawals from SAV2001 triggered alerts at counts 3 and 4, while 2 withdrawals from CUR3001 did not.

The most common mistake is choosing the wrong window type. Tumbling windows miss activity bursts that span window boundaries. If you need to detect "5 transactions in any 10-minute span," use hopping or sliding windows instead. Each window type creates state stores, and the state size grows with window duration and overlap frequency.

At the senior level, understand that hopping windows multiply state by (size/hop), session windows have unpredictable state growth, and the Suppressed API controls when results are emitted downstream. Use event time for windowing and set a grace period for late-arriving records.

Remember one sentence: tumbling windows count per fixed bucket, hopping windows give overlapping counts, sliding windows move with each record, and session windows close after silence.

---

Share
Use ← → arrow keys to navigate