Composition — running multiple nodes in one process
Atlasbot · 17/8/2026 · 11 min read
default, every time ros2 run is a separate operating system process — two nodes that exchange data across each other still have to go through the entire DDS path: serialize message, send via socket/loopback, deserialize again. With small message (Twist, Odometry) this cost is negligible, but with high-resolution camera images published at high frequencies, serialization/continuous copy becomes the real bottleneck. Composition allows the pooling of multiple nodes into a common process, communicating by memory convexity instead of message over DDS.
Key concepts
Differences from regular nodes: registering the node as a plugin over class_loader, allowing the same class to run independently (ros2 run) or load the same process (composition) without modifying the code:
// motor_component.cpp
#include "rclcpp_components/register_node_macro.hpp"
class MotorComponent : public rclcpp::Node {
public:
explicit MotorComponent(const rclcpp::NodeOptions & options)
: Node("motor_component", options) { /* ... */ }
};
RCLCPP_COMPONENTS_REGISTER_NODE(MotorComponent)
# CMakeLists.txt
add_library(motor_component SHARED src/motor_component.cpp)
ament_target_dependencies(motor_component rclcpp rclcpp_components)
rclcpp_components_register_nodes(motor_component "MotorComponent")
Key difference: build out shared libraries (.so), not independently executable — a component that can be loaded onto any composition process at runtime.
How it works
Two ways to load components:
# Option 1: pool components into a process from the launch file
ros2 launch my_pkg composed_bringup.launch.py
# Option 2: dynamically load an existing container.
ros2 run rclcpp_components component_container &
ros2 component load /ComponentManager my_pkg MotorComponent
ros2 component list
component_container is a waiting "empty-shell" process, ros2 component load loading additional components at runtime — is useful when adding/bailing to running nodes without restarting the whole system.
In summary: Composition does not change the logic inside the node — it changes where the node runs and how the data moves between the nodes. The biggest benefit is intra-process communication: the publisher and subscriber in the same process can send messages using a pointer, skipping the entire serialization step.
Technical details
Formulas
The cost savings are estimated by eliminating the serialize + copy step between the two processes:
Inter-process communication cost ≈ serialization time + socket copy time + deserialization time
Intra-process communication cost ≈ passing just 1 pointer (almost 0)
Example: a 1920×1080 RGB camera image ≈ 6MB per frame
Serialization + copying through loopback can take a few ms per frame at high frequencies
→ with a 30fps camera, saving tens of ms/second matters for a real-time image processing pipeline
Parameters
Different container types affect how the internal components are scheduled to run:
| Container type | Running Way |
|---|---|
component_container | Single - file executor — all components in one file |
component_container_mt | Multithreadable — component executor can run in parallel |
component_container_isolated | Each component has a fully separate executor/stream |
Practical applications
The table determines when it is worth using.
| Situation | Should I use Composition? |
|---|---|
| Camera driver → image processing node (OpenCV/YOLO) high frequency | Has — saving serialize large continuous images |
| Some low - speed control nodes (Twist, status) | Unnecessary — DDS cost with negligible small message |
| Expected to isolate — 1 node crash does not collapse another node | No — same process means one node crashes the whole group |
This is the core trade-off to consider for each specific node pair: pipeline camera→AI on AMR clearly benefits from composition, but emergency stop node should be intentional Run separate processes to ensure another node crash doesn't lead to a safe reaction.
Practical example
$ ros2 component list
Output:
/ComponentManager
1 /camera_driver
2 /image_processor
3 /object_detector
All 3 components run in the same process /ComponentManager— image data transmitted from camera_driver through image_processor to object_detector without serializing via DDS at each step, as long as all 3 publish/subscribe to the correct intra-process configuration.
Common mistakes
- Build to executable instead of shared libraries (
.so)— forgetting to changeadd_executabletoadd_library(... SHARED ...)inCMakeLists.txtrendering the component unable to load into the container composition, although running independently usingros2 runis normal. - 1 component crash pulls the entire container— as stated in the decision table, this is an inherent trade-off of the composition — not to include the node that needs strict error isolation (e.g., safety) in the same container with other nodes.
- Defining the registered macro
RCLCPP_COMPONENTS_REGISTER_NODE— component container does not find class on loading, reporting an error of "class not found" despite the correct build and existence of the.solibrary. - Multiplexing of single-threaded containers with multiple-threaded components in the presence of a heavy-task component— using
component_container(single-threaded) for a long-processing callback component (e.g., inference AI) can intercept other components in the same container — consideringcomponent_container_mtor_isolatedin the presence of a heavy-task component.
Conclusion
Composition allows the same code component to run independently or pooling processes to eliminate the serialize/DDS cost for high-frequency big data — the most pronounced benefit with cloud image/point processing pipelines, at the expense of loss of error isolation between nodes in the same container. Carefully consider each pair of nodes, not applying composition to every node in default. The next article in the Callback Group — tool to solve the nested service deadlock problem mentioned in ROS2 Communication.
Get new articles and lessons by email
When a new technical article or course module is published, Atlas Robotics sends you a short email. No spam.
Unsubscribe any time with the link in each email. See our Privacy policy.