[[["易于理解","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):2024-12-22。"],[[["\u003cp\u003eSetting maximum instances limits the number of Cloud Run function instances to control costs and prevent downstream resource overload.\u003c/p\u003e\n"],["\u003cp\u003eYou can set or clear maximum instance limits for a function via the Google Cloud console or the \u003ccode\u003egcloud\u003c/code\u003e command-line tool, with a default limit of 3000 if not specified.\u003c/p\u003e\n"],["\u003cp\u003eThe ideal maximum instance value depends on the function's characteristics, such as execution time and expected traffic, and a good starting point is often a limit of 3, adjusting it based on monitoring.\u003c/p\u003e\n"],["\u003cp\u003eWhen a maximum instance limit is reached, Cloud Run functions will attempt to serve new requests for up to 30 seconds, and if no instance is available, the request will fail or, for events, they will be saved until capacity is available.\u003c/p\u003e\n"],["\u003cp\u003eDeploying a new version of a function can temporarily exceed the set maximum instance limit, as older instances finish processing requests while new ones handle new requests.\u003c/p\u003e\n"]]],[],null,["# Configure maximum instances (1st gen)\n=====================================\n\nYou can control the scaling behavior of your function by setting a maximum\nnumber of Cloud Run functions instances. Setting maximum instances controls\ncost and prevents downstream resources from being overwhelmed with traffic.\n\nCloud Run functions scales by creating new instances of your function. Each\nof these instances can handle only one request at a time, so large spikes in\nrequest volume might result in creating many instances.\n\nAutomatic scaling is beneficial most of the time, but in some cases you might\nwant to limit the total number of instances that can exist at any given time.\nFor example, your function might interact with a database that can only handle a\ncertain number of open connections.\n\nSetting and clearing maximum instances limits\n---------------------------------------------\n\nYou can set a maximum number of instances for a function during deployment. Each\nfunction has its own maximum instances setting. Functions scale independently of\neach other.\n\n### Setting maximum instances limits\n\nYou can set a maximum instances limit using either the Google Cloud CLI\nor the Google Cloud console. If you don't specify a limit, Cloud Run functions sets a\ndefault of 3000.\n\nTo set a maximum instances limit: \n\n### Console\n\n1. Go to the\n [Cloud Run functions Overview page](https://console.cloud.google.com/functions/list).\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 **Maximum number of instances** field in the **Autoscaling**\n section, enter a value or use the default.\n\n### gcloud\n\nTo set a maximum instances limit, run the `deploy` command with the\n`--max-instances` flag: \n\n```bash\ngcloud functions deploy FUNCTION_NAME --max-instances MAX_INSTANCE_LIMIT\n```\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003eFUNCTION_NAME\u003c/var\u003e: The name of the function.\n\n- \u003cvar translate=\"no\"\u003eMAX_INSTANCE_LIMIT\u003c/var\u003e: The number to set as the maximum instances\n limit - for example, 3000.\n\n### Clearing maximum instances limits\n\nYou can clear a maximum instances limit for a function using either the `gcloud`\ncommand-line tool or the Google Cloud console. \n\n### Console\n\nTo clear a maximum instances limit for a function:\n\n1. Go to the\n [Cloud Run functions Overview page](https://console.cloud.google.com/functions/list).\n\n2. Click an existing function to go to its details page. You can see the\n function's current maximum instances limit in the **Details** tab.\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 **Maximum number of instances** field in the **Autoscaling**\n section, enter `0`.\n\n### gcloud\n\nTo clear a maximum instances limit for a function,\nrun the `deploy` command with the `--clear-max-instances` flag: \n\n```bash\ngcloud functions deploy FUNCTION_NAME --no-gen2 --clear-max-instances\n```\n\nLimits \\& best practices\n------------------------\n\nThis section provides guidelines for using maximum instances.\n\n### Choose a maximum instance value\n\nThe optimal value for the maximum instances setting depends on your function's\ncharacteristics, including how long an invocation takes to execute, its expected\naverage and peak invocation frequency, and your application's tolerance for\ninvocation failures. A good rule of thumb is to start with a maximum instances\nvalue of 3, then monitor for invocation failures and adjust the maximum\ninstances value upward as necessary.\n\n### Guard against excessive scale-ups\n\nWhen no maximum instances limit is specified, Cloud Run functions favors\n*scaling up to meet demand* over limiting throughput. This means that the number\nof simultaneous instances that your function might have is effectively\nunlimited unless you've configured such a limit.\n\nWe recommend assigning a `--max-instances` limit to any functions that send\nrequests to throughput-constrained or otherwise unscalable downstream services.\nA maximum instances limit improves overall system stability and helps guard\nagainst abnormally high request levels.\n\n### Request handling when all instances are busy\n\nUnder normal circumstances, your function scales up by creating new instances to\nhandle incoming traffic load. But when you have set a maximum instances limit,\nyou might encounter a scenario where there are insufficient instances to meet\nincoming traffic load.\n\nIn that scenario, Cloud Run functions attempts to serve a new inbound request for\nup to 30 seconds:\n\n- If an instance finishes processing its request during this time period, it might start to process the new inbound request.\n- If no instance becomes available, the request will fail.\n\nRequests sent to overloaded [HTTP functions](/functions/1stgendocs/writing/write-http-functions)\nfail with a response code of\n\n- `429 Too Many Requests` if a maximum instances value is configured, or\n- `500 Internal Server Error` if no maximum instances value is configured\n\nEvents destined for event-driven functions will automatically be saved until\ncapacity is available.\n\n### Max instances limits that exceed Cloud Run functions scaling ability\n\nWhen you specify a maximum instances limit, you are specifying an upper limit.\nSetting a large limit does not mean that your function will scale up to the\nspecified number of instances. It only means that the number of instances that\nco-exist at any point in time shouldn't exceed the limit.\n\nFurther, setting a maximum instances limit might affect the scaling strategies\nthat Cloud Run functions uses to meet your traffic demand. In general,\nCloud Run functions will prioritize honoring your specified limit rather than\nscaling up and potentially exceeding your limit.\n\n### Handling traffic spikes\n\nIn some cases, such as rapid traffic surges, Cloud Run functions might, for a short\nperiod of time, create more instances than the specified maximum instances\nlimit. If your function cannot tolerate this temporary behavior, you might want\nto factor in a safety margin and set a lower maximum instances value than your\nfunction can tolerate.\n\n### Deployments\n\nWhen you deploy a new version of your function, Cloud Run functions migrates traffic\nfrom the earlier version to the new one. Because maximum instances limits are set\nfor each version of your function independently, you might temporarily exceed\nthe specified limit during the period after deployment.\n\nFor example, a function might have a maximum instances limit of 5. Under normal\ncircumstances, the function scales up to 5 instances as it handles requests.\nWhen a new version of the function is deployed, the new version has its own max\ninstances limit of 5.\n\nRequests that are already being handled by the previous version of the function\nare not interrupted when a new version of the function is deployed. Instead,\nthese requests will continue to make progress. New inbound requests will be\nhandled by the newly-deployed version of the function.\n\nThus, the function in the previous example might have up to 10 total instances\n(5 for each version of your function) during the period after deploying the new\nversion. The amount of time required for instances of the previous function to\nterminate depends on the time required for those instances to finish handling\nany active requests. This is an additional factor to take into account when\nselecting an appropriate max instances limit.\n\n### Clearing maximum instances limits\n\nSetting maximum instances for a function to 0 clears the function's existing\nmaximum instances limit but does not pause your function."]]