Architecture

Chaos Engineering: Breaking Your Servers on Purpose, Safely

By NileVPS Team · Published · 4 min read

اقرأ المقال بالعربي

Every system fails eventually: a disk fills up, a dependency slows down, a process crashes at 3 a.m. The question is whether you discover how your system behaves under those failures during a calm, planned test or during a real outage. Chaos engineering is the practice of running controlled experiments that inject failures on purpose, so you can see whether your timeouts, retries, failover and alerts actually work. It sounds risky, but done carefully it is one of the cheapest ways to build confidence in a system. This article explains the method and shows small experiments you can run on Linux servers.

The method, not the mayhem

Chaos engineering is not randomly killing servers. It follows a scientific loop:

  1. Define steady state. Pick measurable signals of "healthy": request success rate, p95 latency, orders per minute, queue depth.
  2. Form a hypothesis. "If the cache server becomes unreachable, the API keeps answering with p95 under 800 ms and error rate under 1%."
  3. Inject a real-world failure. Latency, packet loss, a dead process, a full disk, CPU exhaustion, an expired certificate.
  4. Observe. Compare the signals with steady state during the experiment.
  5. Learn and fix. If the hypothesis was wrong, you found a weakness before your users did. Fix it, then rerun the experiment.

Control the blast radius

The difference between chaos engineering and causing an outage is control:

  • Start outside production. A staging environment that mirrors production is the right first target.
  • Start small. One instance, one dependency, a short duration such as 60 seconds.
  • Have an abort switch. Know the exact command that stops the experiment, and stop immediately if signals cross a threshold.
  • Tell people. Announce the time window so nobody wastes an hour debugging your test.
  • Take a snapshot first of any server whose state you are touching.
  • Make sure you can see. If you do not have dashboards and alerts, build them before injecting failures; otherwise you are just breaking things.

Experiments you can run on a Linux server

These use standard tools available on Debian and Ubuntu. Run them on a test server first.

Add network latency and loss

tc with the netem qdisc simulates a slow or lossy network:

# add 200 ms delay with 50 ms jitter and 2% loss on eth0
sudo tc qdisc add dev eth0 root netem delay 200ms 50ms loss 2%
# observe your service, then remove it
sudo tc qdisc del dev eth0 root

Be careful when running this on a server you reach over SSH through the same interface: your own session will slow down too. Consider wrapping it with a safety timer, such as sleep 120; sudo tc qdisc del dev eth0 root started in a separate tmux window or via systemd-run --on-active=120.

Kill or stop a dependency

sudo systemctl stop redis-server     # does the app degrade gracefully?
sudo systemctl kill -s SIGKILL myapp # does systemd restart it? check Restart= in the unit

Check that the unit has Restart=on-failure and that your monitoring raises an alert within the time you expect.

Exhaust CPU or memory

sudo apt install stress-ng
stress-ng --cpu 0 --timeout 60s          # all cores busy for 60 seconds
stress-ng --vm 1 --vm-bytes 75% --timeout 60s

Watch latency, the load average and whether the OOM killer picks the process you expect.

Fill the disk

fallocate -l 5G /var/tmp/chaos-fill   # size it to leave little free space
df -h /
rm /var/tmp/chaos-fill

Many applications behave badly when writes fail: logs stop, databases refuse connections, uploads hang. This experiment often reveals missing disk alerts and missing log rotation.

Block a dependency with the firewall

sudo iptables -I OUTPUT -p tcp -d 10.0.0.5 --dport 5432 -j DROP
# test, then remove the exact rule
sudo iptables -D OUTPUT -p tcp -d 10.0.0.5 --dport 5432 -j DROP

A dropped connection (no reply) tests your timeouts far better than a refused one.

What to look for

ExperimentCommon weakness revealed
Latency on a dependencyMissing or too-long timeouts, thread pools filling up
Dependency downNo fallback, cascading failures, no circuit breaker
Process killedService not restarted, no alert, lost in-flight jobs
CPU or memory exhaustionNo resource limits, wrong process killed
Disk fullNo disk alert, no log rotation, corrupted writes

Tools for larger setups

When you outgrow shell commands, Chaos Toolkit lets you write experiments as declarative files, and Kubernetes users can look at Chaos Mesh or LitmusChaos. Toxiproxy is handy for injecting faults between an application and a specific dependency in tests. Combine chaos results with ideas like shadow traffic to test changes with real request patterns, and keep a cheap staging server from /deploy for exactly this kind of work.

FAQ

Is chaos engineering only for large companies?

No. The tools are free and the method scales down. A single team can run a monthly 30-minute session on staging: stop a dependency, add latency, fill a disk, and check that alerts fire and the app recovers.

Should I run chaos experiments in production?

Only after experiments pass reliably in staging, with good monitoring, a small blast radius and an abort plan. Many teams get most of the value without ever touching production.

How often should we run experiments?

Repeat them after significant changes to infrastructure or dependencies, and on a regular schedule such as monthly. Automating a few safe experiments in a staging pipeline helps catch regressions in resilience.

Related guides