What is a service in ROS2?
Atlasbot · 16/8/2026 · 10 min read
"Reset encoder value to zero", "store current map to file" — these are requests to perform only once, have explicit results immediately after, not continuous data streams. Service is the ROS2 mechanism for this exact type of interaction.
Key concepts
The service operates according to the classic request - response model, which consists of two sides:
- Service Server — "registration" provides a named service, waits for requests and returns results
- Service Client — sends a request to the server, waits for a correct response before proceeding
Unlike a Topic, Service always has exactly one set of requests – responses for each call — which is identical to calling a normal function call, except that the function runs on a different node.
Summarized: Topic is suitable for continuous flow data without validation; Service is suitable for a discrete action that requires a firm knowledge of the result — such as toggle a mode, or perform a computation on demand.
How it works
Service Client Service Server
│ send request │
├─────────────────────────────────►│
│ │ process request
│ wait... │
│◄─────────────────────────────────┤
│ receive response │
A test call to a service is available from the command line, without writing code:
ros2 service list # list all active services
ros2 service call /reset_encoder std_srvs/srv/Trigger
For example, a Python - based simplified Service Server adds two integers as required:
from example_interfaces.srv import AddTwoInts
class AddServer(Node):
def __init__(self):
super().__init__('add_server')
self.srv = self.create_service(AddTwoInts, 'add_two_ints', self.add_callback)
def add_callback(self, request, response):
response.sum = request.a + request.b # process, assign results to response
return response # returning — client waiting here
The request/response data type (as AddTwoInts above) is defined in the separate .srv file of the — package topic which is further presented in the Service article in the ROS2 Communication section.
Technical details
Parameters
Some common built - in service types avoid having to define .srv separately for simple operations:
| Service Type | Request | Response | Used for |
|---|---|---|---|
std_srvs/srv/Trigger | (empty) | success: bool, message: string | Activates a simple, parameter-free action |
std_srvs/srv/SetBool | data: bool | success: bool, message: string | Toggle 1 feature/mode |
example_interfaces/srv/AddTwoInts | a: int64, b: int64 | sum: int64 | Few examples of real-world use |
Protocol
At the lower level, each ROS2 service is actually installed using 2 DDS hidden topic — 1 topic for request, 1 topic for response, plus a request/response matching mechanism via a sequence number — client publish request onto the request topic, server subscribe to that topic, finish processing publish response onto the response topic with the correct identifier so that the client identifies the correct response corresponding to its own request (important when multiple clients call on a server).
Practical applications
Service suitable for robot operating mode control operations — VD: a service SetBool named /switch_autonomous_mode that allows switching the robot between manual (teleop) and fully autonomous modes on demand from the user interface; or a service Trigger named /save_map to require the SLAM node to save the current map to the file immediately. Both are discrete actions that need to be sure have been performed successfully — in accordance with the strengths of Service versus Topic.
Practical example
$ ros2 service call /add_two_ints example_interfaces/srv/AddTwoInts "{a: 3, b: 5}"
Output:
waiting for service to become available...
requester: making request: example_interfaces.srv.AddTwoInts_Request(a=3, b=5)
response:
example_interfaces.srv.AddTwoInts_Response(sum=8)
The result sum=8 confirms the server has received the correct request, processed, and returned the correct response — the fastest way to check if a service is functioning properly without writing the client code separately.
Common mistakes
- Blocking from within callback of another node — if the client calls the service in a standby synchronization fashion within 1 callback (e.g., subscriber callback), and the server is on the same node/executor that is occupied by the standby callback itself, may cause deadlock (both sides wait together, neither side makes progress) — to use asynchronous calls when calling the service from within another callback.
- No timeout is processed when the server does not respond — if the server is frozen or not booted, the client default can wait indefinitely — always setting reasonable timeout and handling the case of no response received.
- Call the service before the server is ready — if the client calls as soon as the node is started, while the server is not yet registered (due to different node boot order), the request will fail — so use
client.wait_for_service()before the first call. - Service type mismatch between client and server — is similar to message type mismatch at Topic, client and server must correctly declare the same type
.srv— mismatch causing client to not find the corresponding server despite correct service name.
Conclusion
Service is a request - response mechanism for discrete actions that need to know for sure that the — result complements the Topic. Understanding when to use Service rather than Topic, and being aware of the risk of block/deadlock when calling synchronization, is a necessary skill when designing communication between nodes. The next article in the column is about Action — the mechanism for long tasks that require both continuous progress feedback and final results, something neither the Topic nor Service can do well individually.
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.