AWS
KSQL
MSK
Cloud Computing
Data Streaming

Running ksqlDB (KSQL) with AWS MSK

Codemia Knowledge Hub ·

Amazon MSK does not include KSQL, but you can run KSQL (now ksqlDB, part of the Confluent Platform) yourself and point it at the MSK brokers. MSK runs the Kafka cluster as a managed service, and ksqlDB adds a streaming SQL engine for processing the data in real time.

What ksqlDB is

ksqlDB is an open-source stream processing framework that uses SQL-like syntax to read, write and process streaming data in Kafka. It supports stream processing operations, aggregation, joins and windowing.

Its main features:

  1. Stream processing in SQL: complex processing logic is written in familiar SQL syntax, so developers with SQL skills can pick it up quickly.
  2. Event-time processing: streams are processed based on when events actually occurred, not only when they are processed.
  3. Interactive queries: you can run queries against streams and tables to get real-time results.
  4. Materialized views: ksqlDB creates materialized views that update continuously as new data arrives, so queries stay fast.

Connecting ksqlDB to an MSK cluster

You run Confluent's ksqlDB service yourself and configure it to talk to the Kafka cluster that MSK manages:

  1. Set up Amazon MSK: launch an MSK cluster, choosing the Apache Kafka version, cluster configuration and node size for your workload.
  2. Configure Kafka Connect: deploy Kafka Connect, which connects ksqlDB to other data sources and sinks.
  3. Deploy ksqlDB: run ksqlDB servers on their own or as part of the Confluent Platform, on EC2 instances or in containers on Amazon ECS or EKS.
  4. Connect ksqlDB to MSK: configure ksqlDB with the broker endpoints of the MSK cluster so it can consume and produce messages.

Example: aggregating a transaction stream

For a real-time analytics case with a high-throughput event stream, you can define a stream over a topic and aggregate it in hourly windows:

sql
1-- Create a Stream to consume data from a Kafka topic
2CREATE STREAM transactions (
3  transaction_id VARCHAR KEY,
4  amount DECIMAL(12, 2),
5  status VARCHAR,
6  timestamp VARCHAR
7) WITH (
8  KAFKA_TOPIC='transaction_topic',
9  VALUE_FORMAT='json',
10  TIMESTAMP='timestamp',
11  TIMESTAMP_FORMAT='yyyy-MM-dd HH:mm:ss'
12);
13
14-- Aggregate data to calculate the total amount of completed transactions
15CREATE TABLE total_sales AS
16  SELECT status, SUM(amount) AS total_sales
17  FROM transactions
18  WINDOW TUMBLING (SIZE 1 HOUR)
19  WHERE status='completed'
20  GROUP BY status;

Sizing and network latency

Give the ksqlDB servers enough memory and CPU, and configure the network carefully so they have low-latency access to the MSK brokers.

What each side contributes

FeatureDescriptionAWS MSK Integration
Real-time processingProcesses data as it arrives, in real time.Connects to MSK for real-time data streaming.
SQL-like syntaxEnables stream processing using familiar SQL-like queries.Use SQL skills without needing proprietary tech knowledge.
ScalabilityScales horizontally by adding more nodes.MSK manages the Kafka cluster scaling automatically.
Interactive QueriesQueries data streams interactively.MSK supports various consumer configurations for optimized query performance.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design