terça-feira, dezembro 5, 2023

Implement Apache Flink real-time information enrichment patterns

Stream information processing permits you to act on information in actual time. Actual-time information analytics may help you may have on-time and optimized responses whereas enhancing the general buyer expertise.

Knowledge streaming workloads usually require information within the stream to be enriched through exterior sources (comparable to databases or different information streams). Pre-loading of reference information supplies low latency and excessive throughput. Nonetheless, this sample is probably not appropriate for sure varieties of workloads:

  • Reference information updates with excessive frequency
  • The streaming software must make an exterior name to compute the enterprise logic
  • Accuracy of the output is necessary and the applying shouldn’t use stale information
  • Cardinality of reference information may be very excessive, and the reference dataset is just too large to be held within the state of the streaming software

For instance, when you’re receiving temperature information from a sensor community and must get further metadata of the sensors to research how these sensors map to bodily geographic areas, you’ll want to enrich it with sensor metadata information.

Apache Flink is a distributed computation framework that permits for stateful real-time information processing. It supplies a single set of APIs for constructing batch and streaming jobs, making it straightforward for builders to work with bounded and unbounded information. Amazon Managed Service for Apache Flink (successor to Amazon Kinesis Knowledge Analytics) is an AWS service that gives a serverless, absolutely managed infrastructure for working Apache Flink purposes. Builders can construct extremely accessible, fault tolerant, and scalable Apache Flink purposes with ease and without having to grow to be an skilled in constructing, configuring, and sustaining Apache Flink clusters on AWS.

You should utilize a number of approaches to counterpoint your real-time information in Amazon Managed Service for Apache Flink relying in your use case and Apache Flink abstraction stage. Every technique has totally different results on the throughput, community visitors, and CPU (or reminiscence) utilization. For a common overview of knowledge enrichment patterns, discuss with Frequent streaming information enrichment patterns in Amazon Managed Service for Apache Flink.

This publish covers how one can implement information enrichment for real-time streaming occasions with Apache Flink and how one can optimize efficiency. To check the efficiency of the enrichment patterns, we ran efficiency testing based mostly on artificial information. The results of this take a look at is beneficial as a common reference. It’s necessary to notice that the precise efficiency in your Flink workload will rely upon numerous and various factors, comparable to API latency, throughput, dimension of the occasion, and cache hit ratio.

We talk about three enrichment patterns, detailed within the following desk.

. Synchronous Enrichment Asynchronous Enrichment Synchronous Cached Enrichment
Enrichment strategy Synchronous, blocking per-record requests to the exterior endpoint Non-blocking parallel requests to the exterior endpoint, utilizing asynchronous I/O Incessantly accessed data is cached within the Flink software state, with a hard and fast TTL
Knowledge freshness All the time up-to-date enrichment information All the time up-to-date enrichment information Enrichment information could also be stale, as much as the TTL
Improvement complexity Easy mannequin More durable to debug, as a result of multi-threading More durable to debug, as a result of counting on Flink state
Error dealing with Simple Extra complicated, utilizing callbacks Simple
Influence on enrichment API Max: one request per message Max: one request per message Scale back I/O to enrichment API (depends upon cache TTL)
Utility latency Delicate to enrichment API latency Much less delicate to enrichment API latency Scale back software latency (depends upon cache hit ratio)
Different concerns none none

Customizable TTL.

Solely synchronous implementation as of Flink 1.17

Results of the comparative take a look at (Throughput) ~350 occasions per second ~2,000 occasions per second ~28,000 occasions per second

Resolution overview

For this publish, we use an instance of a temperature sensor community (element 1 within the following structure diagram) that emits sensor data, comparable to temperature, sensor ID, standing, and the timestamp this occasion was produced. These temperature occasions get ingested into Amazon Kinesis Knowledge Streams (2). Downstream techniques additionally require the model and nation code data of the sensors, with the intention to analyze, for instance, the reliability per model and temperature per plant facet.

Based mostly on the sensor ID, we enrich this sensor data from the Sensor Information API (3), which give us with data of the model, location, and a picture. The ensuing enriched stream is shipped to a different Kinesis information stream and may then be analyzed in an Amazon Managed Service for Apache Flink Studio pocket book (4).

Solution overview


To get began with implementing real-time information enrichment patterns, you possibly can clone or obtain the code from the GitHub repository. This repository implements the Flink streaming software we described. Yow will discover the directions on the best way to arrange Flink in both Amazon Managed Service for Apache Flink or different accessible Flink deployment choices within the README.md file.

