envoy icon indicating copy to clipboard operation
envoy copied to clipboard

Kafka protocol filter

Open mattklein123 opened this issue 8 years ago • 70 comments

It's looking like Lyft may be able to fund Kafka protocol support in Envoy sometime this year.

Community, can you please chime in on what you would like to see? I know this will be a very popular feature. Stats (as in the Mongo filter) are a no-brainer. What else? Eventually routing and load balancing for languages in which the Kafka client drivers are not as robust?

mattklein123 avatar Mar 20 '18 01:03 mattklein123

First, this is totally awesome. Second, as someone with Kafka experience but rather new to service meshes, I have two types of suggestions: I have some additional features that I'd want to use if I had a Kafka-Proxy and then I also have a very unbaked suggestion for a different way to look at Kafka/Mesh integration that I'd like to discuss.

Let's start with additional features (not all are my idea, folks from Confluent helped!):

  • You can use the proxy to validate events. Because Kafka is "content agnostic", misbehaving clients can write literally anything. A proxy can validate that the message is in Protobufs (or whatever), that it has mandatory headers, etc.
  • Rate limiting is useful.
  • Add headers that allow tracking lineage of events - this was one of the reasons headers were added to Kafka.
  • Message format on the server can't be bumped up until all the clients upgraded, which can delay introduction of new features for a long long time. A proxy can convert the format.
  • Count events for monitoring
  • Really cool if possible: Failover to a DR cluster. This is easy for producers and currently super tricky for consumers (because offsets). Not sure if a service-mesh is enough for that one.

Here's the other point-of-view: Kafka isn't just a service, "kinda like a database", Kafka is a way of sending messages from one service to another. Kinda like a transport layer, except async and persistent. I wonder if Envoy can integrate with Kafka even deeper and allow services to use Kafka to communicate with other services (instead of REST, gRPC, etc). And then you can "hide" transition from REST to Kafka communication in the same way Lyft used Envoy to move to gRPC. Not sure if this makes total sense, since async programming is different, but worth mulling over.

gwenshap avatar Mar 20 '18 03:03 gwenshap

This would be dope. A couple use cases to start are request logs and stats. You can also build a nice audit log by taking your request logs and enriching them with the users's info. This could also help people write their own Kafka filters adding features like upconverting old Kafka clients to newer protocol versions.

travisjeffery avatar Mar 20 '18 03:03 travisjeffery

Thanks @gwenshap those are all great ideas. Would love to discuss more. If Confluent is potentially interested in helping with this (even if just design) can you or someone else reach out to me? My email address is easy to find or you can DM me on Twitter to connect.

mattklein123 avatar Mar 20 '18 05:03 mattklein123

Other interesting ideas that come to mind:

  • Add Kafka support to the upcoming tap/dump feature so that we can dump to a Kafka stream.
  • Shadow requests to a Kafka stream instead of HTTP/gRPC shadow.

mattklein123 avatar Mar 20 '18 05:03 mattklein123

@mattklein123 Do you mind explaining the primary use case you had in mind? Would this be a "Front Envoy" that might be used for ingress into Kubernetes? Or would a side car proxy pretend to be all Kafka brokers to local clients?

By "Front Envoy", I mean something like slide 15 in your deck here.

theduderog avatar Mar 20 '18 05:03 theduderog

  • tracing/monitoring: who is writing to which topics? Create a graph of data flow from producers to topics to consumers. https://logallthethings.com/2017/05/17/visualizing-the-flow-of-data-as-it-moves-through-a-kafka-cluster/
  • add compression to apps which aren’t already using it
  • add/remove SSL for traffic headed to/from broker
  • fault injection. Trigger consumer rebalances, broker down, producer transaction failures
  • metrics: byte rate per client id. Byte rate per consumer group.
  • like @gwenshap said: validate that requests have certain attributes. Example: CreateTopic requests must have minimum replication factor. Like https://kafka.apache.org/0110/javadoc/org/apache/kafka/server/policy/CreateTopicPolicy.html but for all kafka API types.
  • automatic topic name conversion to/from a cluster. Like, an app would publish to topic foo, and it would actually go to topic application.foo. This would allow multi tenant clusters, but the application would think they have the whole namespace to themselves.
  • consumer lag monitoring for the entire datacenter
  • metrics about which apps are using which versions of the client libraries
  • +1 on failover for consumers to another datacenter. You can do offset->timestamp conversion on one datacenter, and then do timestamp->offset conversion on the failover datacenter.

wushujames avatar Mar 20 '18 07:03 wushujames

Awesome that you guys are looking into Kafka protocol support, that'd be an amazing feature to have! +1 to @gwenshap and @wushujames ideas, also:

  • dynamic routing (for multicluster setups) of producers and consumers. This, in conjunction with a control plane would be a killer-feature: cluster/topic drain and rebalancing operations made easy;
  • double-producing (e.g. if user wants to produce data to 2 or more clusters/topics at the same time);

