Getting 401: Missing or invalid credentials after DB replication

After replication of data into production my public API endpoints start returning the following response:

{"data":null,"error":{"status":401,"name":"UnauthorizedError","message":"Missing or invalid credentials","details":{}}}

My Strapi implementation uses two databases - one for the administration and another for serving the live content. This allows our content managers to create content safely and preview it in a staging environment and when they’re happy with they can promote all content from the staging db into the production db - under the hood this is running pgsync which replicates one PostreSQL DB to another.

This setup and approach may be somewhat unusual but it used to work perfectly in V3 but now I’ve switched to V4 it’s causing problems. As soon as the replication completes all of my API GETs start throwing 401s. If I restart the pods everything works correctly again.

Can any advise what’s causing Strapi to throw these errors once the DB replication has occurred?

1 Like

I’m getting the same error on my VPN server on initial load. If I then refresh the page, the following request return data.

In Strapi logs of the intial request:

2022-06-25T16:30:24.504733806Z Connection Error: Connection ended unexpectedly
2022-06-25T16:30:24.508437093Z [2022-06-25 16:30:24.508] http: GET /api/articles (6 ms) 401

and the following one:

2022-06-25T16:30:36.754554686Z [2022-06-25 16:30:36.754] http: GET /api/articles (66 ms) 200

To be honest I’m a bit confused by this behavior and I don’t really know what’s wrong.

1 Like

Recently migrated my Strapi instance from Heroku to Railway and getting this error in the new environment. Has anyone else found a way to fix this?

This thread may also have some information of use to anyone that’s landed over here. So far adding the pool information to my database config file as described there seems to have addressed the 401 issues I was having.

1 Like

i had the same issue. this thread helped me narrowing down the issue to database connection pooling. the strapi docs are quite clear on this:

When using Docker, change the pool min value to 0 as Docker will kill any idle connections, making it impossible to keep any open connections to the database