If you wish to find out how these patterns are applied and the best way to optimize efficiency in your Flink software, you possibly can merely comply with together with this publish with out deploying the samples.

Undertaking overview

The undertaking is structured as follows:

docs/                               -- Accommodates undertaking documentation
├── principal/java/...                   -- Accommodates all of the Flink software code
│   ├── ProcessTemperatureStream    -- Major class that decides on the enrichment technique
│   ├── enrichment.                 -- Accommodates the totally different enrichment methods (sync, async and cached)
│   ├── occasion.                      -- Occasion POJOs
│   ├── serialize.                  -- Utils for serialization
│   └── utils.                      -- Utils for Parameter parsing
└── take a look at/                           -- Accommodates all of the Flink testing code

The principal technique within the ProcessTemperatureStream class units up the run atmosphere and both takes the parameters from the command line, if it’s is an area atmosphere, or makes use of the applying properties from Amazon Managed Service for Apache Flink. Based mostly on the parameter EnrichmentStrategy, it decides which implementation to choose: synchronous enrichment (default), asynchronous enrichment, or cached enrichment based mostly on the Flink idea of KeyedState.

public static void principal(String[] args) throws Exception {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
     ParameterTool parameter = ParameterToolUtils.getParameters(args, env);

    String technique = parameter.get("EnrichmentStrategy", "SYNC");
     swap (technique) ASYNC

We go over the three approaches within the following sections.

Synchronous information enrichment

If you wish to enrich your information from an exterior supplier, you should utilize synchronous per-record lookup. When your Flink software processes an incoming occasion, it makes an exterior HTTP name and after sending each request, it has to attend till it receives the response.

As Flink processes occasions synchronously, the thread that’s working the enrichment is blocked till it receives the HTTP response. This leads to the processor staying idle for a major interval of processing time. Then again, the synchronous mannequin is less complicated to design, debug, and hint. It additionally permits you to at all times have the newest information.

It may be built-in into your streaming software as such:

DataStream<EnrichedTemperature> enrichedTemperatureDataStream =
                .map(new SyncEnrichmentFunction(parameter.get("SensorApiUrl", DEFAULT_API_URL)));

The implementation of the enrichment operate appears to be like like the next code:

public class SyncEnrichmentFunction extends RichMapFunction<Temperature, EnrichedTemperature> {

    // Setup of HTTP consumer and ObjectMapper

    public EnrichedTemperature map(Temperature temperature) throws Exception {
        String url = this.getRequestUrl + temperature.getSensorId();

        // Retrieve response from sensor data API
        Response response = consumer

        // Parse the sensor data
        SensorInfo sensorInfo = parseSensorInfo(response.getResponseBody());

        // Merge the temperature sensor information and sensor data information
        return getEnrichedTemperature(temperature, sensorInfo);

    // ...

To optimize the efficiency for synchronous enrichment, you should utilize the KeepAlive flag as a result of the HTTP consumer can be reused for a number of occasions.

For purposes with I/O-bound operators (comparable to exterior information enrichment), it will possibly additionally make sense to extend the applying parallelism with out rising the assets devoted to the applying. You are able to do this by rising the ParallelismPerKPU setting of the Amazon Managed Service for Apache Flink software. This configuration describes the variety of parallel subtasks an software can carry out per Kinesis Processing Unit (KPU), and the next worth of ParallelismPerKPU can result in full utilization of KPU assets. However remember that rising the parallelism doesn’t work in all circumstances, comparable to if you find yourself consuming from sources with few shards or partitions.

In our artificial testing with Amazon Managed Service for Apache Flink, we noticed a throughput of roughly 350 occasions per second on a single KPU with 4 parallelism per KPU and the default settings.

Synchronous enrichment performance

Asynchronous information enrichment

Synchronous enrichment doesn’t take full benefit of computing assets. That’s as a result of Fink waits for HTTP responses. However Flink presents asynchronous I/O for exterior information entry. This lets you enrich the stream occasions asynchronously, so it will possibly ship a request for different parts within the stream whereas it waits for the response for the primary aspect and requests might be batched for higher effectivity.

Sync I/O vs Async I/O

Whereas utilizing this sample, it’s a must to resolve between unorderedWait (the place it emits the end result to the subsequent operator as quickly because the response is acquired, disregarding the order of the weather on the stream) and orderedWait (the place it waits till all inflight I/O operations full, then sends the outcomes to the subsequent operator in the identical order as the unique parts had been positioned on the stream). When your use case doesn’t require occasion ordering, unorderedWait supplies higher throughput and fewer idle time. Consult with Enrich your information stream asynchronously utilizing Amazon Managed Service for Apache Flink to be taught extra about this sample.

The asynchronous enrichment might be added as follows:

SingleOutputStreamOperator<EnrichedTemperature> asyncEnrichedTemperatureSingleOutputStream =
                        new AsyncEnrichmentFunction(parameter.get("SensorApiUrl", DEFAULT_API_URL)),

The enrichment operate works related because the synchronous implementation. It first retrieves the sensor data as a Java Future, which represents the results of an asynchronous computation. As quickly because it’s accessible, it parses the knowledge after which merges each objects into an EnrichedTemperature:

public class AsyncEnrichmentFunction extends RichAsyncFunction<Temperature, EnrichedTemperature> {

    // Setup of HTTP consumer and ObjectMapper

    public void asyncInvoke(remaining Temperature temperature, remaining ResultFuture<EnrichedTemperature> resultFuture) {
        String url = this.getRequestUrl + temperature.getSensorId();

        // Retrieve response from sensor data API
        Future<Response> future = consumer
                .supplyAsync(() -> {
                    strive {
                        Response response = future.get();

                        // Parse the sensor data as quickly as it's accessible
                        return parseSensorInfo(response.getResponseBody());
                    } catch (Exception e) {
                        return null;
                .thenAccept((SensorInfo sensorInfo) ->

                    // Merge the temperature sensor information and sensor data information
                    resultFuture.full(getEnrichedTemperature(temperature, sensorInfo)));

    // ...

In our testing with Amazon Managed Service for Apache Flink, we noticed a throughput of two,000 occasions per second on a single KPU with 2 parallelism per KPU and the default settings.

Async enrichment performance

Synchronous cached information enrichment

Though quite a few operations in an information movement give attention to particular person occasions independently, comparable to occasion parsing, there are specific operations that retain data throughout a number of occasions. These operations, comparable to window operators, are known as stateful as a result of their means to keep up state.

The keyed state is saved inside an embedded key-value retailer, conceptualized as part of Flink’s structure. This state is partitioned and distributed along with the streams which are consumed by the stateful operators. In consequence, entry to the key-value state is proscribed to keyed streams, that means it will possibly solely be accessed after a keyed or partitioned information change, and is restricted to the values related to the present occasion’s key. For extra details about the ideas, discuss with Stateful Stream Processing.

You should utilize the keyed state for incessantly accessed data that doesn’t change usually, such because the sensor data. This won’t solely let you scale back the load on downstream assets, but in addition enhance the effectivity of your information enrichment as a result of no round-trip to an exterior useful resource for already fetched keys is critical and there’s additionally no must recompute the knowledge. However remember that Amazon Managed Service for Apache Flink shops transient information in a RocksDB backend, which provides a latency to retrieving the knowledge. However as a result of RocksDB is native to the node processing the information, that is quicker than reaching out to exterior assets, as you possibly can see within the following instance.

To make use of keyed streams, it’s a must to partition your stream utilizing the .keyBy(...) technique, which assures that occasions for a similar key, on this case sensor ID, can be routed to the identical employee. You may implement it as follows:

SingleOutputStreamOperator<EnrichedTemperature> cachedEnrichedTemperatureSingleOutputStream = temperatureDataStream
        .course of(new CachedEnrichmentFunction(
                parameter.get("SensorApiUrl", DEFAULT_API_URL),
                parameter.get("CachedItemsTTL", String.valueOf(CACHED_ITEMS_TTL))));

We’re utilizing the sensor ID as the important thing to partition the stream and later enrich it. This fashion, we are able to then cache the sensor data as a part of the keyed state. When choosing a partition key in your use case, select one which has a excessive cardinality. This results in a good distribution of occasions throughout totally different employees.

To retailer the sensor data, we use the ValueState. To configure the state administration, we’ve to explain the state sort through the use of the TypeHint. Moreover, we are able to configure how lengthy a sure state can be cached by specifying the time-to-live (TTL) earlier than the state can be cleaned up and has to retrieved or recomputed once more.

public class CachedEnrichmentFunction extends KeyedProcessFunction<String, Temperature, EnrichedTemperature> {

    // Setup of HTTP consumer and ObjectMapper...

    personal transient ValueState<SensorInfo> cachedSensorInfoLight;
    public void open(Configuration configuration) throws Exception {
        // Initialize HTTP consumer
        ValueStateDescriptor<SensorInfo> descriptor =
                new ValueStateDescriptor<>("sensorInfo", TypeInformation.of(new TypeHint<>()}));
        StateTtlConfig ttlConfig = StateTtlConfig
        cachedSensorInfoLight = getRuntimeContext().getState(descriptor);
    // ...

As of Flink 1.17, entry to the state will not be attainable in asynchronous capabilities, so the implementation should be synchronous.

It first checks if the sensor data for this explicit key exists; if that’s the case, it will get enriched. In any other case, it retrieves the sensor data, parses it, after which merges each objects into an EnrichedTemperature:

public class CachedEnrichmentFunction extends KeyedProcessFunction<String, Temperature, EnrichedTemperature> {

    // Setup of HTTP consumer, ObjectMapper and ValueState

    public void processElement(Temperature temperature, KeyedProcessFunction<String, Temperature, EnrichedTemperature>.Context ctx, Collector<EnrichedTemperature> out) throws Exception {
        SensorInfo sensorInfoCachedEntry = cachedSensorInfoLight.worth();

        // Test if sensor data is cached
        if (sensorInfoCachedEntry != null) {
            out.accumulate(getEnrichedTemperature(temperature, sensorInfoCachedEntry));
        } else {
            String url = this.getRequestUrl + temperature.getSensorId();

            // Retrieve response from sensor data API
            Response response = consumer

            // Parse the sensor data
            SensorInfo sensorInfo = parseSensorInfo(response.getResponseBody());

            // Cache the sensor data

            // Merge the temperature sensor information and sensor data information
            out.accumulate(getEnrichedTemperature(temperature, sensorInfo));

    // ...

In our artificial testing with Amazon Managed Service for Apache Flink, we noticed a throughput of 28,000 occasions per second on a single KPU with 4 parallelism per KPU and the default settings.

Sync+Cached enrichment performance

You too can see the impression and lowered load on the downstream sensor API.

Impact on Enrichment API

Check your workload on Amazon Managed Service for Apache Flink

This publish in contrast totally different approaches to run an software on Amazon Managed Service for Apache Flink with 1 KPU. Testing with a single KPU offers a great efficiency baseline that permits you to examine the enrichment patterns with out producing a full-scale manufacturing workload.

It’s necessary to know that the precise efficiency of the enrichment patterns depends upon the precise workload and different exterior techniques the Flink software interacts with. For instance, efficiency of cached enrichment might differ with the cache hit ratio. Synchronous enrichment might behave in another way relying on the response latency of the enrichment endpoint.

To guage which strategy most closely fits your workload, it is best to first carry out scaled-down exams with 1 KPU and a restricted throughput of practical information, probably experimenting with totally different values of Parallelism per KPU. After you determine the very best strategy, it’s necessary to check the implementation at full scale, with actual information and integrating with actual exterior techniques, earlier than shifting to manufacturing.


This publish explored totally different approaches to implement real-time information enrichment utilizing Flink, specializing in three communication patterns: synchronous enrichment, asynchronous enrichment, and caching with Flink KeyedState.

We in contrast the throughput achieved by every strategy, with caching utilizing Flink KeyedState being as much as 14 occasions quicker than utilizing asynchronous I/O, on this explicit experiment with artificial information. Moreover, we delved into optimizing the efficiency of Apache Flink, particularly on Amazon Managed Service for Apache Flink. We mentioned methods and greatest practices to maximise the efficiency of Flink purposes in a managed atmosphere, enabling you to totally make the most of the capabilities of Flink in your real-time information enrichment wants.

Total, this overview presents insights into totally different information enrichment patterns, their efficiency traits, and optimization strategies when utilizing Apache Flink, significantly within the context of real-time information enrichment eventualities and on Amazon Managed Service for Apache Flink.

We welcome your suggestions. Please go away your ideas and questions within the feedback part.

Concerning the authors

Luis MoralesLuis Morales works as Senior Options Architect with digital-native companies to assist them in continuously reinventing themselves within the cloud. He’s keen about software program engineering, cloud-native distributed techniques, test-driven growth, and all issues code and safety.

Lorenzo NicoraLorenzo Nicora works as Senior Streaming Resolution Architect serving to clients throughout EMEA. He has been constructing cloud-native, data-intensive techniques for a number of years, working within the finance business each by means of consultancies and for fin-tech product corporations. He leveraged open supply applied sciences extensively and contributed to a number of initiatives, together with Apache Flink.

Related Articles


Please enter your comment!
Please enter your name here

Latest Articles