Kafka Streams is Apache Kafka’s stateful stream processing library. It processes records key-by-key and materializes aggregation state into local RocksDB instances backed by Kafka changelog topics. UUID v7 keys benefit Kafka Streams in two ways: embedded timestamps enable event-time processing without a separate timestamp field, and the monotonic ordering reduces RocksDB compaction pressure. See UUID v7 in Kafka Event Streaming for the producer side.
UUID v7 as a custom TimestampExtractor
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.streams.processor.TimestampExtractor;
import java.util.UUID;
/**
* Extracts event time from a UUID v7 record key.
* Falls back to record metadata timestamp if key is not UUID v7.
*/
public class UuidV7TimestampExtractor implements TimestampExtractor {
@Override
public long extract(ConsumerRecord<Object, Object> record, long partitionTime) {
if (record.key() instanceof String key) {
try {
String hex = key.replace("-", "");
if (hex.charAt(12) == '7') { // version nibble check
return Long.parseUnsignedLong(hex.substring(0, 12), 16);
}
} catch (Exception ignored) {}
}
return partitionTime; // fallback
}
}
// Configure the topology to use UUID v7 timestamps
Properties props = new Properties();
props.put(StreamsConfig.DEFAULT_TIMESTAMP_EXTRACTOR_CLASS_CONFIG,
UuidV7TimestampExtractor.class);
The extractor reads the first 12 hex characters of the UUID v7 key as a 48-bit Unix millisecond timestamp — the same extraction used in UUID Epoch arithmetic.
Windowed aggregation with UUID v7 event time
import org.apache.kafka.streams.StreamsBuilder;
import org.apache.kafka.streams.kstream.*;
import java.time.Duration;
StreamsBuilder builder = new StreamsBuilder();
KStream<String, OrderEvent> orders = builder.stream("order-events");
// 5-minute tumbling window — uses UUID v7 timestamp via extractor
KTable<Windowed<String>, Long> orderCounts = orders
.groupByKey()
.windowedBy(TimeWindows.ofSizeWithNoGrace(Duration.ofMinutes(5)))
.count(Materialized.as("order-counts-store"));
// Emit windowed results — key includes window start/end timestamps
orderCounts.toStream()
.map((windowedKey, count) -> KeyValue.pair(
windowedKey.key(),
new WindowedCount(windowedKey.window().start(), count)
))
.to("order-count-results");
Interactive queries with UUID v7 time bounds
import org.apache.kafka.streams.StoreQueryParameters;
import org.apache.kafka.streams.state.*;
// Query state store for a time range
ReadOnlyWindowStore<String, Long> store = streams.store(
StoreQueryParameters.fromNameAndType(
"order-counts-store",
QueryableStoreTypes.windowStore()
)
);
// Build UUID v7 lower bound for "last hour"
long nowMs = System.currentTimeMillis();
long fromMs = nowMs - 3_600_000L;
long toMs = nowMs;
// Fetch all windowed counts for an order ID in the last hour
WindowStoreIterator<Long> results = store.fetch("order-123", fromMs, toMs);
while (results.hasNext()) {
KeyValue<Long, Long> entry = results.next();
System.out.printf("Window start: %d, count: %d%n", entry.key, entry.value);
}
results.close();
See UUID v7 in Event Sourcing for the event ID pattern that feeds these streams.
Exactly-once semantics and UUID v7 deduplication
With processing.guarantee = exactly_once_v2, Kafka Streams uses transactional producers. UUID v7 message IDs enable application-level deduplication as a safety net:
// In a downstream consumer — deduplicate using UUID v7 message ID
private final Set<String> processedIds = new BoundedSet<>(10_000);
@Override
public void process(Record<String, OrderEvent> record) {
String messageId = record.headers()
.lastHeader("message-id")
.value().toString();
if (processedIds.contains(messageId)) {
return; // already processed — UUID v7 dedup
}
processedIds.add(messageId);
// ... process event ...
}
See UUID as Idempotency Keys for the full deduplication pattern.
Further reading
- UUID v7 in Kafka Event Streaming
- UUID v7 in Event Sourcing
- UUID as Idempotency Keys
- UUID in Message Queues — RabbitMQ and SQS
- UUID Epoch — timestamp extraction
External references
Frequently asked questions
How does UUID v7 improve Kafka Streams windowed aggregations?
Kafka Streams windowed aggregations group records by key and time window. With UUID v7 keys, the timestamp is already embedded in the key — you can implement a custom TimestampExtractor that reads the UUID v7 millisecond prefix instead of relying on Kafka record metadata timestamps. This decouples event time from broker ingestion time, useful when producers are delayed or events arrive out of order. See UUID v7 in Kafka Event Streaming.
Can UUID v7 be used for Kafka Streams state store IDs?
State store names in Kafka Streams are arbitrary strings used as RocksDB directory names and changelog topic suffixes. A UUID v7 state store name like 018fbe3a-orders-agg is valid but unconventional — store names are typically human-readable constants. Use UUID v7 for record keys and message IDs instead. The state store name should be a stable constant across deployments so changelog topic names remain predictable.