Understanding systemd Dependencies: After= vs Requires= in Service Units


3 views

When configuring service units in systemd, understanding the distinction between After= and Requires= is crucial for proper service orchestration. These directives serve fundamentally different purposes in the dependency model:

[Unit]
Description=Example Service
Requires=postgresql.service
After=postgresql.service network.target

The Requires= directive establishes a strong dependency relationship between units. If unit A specifies Requires=unitB.service:

  • Unit B must be activated successfully for Unit A to start
  • If Unit B fails or stops, Unit A will be deactivated
  • This creates a runtime dependency that affects the lifecycle of both units

The After= directive specifies ordering constraints without creating a runtime dependency:

  • Only affects the startup sequence, not runtime behavior
  • Unit B must reach the "active" state before Unit A starts
  • No impact if Unit B stops after Unit A is running

Consider a web application that depends on a database:

[Unit]
Description=Web Application
Requires=postgresql.service
After=postgresql.service network.target

[Service]
ExecStart=/usr/bin/webapp
Restart=on-failure

1. Circular Dependencies: Avoid situations where Service A requires Service B, which in turn requires Service A.

2. Overusing Requires=: Only use Requires= when absolutely necessary. For most cases, Wants= provides a softer alternative.

3. Combining Directives: Typically you'll want to use both together when you need both ordering and runtime dependency.

For more complex scenarios, consider these additional directives:

  • Wants=: Similar to Requires but doesn't fail if dependency fails
  • BindsTo=: Stronger version of Requires with lifecycle binding
  • PartOf=: Groups units together for stop/restart operations

Remember that proper dependency configuration is essential for system stability and predictable service behavior in systemd-managed environments.


When configuring systemd service units, two crucial directives often cause confusion: After= and Requires=. While both deal with unit relationships, they serve fundamentally different purposes in the systemd ecosystem.

After= specifies the ordering of units during startup/shutdown, while Requires= defines hard dependencies between units. The key distinction:

[Unit]
# This ensures postgresql starts BEFORE our app
After=postgresql.service

# This makes postgresql a mandatory dependency
Requires=postgresql.service

Consider these scenarios:

  • With only After=: Your service starts after the specified unit, but systemd won't fail if that unit fails to start
  • With Requires=: Your service won't start at all if the required unit fails

For database-dependent applications:

[Unit]
Description=Web Application
After=nginx.service postgresql.service redis.service
Requires=postgresql.service
Wants=redis.service

This configuration means:

  1. The web app requires PostgreSQL (hard dependency)
  2. It prefers Redis (soft dependency via Wants=)
  3. It starts after all three services

For complex microservice architectures:

[Unit]
Description=Order Processing Service
After=rabbitmq.service auth-service.service
Requires=auth-service.service
Wants=rabbitmq.service

[Service]
ExecStart=/usr/bin/order-processor
Restart=on-failure

Use these commands to verify dependencies:

systemctl list-dependencies your-service.service
systemd-analyze verify your-service.service
journalctl -u your-service.service -b
  • Use After= for sequencing without strict requirements
  • Reserve Requires= for essential dependencies
  • Combine with Wants= for optional dependencies
  • Always test dependency chains with systemd-analyze verify