Workers Are So Slow
I'm facing an issue with my Cloudflare Worker. I deployed some APIs to my worker, but the api responses are very slow. I found that the problem lies in the latency and fetch method during the request process. I have identified three services provided by Cloudflare that might help with speeding up the worker API response:
1. Smart Placement
2. Load Balancing
3. Argo Smart Routing
Although I'm using the D1 database in my workers, I have discovered that the issue is not related to database queries based on the timing logs. Can you please clarify which of the above three services are helpful for accelerating the worker API response? It is currently too slow (average 4 seconds).
4 Replies
I had similar issues before. In my case, the reasons were:
- The DB was far away from the worker edge location. Solved it by making the DB close to the customer in location hints.
- Smart placement would work only if you are making more than one query
- Created a replica through multiple databases (one in each region) and used the cf parameter continent to determine which db to call first, and then fallback to the master db if nothing could be found - in case replica did not run yet for some reason
Thanks omar. Great advice. Can you please show me some demo codes of advice 3?( how to determine which db to call first by cf parameter continent) And how to create a replica through multiple databases (one in each region) ?
Sure, let me create a quick example
You can still distinguish between us-east and us-west + eu-east and eu-west if you want to by adjusting the REGIONS_BY_CONTINENT array, or doing the mapping based on the country code instead
Obviously, in your bindings, you will have multiple dbs with the names: db-eu, db-us, db-asia and db-global
@piko
wow, thanks a