@adamwarski, Kafka Summit Europe, May 2021
Delivering:
from Kafka to WebSockets
Problem statement
Send messages from a Kafka topic to WebSockets
service
service
service
Kafka WebSocket


node
Clustered,
multi-node
1 node?


Multiple nodes?
Adding more nodes
service
service
service
Kafka
WebSocket


node
How to
connect?
WebSocket


node
A Kafka topic has many partitions
service
service
service
WebSocket


node
How to
connect?
WebSocket


node
Partition 1
Partition 3
Partition 4
Partition 2
About
• 14 years of backend engineering

• mainly using Scala, but quite a lot of Kafka as well

• coder & CTO @ SoftwareMill

• custom software: distributed systems, Big&Fast Data, ML/AI, Blockchain,
Messaging, …

• Scala/Kafka/Cassandra consulting

• Blog: http://www.softwaremill.com/blog

• Twitter: https://twitter.com/adamwarski
#1: everybody reads everything
WebSocket


node
WebSocket


node
Partition 1
Partition 3
Partition 4
Partition 2
#2: intelligent router
service
service
service
1
9
2
4
3
8
6
WebSocket


node
Partition
based on client
id
WebSocket


node
Partition 1
Partition 3
Partition 4
Partition 2
Router
#2: intelligent router
service
service
service
1
9
2
4
3
8
6
WebSocket


node
Break
existing
connections
WebSocket


node
Partition 1
Partition 3
Partition 4
Partition 2
WebSocket


node
Router
#3: redistributors
WebSocket


node
Backchannel
- client
assignment
WebSocket


node
Partition 1
Partition 3
Partition 4
Partition 2
redistributor
redistributor
#3.1: redistributors backchannel w/ Kafka
WebSocket


node
report client
state
WebSocket


node
redistributor
redistributor
Kafka
build an in-
memory view
#3.2: redistributors backchannel w/ cache
WebSocket


node
report client
state
WebSocket


node
redistributor
redistributor
memcached /


Redis / …
query where
client is located
#3: redistributors
WebSocket


node
WebSocket


node
Partition 1
Partition 3
Partition 4
Partition 2
redistributor
redistributor
partition
reassignment
#3.A: forwarding using Kafka
WebSocket


node
WebSocket


node
Partition 1
Partition 3
Partition 4
Partition 2
redistributor
redistributor
WS partition 1
WS partition 2
custom
partition
assignment
#3.B: forwarding using HTTP
WebSocket


node
WebSocket


node
Partition 1
Partition 3
Partition 4
Partition 2
redistributor
redistributor
How is Kafka influencing our design?
• Static & limited partitions

• no partitions per client

• no partition per WS node (or hard to implement)

• for good reasons!
Summing up
• Three possible solutions:

• everybody reads everything

• intelligent router

• redistributor
service
service
service
WebSocket


node
How to
connect?
WebSocket


node
Summing up: redistributor
• Backchannel

• using Kafka

• using an in-memory cache

• Forwarding

• using Kafka

• using HTTP
service
service
service
WebSocket


node
How to
connect?
WebSocket


node
Links
• https://kafka.apache.org

• Real-time data pipelines and streaming ebook:

https://kafka.softwaremill.com

• Evaluating persistent, replicated message queues: 

https://softwaremill.com/mqperf/

• Twitter: https://twitter.com/adamwarski
Thank you!
@adamwarski

Delivering: from Kafka to WebSockets | Adam Warski, SoftwareMill