Emre Şahin

Инженерски дневник

Investigating Unexpected Restarts in Azure Container Apps

A field guide to finding out why a container app restarts: probes, revisions, replicas, KQL, KEDA scaling, memory limits and graceful shutdown.

Emre Şahin ·

"The container keeps restarting" is one of the least useful sentences in operations, and one of the most common. In Azure Container Apps it can mean at least five different things: a failing health probe, a new revision rolling out, a replica being scaled in, the process running out of memory, or the application exiting on its own. Each has a different cause and a different fix.

This is the method I use to tell them apart. It is not tied to one incident. It is the checklist I go through whenever a container app behaves in a way nobody expected.

Start by naming what actually happened

Before opening any logs, I try to answer one question precisely: what restarted?

  • A container inside a replica can be restarted by the platform while the replica stays.
  • A replica can be removed and replaced by a new one, for example when scaling.
  • A revision can be replaced by a new revision after a deployment or configuration change.

These look similar from the outside, because in each case the process starts again and in-memory state is gone. But they leave different traces. Being clear about which one we are looking at saves a lot of time later.

Health probes: the most common suspect

Container Apps supports three kinds of probes, and each one fails differently.

  • Startup probe: gives a slow-starting application time before the other probes begin. If it fails, the container is restarted.
  • Liveness probe: checks whether the process is still healthy. Repeated failures cause a restart.
  • Readiness probe: checks whether the replica should receive traffic. Failures remove it from load balancing, but do not restart it.

Most surprise restarts I have looked at came from a liveness probe that was too strict, or that checked the wrong thing. Two patterns show up again and again.

The first is a liveness endpoint that depends on downstream services. If the database is slow, the liveness check fails, the platform restarts the container, and now you have a restart loop on top of a database problem. Liveness should answer "is this process stuck?", not "is the whole world healthy?". Dependency checks belong in readiness, if anywhere.

The second is a missing or too-short startup probe. An application that warms caches, runs migrations or loads models at startup can fail liveness before it ever finishes booting. The fix is usually a startup probe with a generous threshold, not a longer liveness timeout.

Revisions and replicas

A new revision is created whenever you change something revision-scoped: the image, environment variables, scale rules, probes and similar settings. In single-revision mode, the old revision is deactivated once the new one is ready. From the application's point of view, every replica of the old revision was shut down.

So the first thing I check is whether a deployment or configuration change happened around the time of the restart. Pipelines, Terraform or OpenTofu applies, and manual portal changes all count. If a new revision appeared, the "restart" may simply be a rollout.

Replicas are a separate story. They come and go as the scaler adds or removes capacity. A replica being removed during scale-in is not a failure, but if the application does not shut down cleanly, it can look like one.

Querying the logs

When Container Apps is connected to a Log Analytics workspace, two tables do most of the work. ContainerAppSystemLogs contains platform events: probe failures, container terminations, scaling and revision activity. ContainerAppConsoleLogs contains what the application wrote to stdout and stderr. Depending on how the environment was set up, the table names may carry a _CL suffix and the column names may differ slightly, so I always check the schema first.

A first pass over system events for one app:

ContainerAppSystemLogs
| where ContainerAppName == "orders-api"
| where TimeGenerated > ago(24h)
| project TimeGenerated, RevisionName, ReplicaName, Reason, Log
| order by TimeGenerated desc

Then I group by reason to see which kind of event dominates:

ContainerAppSystemLogs
| where ContainerAppName == "orders-api"
| where TimeGenerated > ago(24h)
| summarize count() by Reason, bin(TimeGenerated, 15m)
| render timechart

If probe failures or terminations cluster around specific times, I look at what the application said just before:

ContainerAppConsoleLogs
| where ContainerAppName == "orders-api"
| where TimeGenerated between (datetime(2026-01-01T10:00) .. datetime(2026-01-01T10:15))
| project TimeGenerated, ReplicaName, Log
| order by TimeGenerated asc

