How to share variables & logging in Apache Flink?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Flink is a framework and distributed processing engine for stateful computations over unbounded and bounded data streams. Designed to run in all common cluster environments, Flink orchestrates compute, state, and memory processes. Central to its architecture, variables and logging are crucial for any Flink deployment to monitor and optimize applications effectively. In this discussion, we delve deep into methods of sharing variables and implementing logging within Apache Flink.
Sharing Variables in Apache Flink
Due to the distributed nature of Flink, sharing variables is not as straightforward as in single-threaded applications. Variable sharing in Flink mainly involves understanding two contexts: operator state and broadcast state.
Operator State
This is a state that is tied to one specific parallel instance of an operator. The state retains data across stream processing iterations for that specific instance, effectively allowing variable sharing only within the boundaries of that instance or its subtasks. Here’s an example:
Broadcast State
A broadcast state allows for sharing state across all parallel instances of an operation. It’s designed to deal with use cases where some data is necessary as a contextual prerequisite for processing each incoming data element. The usage is ideal when you want to share some configuration settings or specific parameters across all tasks.
Logging in Apache Flink
Logging is essential for debugging and monitoring Flink applications. Flink uses SLF4J as its logging facade, allowing the flexibility to use any logging framework that supports SLF4J, such as Log4j2, Logback, or the JDK Logger.
To configure logging in Flink, modify the log4j.properties or logback.xml file typically located in the conf directory of your Flink installation. Here’s an example of a log4j.properties setup:
This example sets the logger to capture INFO level logs and above, outputting to a file named flink.log.
Summary Table
| Feature | Description | Example Use-Cases |
| Operator State | State scoped to specific sub-tasks of an operator | Counting elements, aggregating user behavior |
| Broadcast State | State that is shared across all parallel instances of an operator | Sharing rule sets or configurations |
| Logging | Provides monitoring and debugging capabilities connected through the SLF4J logging facade | Tracking application flow, error handling |
Additional Considerations
- Initialization of State: Ensure state is initialized in the
open()method of Rich functions. - State Backends: Choose appropriate state backends (e.g., RocksDB, FsStateBackend) based on your application's performance and durability requirements.
- Logging Level: Adjust logging levels based on the deployment cycle—debug levels during development and lower levels in production.
By mastering shared variable handling and logging, developers can build robust, efficient, and maintainable Flink applications tailored for scalable streaming architectures.
Related reading
- How to show the last queries executed on MySQL?
- How to solve Error loading state AccessDenied Access Denied status code 403 when trying to use s3 for terraform backend?
- How to specify a cluster name for a kubernetes cluster
- How to SSH into a Kubernetes Node or Server
- How to SSH into a Kubernetes Node or Server
- How to start a stopped Docker container with a different command?
- How to stop all containers when one container stops with docker-compose?
- How to stop Firebase from logging status updates when app is launched

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.