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:
- Wait up to 10 seconds (configurable via shutdown-timeout) for replicas to complete data synchronization
- Execute RDB snapshot saves (if save points are configured)
- Flush AOF file contents to disk
- Close all client connections in order
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:
- Prioritize using redis-cli SHUTDOWN command in development environments to ensure data integrity
- Use system service management commands in production environments for better monitoring and automation
- Use process signal methods in emergency situations, but be aware of data loss risks
- Regularly check Redis configuration to ensure persistence settings meet business requirements
- 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.