The time window here is a placeholder. The point is to correlate: the platform event tells you that something happened, the console log often tells you why.

I also look at revision names across the window. If the revision changes in the middle of it, I go back to the deployment history before I look at anything else.

KEDA scaling and scale-to-zero

Container Apps uses KEDA-based scale rules: HTTP concurrency, queue length, CPU, memory or custom scalers. Scaling is often the hidden cause behind "random" restarts.

A few interactions I check:

  • Scale-to-zero with a minimum of zero replicas. After a quiet period, all replicas disappear. The next request starts a fresh one. This is expected, but if startup is slow, the first requests may time out and look like an outage.
  • Queue-based scaling with long-running work. A replica processing a message can be chosen for scale-in. If the work is not idempotent or the message is not returned to the queue properly, you get duplicates or lost work.
  • Aggressive rules that oscillate. Rules that react quickly in both directions can add and remove replicas repeatedly. Each removal is a shutdown, and each shutdown is a chance for something to go wrong.
  • Long-lived connections. WebSocket or streaming connections do not move with the replica. When a replica is removed, every client connected to it must reconnect.

If the restarts correlate with traffic patterns rather than deployments or errors, scaling is the first place I look. Sometimes the right answer is a minimum replica count of one, accepting the cost for predictability.

Memory limits and OOM

Each container has CPU and memory allocations. When a process exceeds its memory limit, it is killed. The system log will usually show the termination, and the console log will show nothing useful, because the process did not get a chance to write anything.

That silence is itself a clue. If a replica disappears with no error in the application log, I suspect memory first.

Things I check:

  • Memory usage trend in the metrics, not just the peak. A slow climb suggests a leak or an unbounded cache.
  • Runtime settings. A .NET application, for example, should be aware of the container's memory limit; if it is not, the garbage collector may not run early enough.
  • Large requests or uploads buffered into memory.
  • Sidecars or other containers in the same app sharing the allocation.

Raising the limit can be a valid short-term fix, but I try not to stop there without understanding why the memory grew.

Graceful shutdown and SIGTERM

Every replica removal, whether from scaling, a new revision or maintenance, ends with the platform sending SIGTERM to the container. The application then has a grace period before it is forcefully killed.

An application that ignores SIGTERM loses in-flight work. An application that handles it well stops accepting new work, finishes or hands back what it has, flushes telemetry and exits.

In practice I check three things. The process actually receives the signal, which is not always true if the entrypoint is a shell script that does not forward it. The application has a shutdown hook that does something meaningful. And the shutdown work fits within the grace period, which can be configured if needed.

Many "unexpected restarts" are expected shutdowns that the application handled badly. The restart was normal; the damage was not.

Writing it up

Once I understand what happened, I write a short incident analysis, even when the impact was small. The structure I use is simple:

  • What we observed: the symptom, as users or alerts saw it.
  • What actually happened: container, replica or revision, and the mechanism.
  • Evidence: the queries and log excerpts that support the conclusion.
  • Contributing factors: probe design, scaling rules, resource limits, shutdown handling.
  • Changes: what we will change, who owns it, and how we will know it worked.
  • Open questions: what we still do not know.

Including the queries is important. The next person investigating a similar problem can start from them instead of from a blank editor, and over time they become a small runbook.

I also try to keep the tone factual. Restarts in a managed platform are rarely one person's mistake. They are usually the result of reasonable defaults meeting an application that had different assumptions.

A short checklist

  1. Decide whether a container, replica or revision restarted.
  2. Check for deployments or configuration changes in the window.
  3. Query system logs and group by reason.
  4. Correlate with console logs just before the event.
  5. Review probe design, especially liveness dependencies and startup time.
  6. Compare restarts with traffic and scaling activity.
  7. Check memory trends and limits.
  8. Verify SIGTERM handling and the grace period.
  9. Write it down, with the queries.

None of these steps is complicated. The value is in doing them in order, so that you do not spend an afternoon tuning memory when the real cause was a liveness probe calling the database.