Building a Real-Time Streaming Layer with AWS Kinesis and Lambda
Building a robust data platform on AWS requires understanding both scheduled data processing and instant event-driven reactions. While batch pipelines handle heavy analytics, real-time streaming acts as the immediate smoke detector for critical events.
In this guide, we'll explore how to add a real-time streaming layer to an existing telematics platform using Amazon Kinesis Data Streams and AWS Lambda.
Batch vs. Stream Processing
Understanding the difference between batch and streaming is crucial. They are not competing approaches but complementary rhythms in a complete architecture.
- Batch Processing: This is pull-based and runs on a schedule. It excels at processing large volumes of data comprehensively, providing authoritative reports and deep analysis. It answers the question, "What happened over the past day?"
- Stream Processing: This is push-based and event-driven. The system reacts instantly as records arrive without waiting for a schedule. It answers the question, "Is anything critical happening right now?"
A well-architected platform uses a batch layer, perhaps utilizing tools like AWS Glue and Athena, alongside Amazon S3 for data lakes, to serve as the source of truth, while the streaming layer handles immediate alerting and rapid response.
Real-Time Architecture Overview
Adding a real-time component involves connecting a data source directly to a stream and a consumer.
- Ping Source: Applications, APIs (like AWS API Gateway), or IoT devices send data.
- Kinesis Data Stream: Acts as a durable, ordered log for the incoming data stream.
- Event Source Mapping: An AWS-managed poller that continually reads the stream and groups records into batches.
- AWS Lambda: The consumer function triggered by the Event Source Mapping to analyze the batch and react (e.g., triggering alerts for speeding or harsh braking).
The Role of Event Source Mapping
The event source mapping is a key component in this serverless architecture. When you link a Kinesis stream to a Lambda function, AWS handles the complex task of continuously polling the stream. It gathers records and invokes your Lambda function based on predefined thresholds, such as batch size or a batching window. This means you do not need to manage dedicated polling infrastructure.
The Consumer Lambda Function
The Lambda function processes the streamed events. Because Kinesis delivers data as base64-encoded bytes, the first step is always decoding the payload into a usable format, like JSON.
import json
import base64
def handler(event, context):
alerts = 0
for record in event.get("Records", []):
# Decode the Kinesis data payload
payload = base64.b64decode(record["kinesis"]["data"]).decode('utf-8')
ping = json.loads(payload)
speed = float(ping.get("speed_kph", 0))
reasons = []
# Apply business rules
if speed > 100:
reasons.append(f"Speeding: {speed} kph")
if ping.get("harsh_event"):
reasons.append(f"Harsh Event: {ping['harsh_event']}")
if reasons:
alerts += 1
print(f"ALERT vehicle={ping.get('vehicle_id')} :: {', '.join(reasons)}")
return {"processed": len(event.get("Records", [])), "alerts": alerts}
This function demonstrates how quickly rules can be evaluated on incoming telematics data.
Deployment and Cost Considerations
When deploying real-time architecture, there are important operational differences compared to purely serverless batch setups.
Kinesis Costs
Unlike API Gateway or standard Lambda invocations which are strictly pay-per-use, a Kinesis Data Stream has an hourly cost per shard. The shard is always provisioned and waiting, which incurs charges regardless of data throughput. You must factor this into your architecture decisions.
Using Infrastructure as Code (IaC) tools like Terraform, you can manage this efficiently by placing the streaming components behind feature flags.
resource "aws_kinesis_stream" "telematics_stream" {
count = var.enable_streaming ? 1 : 0
name = "${var.project}-stream"
shard_count = 1
}
This allows you to easily tear down the infrastructure when not in use.
Cold Starts
When utilizing AWS Lambda, "cold starts" can introduce latency. A cold start occurs when AWS needs to initialize the execution environment before running your code. In streaming architectures where milliseconds matter, this initial delay (e.g., 80-100ms) can be significant. However, for many alerting use cases, this occasional latency is acceptable.
Conclusion
Combining a scheduled batch processing layer for deep analytics with a Kinesis and Lambda streaming layer for real-time alerting creates a complete, production-grade data platform. The streaming layer acts as the vital early warning system, reacting to critical events the moment they happen.