# Implementing Real-Time JOIN for Large Tables on Apache Flink: Basic Approach
We need to enable real-time updates for the INNER JOIN result between the users table (4 TB) and domains (2 TB). The query returns user_id, firstname, lastname, and domain_name. Traditional views overload the data source (OLTP), while simple materialization via CDC into DWH doesn't provide the required speed.
Example data:
users:
| id | firstname | lastname |
|----|-----------|----------|
| 1 | Egor | Myasnik |
| 2 | Pavel | Hvastun |
| 3 | Mitya | Volk |
domains:
| id | user_id | domain_name |
|----|---------|-------------|
| 1 | 1 | Approval |
| 2 | 1 | Rejection |
| 3 | 1 | Stoppage |
| 4 | 3 | Cancellation|
Analysis of Alternatives
- Direct view on OLTP — unacceptable due to load on the OLTP system.
- CDC → Kafka → DWH — unloads OLTP, but read speed from DWH is similar to the original. Adds two layers: Kafka and the view.
Both approaches fail to provide real-time updates for the JOIN result.
Flink Architecture
Solution: CDC streams into Kafka → Flink Table API → stateful JOIN → sink (console for MVP).
Maven Dependencies
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-connector-kafka</artifactId>
<version>3.2.0-1.19</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-sql-client</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-java</artifactId>
<version>${flink.version}</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-table-planner-loader</artifactId>
<version>${flink.version}</version>
</dependency>
Data Models
public class User implements Serializable {
public Integer id;
public String firstname;
public String lastname;
// getters/setters
public static User fromRow(Row row) { /* mapper */ }
}
public class Domain implements Serializable {
public Integer id;
public Integer user_id;
public String domain_name;
// getters/setters
public static Domain fromRow(Row row) { /* mapper */ }
}
Creating Kafka Tables
tableEnv.executeSql("CREATE TABLE users (" +
"`before` ROW<id: INT, firstname: STRING, lastname: STRING>," +
"`op` STRING," +
"`after` ROW<id: INT, firstname: STRING, lastname: STRING>," +
") WITH (" +
"'connector' = 'kafka'," +
"'topic' = 'users_topic'," +
"'properties.bootstrap.servers' = 'kafka-brokers'," +
"'properties.group.id' = 'users_consumer_group'," +
"'scan.startup.mode' = 'earliest'");
DataStream<User> users = tableEnv.toDataStream(tableEnv.from("users")).map(User::fromRow);
// Similarly for domains
tableEnv.executeSql("CREATE TABLE domains (...)");
DataStream<Domain> domains = tableEnv.toDataStream(tableEnv.from("domains")).map(Domain::fromRow);
Stateful JOIN Implementation
Joining streams by key user_id == domain.user_id using KeyedCoProcessFunction:
users
.connect(domains)
.keyBy(
user -> user.id,
domain -> domain.user_id
)
.process(new Join1())
.print();
KeyedCoProcessFunction
public class Join1 extends KeyedCoProcessFunction<Integer, User, Domain, Output> {
private MapState<Integer, Domain> domainsState;
private ValueState<User> usersState;
@Override
public void processElement1(User user, Context ctx, Collector<Output> out) throws Exception {
usersState.update(user);
for (Domain domain : domainsState.values()) {
out.collect(new Output(user.id, user.firstname, user.lastname, domain.domain_name));
}
}
@Override
public void processElement2(Domain domain, Context ctx, Collector<Output> out) throws Exception {
domainsState.put(domain.id, domain);
User user = usersState.value();
if (user != null) {
out.collect(new Output(user.id, user.firstname, user.lastname, domain.domain_name));
}
}
@Override
public void open(OpenContext openContext) throws Exception {
usersState = getRuntimeContext().getState(new ValueStateDescriptor<>("users", User.class));
domainsState = getRuntimeContext().getMapState(
new MapStateDescriptor<>("domains", Integer.class, Domain.class));
}
public static class Output implements Serializable {
public Integer user_id;
public String firstname, lastname, domain_name;
// constructors/getters/setters
}
}
MapState is used for domains due to the one-to-many relationship. State operations are O(1).
Key Points
- Real-time updates: JOIN result is up-to-date upon CDC event arrival.
- Stateful processing: ValueState for users (1:1), MapState for domains (1:N).
- Scalability: proven on terabyte-scale volumes with OLAP sink.
- Performance: O(1) state access, low latency.
- Next steps: handle DELETE/UPDATE, RocksDB, skew balancing.
Production Recommendations
- Use OLAP sink (ClickHouse, Pinot) instead of console.
- Process op flags from CDC for correct UPDATE/DELETE handling.
- Use RocksDB for state backend with large volumes.
- Monitor key skew and distribute partitions.
- Test scaling on a cluster.
— Editorial Team
No comments yet.