Starting Server runtime... why does it take so long?

Hello,

I have been trying to optimize the initial load time of my app, and while I’ve been able to improve it somewhat by reducing things like server code calls, it seems the bulk of the load time is used by the server runtime initializing. At times it seems to load for 10-15 seconds on first load, between the time when the console prints “Starting server runtime…” and “Server runtime initialized” , which is really unacceptable for a web app.
Is there a way to improve it? Am I doing something wrong that it takes so long to load?
It doesn’t always happen, but when it does the app really takes a long time to load.

1 Like

You should only have one server call. If you have more than one you are wasting time.

Does it happen only after you edit the app, or it happens randomly even without any changes?

You can time any unusual imports to see if there are any that take much time. Also look for any code you have outside of functions in server modules.

The Persistent Server option, on the Business plan, will be the best way to speed things up. I have started moving some functions over to Cloudflare Workers, to speed things up, and it also means I can replace pieces of server code bit by bit as I go along, and keep my UI the same within Anvil

Hi @zielinski.mark,

That time is spent waiting not for your code but for ours: while that message is being shown, we’re provisioning a new server runtime environment, with all of your specified packages (most of the delay is spent downloading your Python packages). This is a common issue with serverless systems, and is referred to as “cold start”.

However, once it’s started, this server environment hangs around between server calls (even if you don’t have Persistent Server turned on - so even if your code is starting afresh every time, the environment it’s running in is already there). This means that the next session will be faster - to try it out, click Restart on your app and watch how much faster it loads! If you’re seeing the slow load more than once, it’s probably because you’re changing your app’s packages, and Anvil needs to download a new image the first time you call a server function.

The other thing to mention is that, if you’re using the Free plan, you’re going to see slower initial loads. Business users get priority for server startup (even Hobby users get a boost), and their server environments stay around for longer before we shut them down.

5 Likes

Seems like it happens randomly, I am not sure if I noticed it more after making changes to the code or not.

How often does the code cold-start on the free tier and the hobby tier? Would it be everytime the app experiences a long period of inactivity?

For context, I’m trying to build an MVP for an app that would have users submitting something on forms about 3x a day. I’m currently on the free tier. It takes 23 seconds to run fairly minimal server code, most of it is spent initializing.

In short, yes - although cold start on the Hobby tier is substantially faster than the Free plan, and Business is faster still, with longer idle time before shutdown. (And, I should say, a project to investigate startup bottlenecks is on our list! 23s is not a great number.)

Thanks for getting back so quickly! Do you know apx how fast it would be on each tier and how long between timeouts?