How can I keep Google Cloud Functions warm?

前端 未结 5 1972
自闭症患者
自闭症患者 2020-11-30 08:48

I know this misses the point of using Cloud Functions in the first place, but in my specific case, I\'m using Cloud Functions because it\'s the only way I can bridge Next.js

相关标签:
5条回答
  • 2020-11-30 09:15

    In order to keep the cold start to minimum there is not a single solution, it is a mixture of multiple techniques. The question is more how to make our lambdas so fast we don't care so much about cold starts - I am talking about a startup time in the range of 100-500 ms.

    How to make your lambda faster ?

    1. Keep your package size as small a possible (remove all big libraries where a fraction of it is used) - keep package size to max 20 MB. On every cold start this package is fetched and decompressed.
    2. Try initialise on start of your application only the pieces you want. Nodejs - https://gist.github.com/Rich-Harris/41e8ccc755ea232a5e7b88dee118bcf5
    3. If you use a JVM technology for your services, try to migrate them to Graalvm where the boot-up overhead is reduced to minimum.
      • micronaut + graalvm
      • quarkus + graalvm
      • helidon + graalvm
    4. Use cloud infrastructure configs to reduce the cold starts.

    In 2020 cold start are not such pain as few years ago. I would say more about AWS, but I am sure all above works well for any cloud provider.

    In end of 2019 AWS intorduced Lambda concurrency provisioning -https://aws.amazon.com/about-aws/whats-new/2019/12/aws-lambda-announces-provisioned-concurrency/, you don't have to care so much about warming anymore.

    0 讨论(0)
  • 2020-11-30 09:16

    You can trigger it via cron job as explained here: https://cloud.google.com/scheduler/docs/creating

    0 讨论(0)
  • 2020-11-30 09:26

    With all "serverless" compute providers, there is always going to be some form of cold start cost that you can't eliminate. Even if you are able to keep a single instance alive by pinging it, the system may spin up any number of other instances to handle current load. Those new instances will have a cold start cost. Then, when load decreases, the unnecessary instances will be shut down.

    There are ways to minimize your cold start costs, as you have discovered, but the costs can't be eliminated.

    If you absolutely demand hot servers to handle requests 24/7, then you need to manage your own servers that run 24/7 (and pay the cost of those servers running 24/7). As you can see, the benefit of serverless is that you don't manage or scale your own servers, and you only pay for what you use, but you have unpredictable cold start costs associated with your project. That's the tradeoff.

    0 讨论(0)
  • 2020-11-30 09:27

    Using Google Scheduler is a wise solution but the actual implementation is not so straightforward. Please check my article for details. Examples of functions:

    myHttpFunction: functions.https.onRequest((request, response) => {
      // Check if available warmup parameter.                                   
      // Use  request.query.warmup parameter if warmup request is GET.                                   
      // Use request.body.warmup parameter if warmup request is POST.                                   
      if (request.query.warmup || request.body.warmup) {
        return response.status(200).type('application/json').send({status: "success", message: "OK"});
      }
    });
    myOnCallFunction: functions.https.onCall((data, context) => {
      // Check if available warmup parameter.
      if (data.warmup) {
        return {"success": true};
      }
    });
    

    Examples of gcloud cli comands:

    gcloud --project="my-awesome-project" scheduler jobs create http  warmupMyOnCallFuntion --time-zone "America/Los_Angeles" --schedule="*/5 5-23 * * *" --uri="https://us-central1-my-awesome-project.cloudfunctions.net/myOnCallFuntion" --description="my warmup job" --headers="Content-Type=application/json" --http-method="POST" --message-body="{\"data\":{\"warmup\":\"true\"}}"
    
    gcloud --project="my-awesome-project" scheduler jobs create http  warmupMyHttpFuntion --time-zone "America/Los_Angeles" --schedule="*/5 5-23 * * *" --uri="https://us-central1-my-awesome-project.cloudfunctions.net/myHttpFuntion?warmup=true" --description="my warmup job" --headers="Content-Type=application/json" --http-method="GET"
    
    0 讨论(0)
  • 2020-11-30 09:33

    You're not the first to ask ;-)

    The answer is to configure a remote service to periodically call your function so that the single|only instance remains alive.

    It's unclear from your question but I assume your Function provides an HTTP endpoint. In that case, find a healthcheck or cron service that can be configured to make an HTTP call every x seconds|minutes and point it at your Function.

    You may have to juggle the timings to find the Goldilocks period -- not too often that that you're wasting effort, not too infrequently that it dies -- but this is what others have done.

    0 讨论(0)
提交回复
热议问题