Cloud Run functions 函数通过创建新函数实例来进行扩容。每个此类实例一次只能处理一个请求,因此创建新实例来满足需求时,请求数量达到高峰可能会导致等待时间更长。
由于函数是无状态的,因此函数有时会从头开始初始化执行环境,这称为“冷启动”。冷启动可能需要一定时间来完成,因此如果您的应用对延迟时间较为敏感,则建议您设置一个 Cloud Run functions 函数实例数下限。
空闲实例和冷启动
为最大限度地降低冷启动产生的影响,Cloud Run functions 会尝试在处理请求后的一段时间(不可指定该时间)内将函数实例保持空闲状态。在此空闲期间,打开的数据库连接等资源可能会持久保留,以防需要处理其他请求。以这种方式保持空闲状态的实例会计入您的函数的实例数上限,但您不需要为此空闲时间付费。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-05。"],[[["\u003cp\u003eSetting a minimum number of instances for Cloud Run functions helps avoid cold starts and reduces latency for latency-sensitive applications.\u003c/p\u003e\n"],["\u003cp\u003eCold starts occur when a function initializes its execution environment from scratch, which can take a noticeable amount of time.\u003c/p\u003e\n"],["\u003cp\u003eWhile Cloud Run functions keep instances idle for a period after handling requests, explicitly setting minimum instances can further reduce cold starts.\u003c/p\u003e\n"],["\u003cp\u003eYou can configure minimum instance limits during function deployment or by modifying an existing function via the Google Cloud console or the \u003ccode\u003egcloud\u003c/code\u003e command-line tool.\u003c/p\u003e\n"],["\u003cp\u003eSetting a minimum number of instances will incur costs, as you are billed for the idle time of those instances, and the Recommender service can provide recommendations for setting minimum instances for functions.\u003c/p\u003e\n"]]],[],null,["# Configure minimum instances (1st gen)\n=====================================\n\nYou can avoid cold starts for your application and reduce application latency by\nsetting a minimum number of instances.\n\nCloud Run functions scales by creating new instances of your function.\nEach of these instances can handle only one request at a time, so large spikes\nin request volume often cause longer wait times as new instances are created to\nhandle the demand.\n\nBecause functions are stateless, your function sometimes initializes the\nexecution environment from scratch, which is called a *cold start*. Cold starts\ncan take significant amounts of time to complete, so we recommend setting a\nminimum number of Cloud Run functions instances if your application is\nlatency-sensitive.\n| **Note:** Setting a minimum number of instances incurs cost. For details, see the [Pricing](/functions/pricing) page.\n\n### Idle instances and cold starts\n\nTo minimize the impact of\n[cold starts](/functions/1stgendocs/concepts/execution-environment#cold-starts),\nCloud Run functions attempts to keep function instances idle for an unspecified\namount of time after handling a request. During this idle time, resources such\nas open database connections might be persisted in case another request needs\nto be handled. Instances kept idle in this way count toward the maximum\ninstances limit for your function, but you are not billed for this idle time.\n\nThis built-in behavior might not be sufficient for your application,\nparticularly if it is latency-sensitive. Explicitly setting a minimum number of\ninstances can further help you avoid cold starts and reduce application latency.\n\nNote that when instances are kept idle because a minimum number of instances has\nbeen set, these instances are not considered active unless they have served\ntraffic recently. For example, this means that if a function has not recently\nserved traffic, the \"Active Instances\" metric may show no instances are active,\neven if a value for minimum instances has been set.\n\nNote also that when you set a minimum number of instances, you *are* billed for\nthe idle time of those instances---see [Pricing](/functions/pricing).\n\nSetting and clearing minimum instance limits\n--------------------------------------------\n\nYou can set a minimum number of instances for a function during deployment. Each\nfunction has its own minimum instances setting.\n\n### Setting minimum instance limits\n\nYou can set a minimum instance limit using either the Google Cloud CLI\nor the Google Cloud console. \n\n### Console\n\nTo set a minimum instance limit for a new function:\n\n1. To create a new function, go to the Cloud Run functions page in the\n Google Cloud console: \n\n [Go to the Cloud Run functions page](https://console.cloud.google.com/functions)\n\n2. Click **Create function**.\n\n3. Fill in the required fields for your function.\n\n4. Expand the **Runtime, build...** section at the end of the page and click\n the **Runtime** tab.\n\n5. In the **Minimum number of instances** field in the **Autoscaling**\n section, enter a number greater than or equal to 1.\n\n6. Click **Next**.\n\n7. Click **Deploy**.\n\nTo set a minimum instance limit for an existing function:\n\n1. Go to the Cloud Run functions page in the Google Cloud console: \n\n [Go to the Cloud Run functions page](https://console.cloud.google.com/functions)\n\n2. Click the name of an existing function to be taken to its\n **Function details** page.\n\n3. Click **Edit**.\n\n4. Expand the **Runtime, build...** section at the end of the page and click\n the **Runtime** tab.\n\n5. In the **Minimum number of instances** field in the **Autoscaling**\n section, enter a number greater than or equal to 1.\n\n6. Click **Next**.\n\n7. Click **Deploy**.\n\n### gcloud\n\nTo set a minimum instance limit, run the following command: \n\n gcloud functions deploy \u003cvar translate=\"no\"\u003eFUNCTION_NAME\u003c/var\u003e \\\n --no-gen2 \\\n --min-instances \u003cvar translate=\"no\"\u003eMIN_INSTANCE_LIMIT\u003c/var\u003e\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003eFUNCTION_NAME\u003c/var\u003e: The name of your function.\n\n- \u003cvar translate=\"no\"\u003eMIN_INSTANCE_LIMIT\u003c/var\u003e: The number to set as the minimum instance\n limit, cannot exceed 1000.\n\nThe function's minimum instance limit is set.\n\n### Clearing minimum instance limits\n\nYou can clear or remove a minimum instance limit using either the\nGoogle Cloud CLI or the Google Cloud console. \n\n### Console\n\nTo clear a function's minimum instance limit:\n\n1. Go to the Cloud Run functions page in the\n Google Cloud console: \n [Go to the Cloud Run functions page](https://console.cloud.google.com/functions)\n\n2. Click the name of an existing function to be taken to its\n **Function details** page.\n\n3. Click **Edit**.\n\n4. Expand the **Runtime, build...** section at the end of the page and click\n the **Runtime** tab.\n\n5. In the **Minimum number of instances** field in the **Autoscaling**\n section, enter 0.\n\n6. Click **Next**.\n\n7. Click **Deploy**.\n\n### gcloud\n\nUse the `--clear-min-instances` flag at deploy time: \n\n gcloud functions deploy \u003cvar translate=\"no\"\u003eFUNCTION_NAME\u003c/var\u003e --clear-min-instances\n\nThe function's minimum instance limit is cleared.\n\nGetting recommendations to set minimum instances\n------------------------------------------------\n\nThe [Recommender service](/functions/1stgendocs/configuring/recommender) automatically\nsupplies recommendations for deployed functions that could benefit from\nhaving minimum instances set."]]