The Problem: Memory Leaks and Downtime
We've all experienced it—your computer slows down after a few weeks of continuous use. Software accumulates memory leaks gradually, and even the operating system isn't immune. The same issue affects servers.
While you might want to keep a server running indefinitely to avoid restart downtime, memory consumption creeps up over time. The real challenge is this: restarting servers means your website or API goes down temporarily. For a service that needs to be available 24/7, downtime isn't an option.
There's a better approach.
The Solution: Automated Restarts with AWS Elastic Beanstalk
AWS Elastic Beanstalk offers an elegant solution: schedule automatic restarts during off-peak hours without taking your service offline. Here's how it works.
When Elastic Beanstalk auto-scales up, it launches new server instances. When it scales down, it terminates older instances while keeping the newer ones. By strategically scheduling a scale-down followed by a scale-up during the night, you effectively restart your infrastructure with zero downtime.
Instead of manually managing individual EC2 instances, Elastic Beanstalk handles the orchestration. Your application stays online the entire time.
Step-by-Step Tutorial
Follow these steps to set up automatic nightly restarts:
-
Open Elastic Beanstalk
- Go to AWS Elastic Beanstalk
- Select your Environment
- Click on Configuration
-
Navigate to Capacity Settings
- Scroll down to the "Instances" section or find "Capacity"
- Continue scrolling to locate "Time-based scaling"
-
Add Scheduled Actions
- Click "Add scheduled action"
- Create your second action: UpscaleToRestartMidnight
- Schedule this to run a few minutes after the downscale (e.g., 0:00 AM)
- This scales back up with fresh instances
- Click "Add scheduled action" again
- Create your first action: DownscaleToRestartMidnight
- Set this to run at your preferred restart time (e.g., 0:30 AM)
- This reduces capacity to scale down and terminate old instances
-
Verify and Deploy
- Review your scheduled actions
- Apply the configuration
- Review your scheduled actions
Why This Works
- Zero downtime: New instances are running before old ones are fully terminated
- Memory cleanup: Fresh servers start with clean memory states
- Automated: No manual intervention required
- Predictable: Happens during off-peak hours you control
Your production servers will stay stable and performant around the clock.
Have you dealt with memory leaks in your infrastructure? Share your approach in the comments below.

Top comments (0)