nodetool drain
Drains the node by flushing all memtables, stopping acceptance of writes, and preparing for shutdown.
Synopsis
Section titled “Synopsis”nodetool [connection_options] drainSee connection options for connection options.
Description
Section titled “Description”nodetool drain gracefully prepares a node for shutdown by:
- Flushing all memtables to SSTables
- Stopping native transport (CQL connections)
- Stopping gossip
- Stopping accepting writes
- Completing in-flight requests
This ensures no data loss when stopping Cassandra.
When to Use
Section titled “When to Use”Before Stopping Cassandra
Section titled “Before Stopping Cassandra”Always Drain Before Shutdown
nodetool drainsudo systemctl stop cassandraWithout drain, Cassandra must replay commit logs on restart, which takes longer.
Before Upgrades
Section titled “Before Upgrades”nodetool drainsudo systemctl stop cassandra# Perform upgradesudo systemctl start cassandraBefore Maintenance
Section titled “Before Maintenance”nodetool drain# Node is now safe to work onWhen NOT to Use
Section titled “When NOT to Use”For Quick Restart
Section titled “For Quick Restart”If Cassandra needs to restart quickly and data persistence is not critical:
# Faster but commit log replay on startupsudo systemctl stop cassandraHowever, this is rarely recommended.
When Node Will Be Removed
Section titled “When Node Will Be Removed”If decommissioning the node:
# Use decommission insteadnodetool decommissionDrain Process
Section titled “Drain Process”| Step | Action | Description |
|---|---|---|
| 1 | Stop accepting new connections | No new client connections accepted |
| 2 | Complete in-flight requests | Allow current requests to finish |
| 3 | Flush all memtables to disk | Ensures all data in memory is written to SSTables |
| 4 | Stop gossip | Node stops communicating with cluster |
| 5 | Stop native transport | CQL connections closed |
| 6 | Enter DRAINED state | Node is safe to stop |
After Drain
Node is now safe to stop. The Cassandra process is still running but not accepting work.
After Drain
Section titled “After Drain”Node State
Section titled “Node State”nodetool info# Shows: Mode: DRAINEDBehavior
Section titled “Behavior”| Feature | State |
|---|---|
| CQL connections | Refused |
| Gossip | Stopped |
| Writes | Rejected |
| Reads | May still work briefly |
| JMX | Still available |
Stopping Cassandra
Section titled “Stopping Cassandra”# After drain completessudo systemctl stop cassandra
# Orpkill -f CassandraDaemonExamples
Section titled “Examples”Standard Shutdown
Section titled “Standard Shutdown”nodetool drainsudo systemctl stop cassandraShutdown Script
Section titled “Shutdown Script”#!/bin/bashecho "Draining node..."nodetool drain
echo "Waiting for drain to complete..."while nodetool info 2>/dev/null | grep -q "Mode: NORMAL"; do sleep 1done
echo "Stopping Cassandra..."sudo systemctl stop cassandraecho "Done"Remote Drain
Section titled “Remote Drain”ssh 192.168.1.101 "nodetool drain"ssh 192.168.1.101 'sudo systemctl stop cassandra'Monitoring Drain
Section titled “Monitoring Drain”Check Progress
Section titled “Check Progress”# Watch memtable flushnodetool tpstats | grep -i flush
# Check modenodetool info | grep ModeDrain Complete
Section titled “Drain Complete”When drain completes:
Mode: DRAINEDtail -f /var/log/cassandra/system.log | grep -i drainCommon Issues
Section titled “Common Issues”Drain Times Out
Section titled “Drain Times Out”If drain takes too long:
- Check for large memtables
- Check disk I/O performance
- Check for pending tasks
nodetool tpstatsnodetool compactionstats"Cannot drain - already drained"
Section titled “"Cannot drain - already drained"”Node was already drained:
# Just stop the processsudo systemctl stop cassandraConnections Still Active After Drain
Section titled “Connections Still Active After Drain”Some connections may linger briefly. Drain stops new connections but allows in-flight requests to complete.
Stuck in Draining State
Section titled “Stuck in Draining State”If drain doesn't complete:
# Check what's blockingnodetool tpstatsnodetool info
# Last resort: force stopsudo systemctl stop cassandraForce Stop Risk
Force stopping without drain completing requires commit log replay on restart.
Drain vs. Other Commands
Section titled “Drain vs. Other Commands”| Command | Purpose | Continues Running |
|---|---|---|
drain | Prepare for shutdown | Yes (drained state) |
stopdaemon | Stop Cassandra immediately | No |
disablebinary | Stop CQL only | Yes (operational) |
disablegossip | Stop gossip only | Yes (isolated) |
When to Use Each
Section titled “When to Use Each”| Scenario | Command |
|---|---|
| Graceful shutdown | drain then stop |
| Emergency stop | stopdaemon (last resort) |
| Maintenance (keep running) | disablebinary |
| Network troubleshooting | disablegossip |
Recovery from Drained State
Section titled “Recovery from Drained State”To return a drained node to service:
# Must restart Cassandrasudo systemctl restart cassandraThere is no "undrain" command. The node must be restarted.
Best Practices
Section titled “Best Practices”Drain Guidelines
- Always drain before shutdown - Prevents data loss
- Wait for completion - Check mode is DRAINED
- Include in scripts - Automate shutdown procedures
- Monitor during drain - Catch any issues
- Plan for duration - Large memtables take time to flush
Shutdown Checklist
Section titled “Shutdown Checklist”- Check node status:
nodetool status - Drain the node:
nodetool drain - Verify drained:
nodetool info | grep Mode - Stop service:
sudo systemctl stop cassandra - Verify stopped:
pgrep -f CassandraDaemon
Related Commands
Section titled “Related Commands”| Command | Relationship |
|---|---|
| flush | Flushes memtables only |
| disablebinary | Disables CQL only |
| disablegossip | Disables gossip only |
| info | Check drain state |