1

I'm developing an API in PHP. I need to know in which region this api is being requested. If possible, with the lowest latency policy (Although I didn't find anything to help with that).

For that, I'm using a global load balancer with CDN in my cloud function.

I've tried to look at the request headers and environment variables, but nothing reminds me of the region where the global load balancer with the CDN is caching the API.

From now on, I am very grateful for any help...

Donnald Cucharo
  • 3,866
  • 1
  • 10
  • 17
  • Just to clarify: do you want the region of the requester? Or of the Load Balancer? Or of the CDN? Or of the Cloud Functions (if you deploy it in several regions)? – guillaume blaquiere Jul 01 '21 at 19:11
  • I'm trying to see which region the API cache is running from. Because the GCP reports that when using the global load balancer with cdn, a copy of the cloud function is cached in all regions. So when a user requests the API, he will have minimal response time. – Jadson Lucena Jul 01 '21 at 20:08
  • It's not exactly accurate! Cloud Functions answer is cached if the cache control header is set. If so, the response is cached and the next time that the exact request is performed, the content will be served from the cache. But, becareful, if you have a request in europe and the same in the US, both will request your Cloud Functions and store in the CDN cache of the region the result. The CDN aren't synchronized all around the globe. the 2nd user in the same region as the 1st one will have a minimal latency, not if they are in different regions. – guillaume blaquiere Jul 01 '21 at 21:07
  • And because the request are served from cache, you will see only the first one, this one that will create the cache. The subsequent query are unknown from Cloud Functions, because served from cache, not from the function itself. – guillaume blaquiere Jul 01 '21 at 21:08
  • I understand your explanation. But the question remains: are there any headers, environment variables or anything else I can activate to identify where the cache is being served from? – Jadson Lucena Jul 01 '21 at 22:10
  • Because you need to catch this information on client side (on the browser of the user)? – guillaume blaquiere Jul 02 '21 at 07:07
  • I'm actually trying to get this information in the API – Jadson Lucena Jul 02 '21 at 10:43
  • What do you mean "in the API"? In the Cloud Functions code? – guillaume blaquiere Jul 02 '21 at 12:18
  • Yes, in the php code that is running in cloud function – Jadson Lucena Jul 02 '21 at 12:42
  • Read my previous comment. You will receive only the 1st request. the subsequents will be cached and you won't receive them -> it's the principle of CDN: serve directly without contacting the backend – guillaume blaquiere Jul 02 '21 at 13:32
  • So what do you suggest in the case where you need to know the region closest to the user? – Jadson Lucena Jul 03 '21 at 01:06

1 Answers1

0

If you want to know the region of the user in your backend when you process the request, you can't use CDN. You need to let all the request go to your Cloud Functions.

If you want to have the lowest latency, you can deploy the same function in several (all?) region of GCP and put in front of them a Load Balancer with a serverless NEG.

Like that, the request will be served the closest to the requester, and you will be able to get the IP of the requester and deduce the region from it. You can also rely on the Cloud Functions regions to know which region process the user request without knowing the exact location of the user IP location.

guillaume blaquiere
  • 66,369
  • 2
  • 47
  • 76
  • I've been researching custom headers, and knowingly found this resource: https://cloud.google.com/load-balancing/docs/user-defined-request-headers?hl=en-us#how_user-defined_request_headers_work Apparently this allows you to define dynamic http headers using predefined google variables: https://cloud.google.com/load-balancing/docs/user-defined-request-headers?hl=en-us#variables_that_can_appear_in_the_header_value – Jadson Lucena Aug 10 '21 at 15:17