Hi all. I'm planning to use Flink to implement a c...
# pyflink
y
Hi all. I'm planning to use Flink to implement a counter system in my service; the basic idea is that different parts of the service may emit simple key-value counter metrics to a stream, a Flink job will sum them and try to store the cumulative running total of a key to a Dynamo table. In theory, it seems like I should be able to use Dynamo's incremental
ADD
operator for this purpose: the Flink job would take care of summing many events for a given key over a window and then incrementally write the sum to the existing total in DDB. However, this requires the use of
UpdateItem
, which is not supported by the DDB client's
BatchWriteItem
operation, which is currently the only supported request operation by the DDB connector for Java. (Sorry if I'm posting this question in the wrong channel, but I didn't see any more appropriate-sounding one... and this question/problem should translate to other clients too.) DDB supports
UpdateItem
(and
PutItem
and
DeleteItem
) for
TransactWriteItems
requests, which uses DDB transactions to atomically write updates across multiple tables/table entries. Looking through the connector code, it seems pretty straightforward to add support for an "update" operation by using DDB transactions -- these are more expensive operations (in terms of latency and consumed capacity units) so it might not make sense to make them the "default" request type, but it would enable functionality that doesn't currently exist in the connector. So my questions are: 1. Is there a specific reason this functionality was not added in the first place (i.e. "because it's a bad idea", "because it tested poorly", "because there's an easier way to do this that works fine")? 2. Is there an easier way to do this that works fine? Chatting with a co-worker who is also delving into Flink for a different project, he mentioned that it should work fine to explicitly read the existing value from the DDB table in the Flink job, add the delta, and write it back -- since the stream is keyed by the metric key, only one node should process/sum the records for that key, so there should be no race condition (is this true?). Even so, it seems like a decent amount of extra work (and extra network calls to read in the values) in the Flink job for what seemingly could just be handled by DDB itself if the
TransactWriteItem
API is added to the connector. Am I missing something here? Sorry for the wall of text and thanks!
βž• 1
In case anyone is interested, I decided to implement my own DDB sink based on the existing one. It batches
UpdateItem
requests using DDB transactions, which is nice because DDB transactions can handle up to 100 individual items. The Flink docs assert that as long as you setup checkpointing in Flink and your source is replayable and your sink is transactional or idempotent, then you get exactly-once semantics end-to-end. It was pretty straightforward to implement the custom DDB sink, since there are base classes that do most of the heavy lifting for you (see https://flink.apache.org/2022/03/16/the-generic-asynchronous-base-sink/), and the existing DDB sink code serves as an example implementation. The only slight wrinkle was that batches could contain entries for the same DDB key, which DDB doesn't allow, so I had to merge same-key entries before constructing the
TransactWriteItems
request. Based on what I've read and my understand of Flink so far, this should suffice; my approach may have to change once I start testing, so YMMV πŸ™‚
n
Hey thanks for the update, one question- did you implement the sink in Java, build the package as jar and then reference it in your pyflink code?
y
No, our project is purely in Java.
n
okay makes sense! thank you!
πŸ‘ 1