Skip to main content

Complex Event Processing: Pattern Detection at Scale

Module Duration: 6-7 hours Focus: Pattern API, sequences, quantifiers, conditions, fraud detection, system monitoring Prerequisites: Flink DataStream API, state management, event-time processing Hands-on Labs: 12+ CEP applications

Introduction: What is Complex Event Processing?

The Problem CEP Solves

Scenario: Detect credit card fraud by identifying patterns like:
“3 declining transactions from different merchants within 10 minutes, followed by 1 approved high-value transaction”
Traditional Approach (painful):
CEP Approach (elegant):

Real-World Use Cases


Part 1: CEP Fundamentals

The Pattern API

Pattern Building Blocks


Part 2: Pattern Basics - Individual Patterns

Simple Pattern

Chaining Patterns (next, followedBy, followedByAny)

1. Strict Contiguity (.next)

Only immediate successor, no events in between.

2. Relaxed Contiguity (.followedBy)

Allows other events in between, but maintains order.

3. Non-Deterministic Relaxed (.followedByAny)

Allows multiple matches with different intermediate events.

Part 3: Quantifiers - Specifying Occurrences

Fixed Count

Range

One or More (.oneOrMore)

Zero or More (.timesOrMore)

Greedy Quantifiers (.greedy)


Part 4: Conditions - Advanced Filtering

Simple Condition

Iterative Condition (Access Previous Events)

Subtype Condition

Combining Conditions (.or, .where + .or)

Until Condition (.until)


Part 5: Time Constraints

Within (Time Window)

After Match Skip Strategy

Skip Strategies:

Part 6: Real-World Patterns

Pattern 1: Fraud Detection (Velocity Check)

Pattern 2: Login Intrusion Detection

Pattern 3: E-Commerce Funnel Analysis

Pattern 4: IoT Equipment Failure Prediction


Part 7: Advanced CEP Techniques

Pattern Groups (Combining Multiple Patterns)

Timeout Handling (Partial Matches)

Flatselect (Multiple Outputs per Match)


Part 8: Production Patterns

Pattern 1: Multi-Stage Fraud Detection

Pattern 2: System Health Monitoring


Part 9: CEP vs Manual State Management

Manual Approach (Complex, Error-Prone)

CEP Approach (Declarative, Maintainable)


Part 10: Performance Optimization

State Size Management

Avoid Overly Greedy Patterns

Use Consecutive When Possible


Part 11: Testing CEP Patterns


Part 12: Interview Questions

Conceptual

Q1: What is Complex Event Processing and when should you use it? A: CEP is pattern detection over event streams. Use it when you need to detect sequences or combinations of events (fraud patterns, user behavior, system anomalies) rather than processing events individually. Q2: Explain the difference between .next(), .followedBy(), and .followedByAny(). A:
  • .next(): Strict contiguity (no events in between)
  • .followedBy(): Relaxed contiguity (other events allowed, single match)
  • .followedByAny(): Non-deterministic relaxed (multiple matches possible)
Q3: Why is the .within() clause important? A: Without .within(), state grows unbounded as Flink must keep all events indefinitely to potentially match the pattern. .within() bounds state by discarding events older than the time window. Q4: What’s the difference between greedy and non-greedy quantifiers? A:
  • Greedy (.oneOrMore().greedy()): Matches as many events as possible
  • Non-greedy (.oneOrMore()): Stops at first valid match

Coding

Q: Implement a pattern to detect 3 consecutive failed logins within 5 minutes.

Part 13: Hands-On Lab - Complete Fraud Detection System


Summary

What You’ve Mastered

✅ CEP fundamentals and pattern API ✅ Pattern building blocks (conditions, quantifiers, contiguity) ✅ Time constraints and skip strategies ✅ Real-world fraud detection patterns ✅ Advanced techniques (groups, timeouts, flatselect) ✅ Performance optimization ✅ Production deployment patterns

Key Takeaways

  1. CEP Simplifies Complex Logic: What takes 200 lines manually is 10 lines with CEP
  2. Always Use .within(): Unbounded state is dangerous in production
  3. Choose Contiguity Wisely: .next() vs .followedBy() vs .followedByAny()
  4. Test Patterns Thoroughly: Use unit tests with synthetic data
  5. Monitor State Size: CEP patterns can consume significant state

Next Module

Module 8: Flink Operations & Production Deployment

Kubernetes deployment, HA, monitoring, and production best practices

Resources

Documentation

Papers

Practice: Build a comprehensive fraud detection system using 5+ CEP patterns. Test with synthetic transaction data. Deploy to Flink cluster and monitor pattern matching metrics!