Skip to Content
Course content

168: Kafka Streams DSL in Scala

Click on the "Edit" button in the top corner of the screen to edit your slide content.

I once worked with a developer who tried to implement a sliding window for a real-time fraud detection system using raw Kafka consumers and a separate Redis cluster for state management. He spent nearly two weeks fighting race conditions where the Redis state would drift from the Kafka offsets during a consumer rebalance. It was a nightmare of manual bookkeeping. When I showed him the Kafka Streams DSL, he was floored—not because the syntax was "magical," but because the DSL handles the state stores and the changelog topics under the hood. He replaced about 600 lines of brittle coordination code with about 20 lines of declarative transformations.

The Kafka Streams DSL is essentially a high-level abstraction over the Processor API. Instead of worrying about how to punch holes in your state store or how to handle punctuation, you describe what should happen to the data as it flows. In Scala, this feels very natural because the DSL mirrors the functional transformations you're already used to in the standard library.

Distinguishing KStreams from KTables

To use the DSL effectively, you have to stop thinking of Kafka as just a "queue" and start thinking about the duality between a stream and a table. A KStream is a record of everything that happened—it's an append-only log. A KTable, however, is a materialized view of the latest state for any given key. If you're tracking the current price of a stock, the stream is every single tick, but the table is just the current price.

In Scala, you'll often find yourself switching between these two. You might start with a KStream of raw events, group them by a key, and then aggregate them into a KTable. The magic happens when you join them. Joining a stream to a table allows you to "enrich" an event with the latest known state—like taking a "Purchase" event (KStream) and joining it with "User Profile" data (KTable) to get the user's current shipping address without making a blocking API call to a database.

Avoiding the Repartitioning Trap

One of the most common mistakes I see Scala devs make is using .map() when they should be using .mapValues(). It seems like a pedantic distinction, but it's a performance killer. When you use .map(), you're telling Kafka that you might have changed the key of the record. Because Kafka guarantees that all records with the same key go to the same partition, any change to the key forces Kafka to "repartition" the data—meaning it writes the data back to an internal Kafka topic and reads it again.

If you're only transforming the value, always use .mapValues(). This keeps the processing local to the current partition and avoids the overhead of network I/O. Here is how a typical pipeline for monitoring price spikes looks in Scala:

val builder = StreamsBuilder()

val priceUpdates: KStream[String, Double] = builder.stream("product-prices")

priceUpdates
  .filter((key, value) => value > 0) // Clean up bad data
  .mapValues(price => price * 1.10) // Apply a simulated tax/fee
  .groupByKey()
  .windowedBy(TimeWindows.of(Duration.ofMinutes(5)))
  .aggregate(
    () => 0.0, 
    (key, newValue, aggregate) => (aggregate + newValue) / 2,
    Materialized.as("price-averages")
  )
  .toStream()
  .to("price-alerts")

Managing State with Windowed Aggregations

The example above uses a windowed aggregation. In the DSL, windows allow you to group records that arrive within a specific timeframe. This is where the "state store" mentioned earlier comes into play. Kafka Streams creates a local RocksDB instance to keep track of the running average for that 5-minute window. If your instance crashes, it doesn't lose this data; it recreates the state by reading the "changelog" topic from Kafka.

I've found that the trickiest part of windowing is handling "late" data. By default, the DSL allows a certain amount of grace period. If a record arrives late due to network lag, Kafka Streams will actually go back and update the windowed aggregation. This "event-time" processing is why the DSL is so much more powerful than simply using a foreach loop on a consumer—it respects when the event actually happened, not just when it arrived at your server.




📋 Practical Task

Exercise: Building a Real-time Currency Volatility Monitor

Your goal is to create a Kafka Streams topology in Scala that identifies "volatile" currency pairs. You will be given a stream of exchange rates (e.g., "EURUSD" -> 1.08). A currency pair is considered volatile if its price fluctuates by more than 2% within a 1-minute sliding window.

Requirements:

  • Consume from a topic named exchange-rates.
  • Use a KStream to filter out any rates that are less than or equal to zero.
  • Group the stream by the currency pair key.
  • Use a windowedBy operation with a 1-minute window.
  • Implement an aggregate function that tracks both the minimum and maximum price seen within that window.
  • Convert the resulting KTable back into a KStream and filter for pairs where (max - min) / min > 0.02.
  • Sink the volatile pairs into a topic named currency-alerts.

Constraint: Ensure you use mapValues instead of map where applicable to avoid unnecessary re-partitioning of the stream.

Rating
0 0

There are no comments for now.

to be the first to leave a comment.