In this lesson, you will learn how to orchestrate service relationships by distinguishing between the Wants and Requires directives in Systemd unit files.
The Logic of Dependency
When designing your system architecture, you often need one service to start alongside another. However, not all dependencies are "mission-critical." Systemd uses Wants and Requires to define these relationships, dictating exactly how your system should behave when a dependency fails to start or crashes.
Wants: The "Best Effort" Approach
The Wants directive is a non-strict relationship. When you configure a unit file with Wants=service-b.service, you are telling Systemd: "I would like service-b to be active, but if it fails to start, proceed with launching the main service anyway." This is ideal for optional components like logging agents or non-essential telemetry tools.
Requires: The "Hard Constraint" Approach
In contrast, Requires creates a strict dependency. If you define Requires=database.service in your application’s unit file, Systemd will monitor that dependency closely. If the database fails to initialize or experiences a terminal error, your application will refuse to start—or, if it was already running, Systemd will pull it down immediately.
Consider this example configuration for a web application:
[Unit]
Description=My Web App
# Hard dependency: we cannot function without the DB
Requires=postgresql.service
# Soft dependency: logs are helpful, but not required for operation
Wants=log-shipper.service
[Service]
ExecStart=/usr/bin/webapp
Why this matters
By choosing between these two, you build resilience into your boot process. If you force everything to be a Requires dependency, a minor failure in a peripheral service could cascade into a full system lockup. By using Wants for auxiliary services, you ensure that your core system remains functional even when non-essential components hit a snag. This is the foundation of building a robust, predictable system initialization sequence.
Key Takeaway: Use Requires when a service is strictly mandatory for functional integrity, and use Wants for peripheral services that should run if possible but shouldn't impede system startup.