# Quality of Service

## Related Readings

* [ROS Design: QoS - Deadline, Liveliness, and Lifespan](https://design.ros2.org/articles/qos_deadline_liveliness_lifespan.html)
* [Quality of Service Settings](https://docs.ros.org/en/humble/Concepts/Intermediate/About-Quality-of-Service-Settings.html)

## Default QoS Setting

ROS2 provies default QoS setting.  For example, By default, publishers and subscriptions in ROS 2 have “keep last” for history with a queue size of 10, “reliable” for reliability, **“volatile” for durability**, and “system default” for liveliness. Deadline, lifespan, and lease durations are also all set to “default”.

The volatile durability on the publishers side means the message is not stored in publishers for late-joining subscribers. This is one of the reasons why the bringup order matters during the system start-up.

## QoS Service Event Callbacks

The callback is part of the subscription options. (see [code](https://github.com/ros2/rclcpp/blob/d9b2744057b8fd230f2c71c739fcdf7e27219d85/rclcpp/include/rclcpp/subscription_options.hpp#L42))

Currently in the Humble distribution, the following callback types are defined (see [code](https://github.com/ros2/rclcpp/blob/d9b2744057b8fd230f2c71c739fcdf7e27219d85/rclcpp/include/rclcpp/event_handler.hpp#L66-L73)):

```
QOSDeadlineOfferedCallbackType deadline_callback;
QOSLivelinessLostCallbackType liveliness_callback;
QOSOfferedIncompatibleQoSCallbackType incompatible_qos_callback;
IncompatibleTypeCallbackType incompatible_type_callback;
PublisherMatchedCallbackType matched_callback;
```

**Example: Usage of deadline callback**

See [original post](https://answers.ros.org/question/352472/ros2-qos-deadline-usage/).

```
// create publisher
  rclcpp::QoS qos_profile(10);
  rclcpp::PublisherOptions publisher_options;
  qos_profile.deadline(deadline_duration);
  publisher_options.event_callbacks.deadline_callback =
    [](rclcpp::QOSDeadlineOfferedInfo & event) -> void
    {
      // handle missing deadlines
    };
  publisher_ = this->create_publisher<sensor_msgs::msg::Joy>("joy", qos_profile,  publisher_options);

  // create subscription
  rclcpp::SubscriptionOptions subscription_options;
  subscription_options.event_callbacks.deadline_callback =
  [](rclcpp::QOSDeadlineRequestedInfo & event) -> void
  {
     // handle missing deadlines
  };
   subscription_ = this->create_subscription<sensor_msgs::msg::Joy>("joy", qos_profile, std::bind(&JoyToVel::topic_callback, this, _1), subscription_options);
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.learnros2.com/ros/ros2-building-blocks/quality-of-service.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
