CORS sometimes failing with Rails App

I have my ruby on rails app deployed on Render. In the app, I have cors configuration set up properly (the request origins are explicitly set, as well as allowed methods, headers, etc).

About 20% of the time, api requests via fetch on the client side (not deployed on Render, but vercel) fail due to CORS error (see screenshot).

Note that I have a purchased domain from Namecheap, and have configured the A Record to point to my vercel app, and the CNAME with host www for the vercel URL and CNAME with host api for the Rails app on Render. In other words:
A Record, Hostname: @, value: Vercel IP
CNAME, Hostname: www, value: Vercel URL
CNAME, hostname: api, value: Render URL

I have configured the custom domains so that on Vercel, it’s using www.mydomainexample.com and on Render, it’s using api.mydomainexample.com. All domain certs are verified.

So I’m wondering where the point of failure is with this CORS error that is occurring about 20% of the time?

Hi,

Thanks for reaching out.

There are CORS errors, as the report is a 502, so the response is not coming from your service. 502s are usually raised when your app returns an unexpected response, e.g. no response, closing the connection early, etc.

It looks like you’re using Rails 7. Puma will try to predict the number of workers by the number of CPUs it can find. On a containerized platform like Render, the CPU count will be different from the resources actually available to your service.

Because of this, Puma can start too many workers and exhaust resources, making the service feel unstable (throwing 502s). More Puma workers require more RAM, so you want to keep worker counts under control to ensure you utilising the RAM available without exhausting it.

The WEB_CONCURRENCY environment variable can be used to override the CPU count estimation. Maybe start by setting WEB_CONCURRENCY to 2. If your service metrics show low RAM use with 2, you may be able to increase the number to make more use of the resources available, without exhausting them.

Kind regards

Alan

1 Like