mironovgh avatar Mar 20 '18 10:03 mironovgh

Here are some ideas I would find useful (some already mentioned)

  • Monitor consumer lag
  • Failover to another cluster/datacenter while maintaining log position (hard due to offset mismatch)
  • Mirroring topics to another cluster, or teeing publishes "exactly once" to maintain identical clusters
  • Automatic topic switching e.g. specify my_topic_* to consume my_topic_1 and switch to my_topic_2 when it becomes available, transparently to the consumer. This would be useful for data migrations without interrupting consumption. In other terms, the ability to hot swap topics unbeknownst to the consumer.
  • Filter data on the server before sending to the consumer.
  • Producer rate limiting

sdotz avatar Mar 20 '18 14:03 sdotz

Between @mattklein123 @gwenshap and @wushujames this is an awesome list of features.

As a general question, particularly for Matt: would you see any value in capturing some of the more generic features and turning them higher level abstraction for messaging support in the service mesh?

mbogoevici avatar Mar 20 '18 15:03 mbogoevici

Perhaps also look at some of what kafka-pixy does. I find the wrapping of Kafka's native protocol into with REST/gRPC to be pretty compelling. This better supports usage from FaaS and apps that don't necessarily have the ability to do a long-lived connection.

sdotz avatar Mar 20 '18 15:03 sdotz

I'd like to see Envoy's Zipkin traces reported to Zipkin using Zipkin's Kafka collector.

rmichela avatar Mar 25 '18 04:03 rmichela

Thanks everyone for the awesome suggestions that have been added to this issue. From Lyft's perspective, we are primarily interested in:

  • L7 protocol parsing for observability (stats, logging, and trace linking with HTTP RPCs)
  • Ratelimiting at both the connection and L7 message level

So I think this is where we will focus, probably starting in Q3. I will need to go through and do some basic SWAGing in terms of how much existing code in https://github.com/edenhill/librdkafka can be reused for the protocol parsing portion. We will also coordinate with folks at Confluent on this work as well. Please reach out if you are also interested in helping.

mattklein123 avatar Mar 28 '18 22:03 mattklein123

Are there any plans at this point for how to practically proxy the Kafka protocol to a pool of brokers? In general, clients connect to a seed node and send it a "metadata" request for the topic/partition they're interested in. The response to that includes a hostname and port, which clients then connect to directly. It means that in practice Kafka clients are (by design) very good at dis-intermediating proxies.

ebroder avatar Mar 29 '18 02:03 ebroder

@ebroder One way to do it will be to register the proxy address (probably localhost:port if we are using sidecar) as their advertised-listeners. And then they'll return this address to the clients. In the latest release, advertised hosts will be a dynamic property, so this may become even easier to manage.

gwenshap avatar Mar 29 '18 02:03 gwenshap

@gwenshap: Interesting. That would imply a Kafka cluster that would only work with the sidecars then, right?

wushujames avatar Mar 29 '18 02:03 wushujames

I didn't mean to imply that. That's why I said "one way". I know @travisjeffery and @theduderog have ideas about central proxies. Sidecars do seem to be Envoy's main mode of deployment.

gwenshap avatar Mar 29 '18 02:03 gwenshap

That does require that you have to allocate a sidecar port for every kafka broker you're running, right? It seems like the overhead/management costs there could potentially add up quickly

ebroder avatar Mar 29 '18 02:03 ebroder

I'm not sure? How expensive are ports? Kafka clusters with over 50 brokers are quite rare.

gwenshap avatar Mar 29 '18 02:03 gwenshap

@ebroder @wushujames @gwenshap TBH I really have not gotten into the details yet. If the Kafka protocol does not support a built-in method of proxying (we should discuss), I think there are a few options:

  • Pre-configure all broker addresses in Envoy and have the seed node return Envoy addresses. Pro: Conceptually simple, Con: Annoying to configure.
  • Use some type of IP tables interception to make sure all Kafka connections go through Envoy on the way to the brokers. Pro: Transparent. Con: Requires kernel/external scripts. Needs more investigation and thinking.
  • Have Envoy do active L7 proxying/mutation of the seed communication, and swap broker addresses with local Envoy, and then potentially remember which broker to send which messages to. Pro: No kernel magic, fully transparent. Con: Very complicated, involves handling parts of the client/broker handshake.

But again I haven't done any investigation. I was going to carve out some time to learn more about all of this in Q2 and potentially find some people who would like to help me learn more about it. :)

mattklein123 avatar Mar 29 '18 03:03 mattklein123

@mattklein123 @gwenshap @ebroder: Yeah, I had the same idea as Matt's option 3. Since the initial request to the brokers has to flow through the sidecar anyway, it can intercept and rewrite the response back to the client, and transform the request/responses as they flow between client/broker. Sounds expensive to me, but I know very little about envoy's performance.

wushujames avatar Mar 29 '18 17:03 wushujames