Complete Guide to Gracefully Stopping and Restarting Redis Server

Nov 01, 2025 · Programming · 13 views · 7.8

Keywords: Redis shutdown | Ubuntu system | service management | data persistence | process signals

Abstract: This article provides a comprehensive overview of various methods to stop and restart Redis server in Ubuntu systems, including using redis-cli SHUTDOWN command, system service management commands, and process signal handling. It offers in-depth analysis of applicable scenarios, operational procedures, and important considerations, along with complete code examples and best practice recommendations to help developers manage Redis services safely and efficiently.

Overview of Redis Service Stopping Issues

In Ubuntu operating system environments, when attempting to start a new Redis server instance, encountering the "Opening port: bind: Address already in use" error message indicates that Redis service is already running and occupying the default port. This situation commonly occurs in development or testing environments where existing services must be stopped before starting new instances.

Graceful Shutdown Using redis-cli Tool

Redis provides the dedicated SHUTDOWN command for graceful service termination. By connecting to the Redis instance through the redis-cli command-line tool and executing this command, data can be safely saved before service termination.

# Connect to local Redis instance and execute shutdown command
redis-cli SHUTDOWN

After executing this command, Redis follows this shutdown sequence: first pausing all client write operations, waiting for the configured replication timeout to ensure replica nodes complete data synchronization; then stopping all client connections; performing blocking data save operations (if persistence is configured); finally flushing AOF files and exiting the server process.

System Service Management Commands

In Ubuntu systems, Redis typically runs as a system service and can be managed through init.d or systemctl commands.

# Stop Redis service using init.d script
/etc/init.d/redis-server stop

# Stop Redis service using systemctl (for newer Ubuntu versions)
sudo systemctl stop redis

# Restart Redis service
/etc/init.d/redis-server restart
sudo systemctl restart redis

# Start Redis service
/etc/init.d/redis-server start
sudo systemctl start redis

The system service management approach is suitable for production environments, ensuring services start and stop in the correct order while facilitating service status monitoring.

Process Signal Handling Methods

In certain situations where Redis service cannot be stopped through conventional methods, process signals can be used for forced termination.

# Find Redis process ID
ps aux | grep redis-server

# Send SIGTERM signal (equivalent to SHUTDOWN SAVE)
kill <pid>

# Send SIGINT signal (shutdown without saving data)
kill -2 <pid>

The SIGTERM signal triggers Redis to execute the same shutdown sequence as the SHUTDOWN command, ensuring data safety. The SIGINT signal immediately terminates the process without data saving, suitable for emergency situations or testing environments.

Advanced SHUTDOWN Command Options

Redis version 7.0 and above provide multiple modifiers for the SHUTDOWN command, enhancing shutdown control flexibility.

# Force save data before shutdown, even without configured save points
redis-cli SHUTDOWN SAVE

# Shutdown without saving data
redis-cli SHUTDOWN NOSAVE

# Skip waiting for replica synchronization and shutdown immediately
redis-cli SHUTDOWN NOW

# Ignore errors and force shutdown
redis-cli SHUTDOWN FORCE

# Cancel ongoing shutdown operation
redis-cli SHUTDOWN ABORT

These options can be selected based on different business requirements. For example, in caching scenarios, the NOSAVE option may be preferred to avoid blocking, while in data persistence-critical scenarios, the SAVE option should be used.

Data Safety and Risk Control

Redis shutdown process design fully considers data security. Under default configuration, the SHUTDOWN command will:

For production environments, it's recommended to perform manual failover before shutting down the master node, demoting the master to a replica and promoting one replica as the new master, thus minimizing data loss risk.

Cross-Platform Operational Differences

Redis service stopping methods vary across different operating system environments:

# macOS system using Homebrew service management
brew services stop redis

# Windows system through service manager
# Press Win+R, input services.msc, find Redis service and stop it

Understanding these differences helps quickly identify and resolve issues across different development environments.

Best Practice Recommendations

Based on practical application experience, the following best practices are recommended:

  1. Prioritize using redis-cli SHUTDOWN command in development environments to ensure data integrity
  2. Use system service management commands in production environments for better monitoring and automation
  3. Use process signal methods in emergency situations, but be aware of data loss risks
  4. Regularly check Redis configuration to ensure persistence settings meet business requirements
  5. Ensure data synchronization and failover completion before shutting down nodes in cluster environments

By appropriately selecting shutdown methods, Redis services can be safely and efficiently stopped and restarted across various scenarios, providing reliable data storage services for applications.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.