This document provides an overview of leveraging Kafka and Redis Streams together. It begins with crash courses on Kafka and Redis Streams. It then discusses marrying the two by using Redis Streams to store Kafka records, mapping Kafka offsets to Redis entry IDs, and creating adapters for Kafka producers and consumers to integrate with Redis Streams. Finally, it mentions that a demo is provided and links to the experimental GitHub repository containing the code.
2. PRESENTED BY
● Founder, RDBTools
● CTO @ HashedIn
● Long time Redis user
● Speak regularly at Redis Conferences
About Me
@srithedabbler sripathikrishnan
20. PRESENTED BY
Adapter for Kafka Producer
public Future<RecordMetadata> send(ProducerRecord<K, V> record)
xadd <topic>.<partition> * _key <key bytes> _value <value bytes>...
Convert this:
Into this:
21. PRESENTED BY
1 Kafka Partition = 1 Redis Stream
- Redis Stream has no concept of partitions.
- So to match Kafka’s API, we make 1 Kafka Partition == 1 Redis
Stream
- An extra dictionary per kafka topic stores metadata about the
Topic
22. PRESENTED BY
Primary Key
(topic, partition, offset)
Fields
timestamp
headers
key (bytes)
value (bytes)
Format of Records
Primary Key
(redis key, <timestamp>-<counter>)
Fields
dictionary of key=value pairs
23. PRESENTED BY
Mapping Offsets to Entry Ids
Kafka offset is a monotonically
increasing, sequential long
eg. 3292
Redis entry id is a combination
of two longs <timestamp>.<counter>
eg. 1506872463535.3
No lossless way to combine 2 longs into 1 -:(
24. PRESENTED BY
… but you can make some assumptions
1. Kafka offsets don’t have to be sequential
(they have to always increase, but gaps are fine)
2. Reduced precision timestamps are probably fine
(support for ~100 years instead of all eternity)
3. Support ~2M entries in the same stream per millisecond
With these assumptions, we can translate one to another without
loss of data!
25. PRESENTED BY
Alternatively, you can manually generate streams Ids
This will involve maintaining a counter in redis, and
using it to generate sequential ids.
26. PRESENTED BY
Adapter for Kafka Consumer
public ConsumerRecords<K, V> poll(final Duration timeout)
xread count 20 block <timeout> streams <topic1>.<partition1> …
<offset1>
Convert this:
Into this: