12.6 C
Saturday, November 18, 2023

Implement Apache Flink near-online information enrichment patterns

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

Information streaming workloads usually require information within the stream to be enriched by way of exterior sources (comparable to databases or different information streams). Pre-loading of reference information gives low latency and excessive throughput. Nevertheless, this sample is probably not appropriate for sure sorts of workloads:

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

For instance, should you’re receiving temperature information from a sensor community and have to get extra metadata of the sensors to investigate 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 gives 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 Information Analytics) is an AWS service that gives a serverless, absolutely managed infrastructure for working Apache Flink purposes. Builders can construct extremely out there, fault tolerant, and scalable Apache Flink purposes with ease and without having to develop into an knowledgeable in constructing, configuring, and sustaining Apache Flink clusters on AWS.

You should use 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 normal overview of information enrichment patterns, seek advice from Widespread streaming information enrichment patterns in Amazon Managed Service for Apache Flink.

This submit covers how one can implement information enrichment for near-online streaming occasions with Apache Flink and how one can optimize efficiency. To match the efficiency of the enrichment patterns, we ran efficiency testing primarily based on artificial information. The results of this take a look at is beneficial as a normal reference. It’s essential to notice that the precise efficiency to your Flink workload will rely upon numerous and various factors, comparable to API latency, throughput, measurement 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 Often accessed info is cached within the Flink utility state, with a hard and fast TTL
Information 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
Growth complexity Easy mannequin More durable to debug, because of multi-threading More durable to debug, because of counting on Flink state
Error dealing with Simple Extra advanced, utilizing callbacks Simple
Impression on enrichment API Max: one request per message Max: one request per message Cut back I/O to enrichment API (depends upon cache TTL)
Utility latency Delicate to enrichment API latency Much less delicate to enrichment API latency Cut back utility latency (depends upon cache hit ratio)
Different issues 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 submit, we use an instance of a temperature sensor community (element 1 within the following structure diagram) that emits sensor info, comparable to temperature, sensor ID, standing, and the timestamp this occasion was produced. These temperature occasions get ingested into Amazon Kinesis Information Streams (2). Downstream methods additionally require the model and nation code info of the sensors, as a way to analyze, for instance, the reliability per model and temperature per plant aspect.

Primarily based on the sensor ID, we enrich this sensor info from the Sensor Data API (3), which offer us with info of the model, location, and a picture. The ensuing enriched stream is distributed 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 near-online information enrichment patterns, you possibly can clone or obtain the code from the GitHub repository. This repository implements the Flink streaming utility we described. You’ll find the directions on the right way to arrange Flink in both Amazon Managed Service for Apache Flink or different out there Flink deployment choices within the README.md file.

If you wish to learn the way these patterns are applied and the right way to optimize efficiency to your Flink utility, you possibly can merely comply with together with this submit with out deploying the samples.

Venture overview

The venture is structured as follows:

docs/                               -- Comprises venture documentation
├── essential/java/...                   -- Comprises all of the Flink utility code
│   ├── ProcessTemperatureStream    -- Important class that decides on the enrichment technique
│   ├── enrichment.                 -- Comprises 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/                           -- Comprises all of the Flink testing code

The essential technique within the ProcessTemperatureStream class units up the run surroundings and both takes the parameters from the command line, if it’s is a neighborhood surroundings, or makes use of the appliance properties from Amazon Managed Service for Apache Flink. Primarily based on the parameter EnrichmentStrategy, it decides which implementation to choose: synchronous enrichment (default), asynchronous enrichment, or cached enrichment primarily based on the Flink idea of KeyedState.

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

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

We go over the three approaches within the following sections.

Synchronous information enrichment

Whenever you need to enrich your information from an exterior supplier, you should use synchronous per-record lookup. When your Flink utility 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 ends in the processor staying idle for a major interval of processing time. Then again, the synchronous mannequin is simpler 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 utility as such:

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

The implementation of the enrichment operate seems like the next code:

public class SyncEnrichmentFunction extends RichMapFunction<Temperature, EnrichedTemperature> {

    // Setup of HTTP shopper and ObjectMapper

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

        // Retrieve response from sensor information API
        Response response = shopper

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

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

    // ...

To optimize the efficiency for synchronous enrichment, you should use the KeepAlive flag as a result of the HTTP shopper will probably 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 appliance parallelism with out growing the assets devoted to the appliance. You are able to do this by growing the ParallelismPerKPU setting of the Amazon Managed Service for Apache Flink utility. This configuration describes the variety of parallel subtasks an utility can carry out per Kinesis Processing Unit (KPU), and a better worth of ParallelismPerKPU can result in full utilization of KPU assets. However take into account that growing the parallelism doesn’t work in all instances, 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 affords 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 may be batched for higher effectivity.

Sync I/O vs Async I/O

Whereas utilizing this sample, you must determine between unorderedWait (the place it emits the end result to the subsequent operator as quickly because the response is obtained, 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 gives higher throughput and fewer idle time. Check with Enrich your information stream asynchronously utilizing Amazon Managed Service for Apache Flink to be taught extra about this sample.

The asynchronous enrichment may 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 information as a Java Future, which represents the results of an asynchronous computation. As quickly because it’s out there, it parses the data after which merges each objects into an EnrichedTemperature:

public class AsyncEnrichmentFunction extends RichAsyncFunction<Temperature, EnrichedTemperature> {

    // Setup of HTTP shopper and ObjectMapper

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

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

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

                    // Merge the temperature sensor information and sensor information 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 a knowledge move give attention to particular person occasions independently, comparable to occasion parsing, there are specific operations that retain info throughout a number of occasions. These operations, comparable to window operators, are known as stateful because of their means to take care of 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 at the side of the streams which are consumed by the stateful operators. Consequently, entry to the key-value state is proscribed to keyed streams, which 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, seek advice from Stateful Stream Processing.

You should use the keyed state for incessantly accessed info that doesn’t change usually, such because the sensor info. This is not going to solely can help you cut back the load on downstream assets, but additionally improve 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 have to recompute the data. However take into account that Amazon Managed Service for Apache Flink shops transient information in a RocksDB backend, which provides a latency to retrieving the data. However as a result of RocksDB is native to the node processing the info, that is quicker than reaching out to exterior assets, as you possibly can see within the following instance.

To make use of keyed streams, you must partition your stream utilizing the .keyBy(...) technique, which assures that occasions for a similar key, on this case sensor ID, will probably be routed to the identical employee. You’ll be able to 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 manner, we are able to then cache the sensor info as a part of the keyed state. When selecting a partition key to your use case, select one which has a excessive cardinality. This results in a fair distribution of occasions throughout totally different employees.

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

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

    // Setup of HTTP shopper and ObjectMapper...

    non-public transient ValueState<SensorInfo> cachedSensorInfoLight;
    public void open(Configuration configuration) throws Exception {
        // Initialize HTTP shopper
        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 isn’t doable in asynchronous capabilities, so the implementation have to be synchronous.

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

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

    // Setup of HTTP shopper, 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 information is cached
        if (sensorInfoCachedEntry != null) {
            out.accumulate(getEnrichedTemperature(temperature, sensorInfoCachedEntry));
        } else {
            String url = this.getRequestUrl + temperature.getSensorId();

            // Retrieve response from sensor information API
            Response response = shopper

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

            // Cache the sensor information

            // Merge the temperature sensor information and sensor information 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

It’s also possible to see the affect and decreased load on the downstream sensor API.

Impact on Enrichment API

Take a look at your workload on Amazon Managed Service for Apache Flink

This submit in contrast totally different approaches to run an utility on Amazon Managed Service for Apache Flink with 1 KPU. Testing with a single KPU provides a superb efficiency baseline that permits you to evaluate the enrichment patterns with out producing a full-scale manufacturing workload.

It’s essential to know that the precise efficiency of the enrichment patterns depends upon the precise workload and different exterior methods the Flink utility interacts with. For instance, efficiency of cached enrichment might range 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 accurately fits your workload, it is best to first carry out scaled-down checks with 1 KPU and a restricted throughput of real looking information, probably experimenting with totally different values of Parallelism per KPU. After you determine the perfect strategy, it’s essential to check the implementation at full scale, with actual information and integrating with actual exterior methods, earlier than shifting to manufacturing.


This submit explored totally different approaches to implement near-online 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 instances quicker than utilizing asynchronous I/O, on this specific 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 finest practices to maximise the efficiency of Flink purposes in a managed surroundings, enabling you to completely make the most of the capabilities of Flink to your near-online information enrichment wants.

General, this overview affords insights into totally different information enrichment patterns, their efficiency traits, and optimization strategies when utilizing Apache Flink, significantly within the context of near-online 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.

In regards to 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 obsessed with software program engineering, cloud-native distributed methods, test-driven growth, and all issues code and safety.

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

Latest news
Related news


Please enter your comment!
Please enter your name here