How to set timeout for API requests in Strapi?

Hi guys, how can we set timeout for Strapi API requests? This seems not to be documented anywhere in the docs and there are not any related topics in the forum either.

Thanks in advance.

In what way you want to set a timeout ? Like rate limit the response or you trying to set a timeout on the frontend to fetch the data ?

I want to set timeout for incoming requests. Because there will be cases that db queries respond quite slow so I want to just terminate the request and respond 408 Timeout to the client.

And furthermore, does such “timeout” also terminate the pending db queries or clear Knex pool? Most of the time when I get slow queries there will be an error “KnexTimeout: Pool is full”. Hence it would be good if the timeout behavior I described can help release some connections in the pool.

My context is that we have high traffic, approximately 10k CCU and 50-70k API requests at a time.

FYI, we are using Postgres.

Many thanks.

Hello Khoa,
Since strapi uses Koa for requests, you should be able to send a timeout response by doing something like this:
return ctx.throw(408);
See Koa - next generation web framework for node.js for more documentation.

This will most likely not cancel the db query, and I’m not sure it can be done easily, because I believe you’ll need the Postgres process ID for the running query to stop it (see Killing/cancelling a long running Postgres query).

I would highly suggest you review your data structure and queries to make sure you can’t improve their performance first.
Some in-memory caching can do a lot of good sometimes.

1 Like