tencent cloud

Feedback

Circuit Breaker

Last updated: 2022-03-04 16:52:54

    Overview

    You can use the circuit breaker plugin for circuit breaking and downgrade when the backend is exceptional in order to protect the backend and recover the requests after it returns to normal.

    Note:

    The circuit breaker supports only APIs in dedicated instances but not shared instances.

    How It Works


    You can set configuration items such as judgment time window, error condition, number of errors, circuit breaking duration, and downgraded backend configuration in the circuit breaker plugin.
    A circuit breaker cycle can be divided into three phases:

    • If the number of times the backend of an API bound to the circuit breaker plugin triggers the error condition within the judgment time window exceeds the number of errors, the system will trigger and enable the circuit breaker.
    • After the circuit breaker is enabled, it will work for the time specified by the circuit breaking duration in seconds, during which all requests will be forwarded to the backend specified by the downgraded backend configuration.
    • After the circuit breaking duration ends, the circuit breaker will be disabled, and requests will return to normal.

    Directions

    Step 1. Create a plugin

    1. Log in to the API Gateway console
    2. On the left sidebar, click Plugin > System Plugin to enter the system plugin list page.
    3. Click Create in the top-left corner of the page and select Circuit Breaker as the plugin type to create a circuit breaker plugin. The plugin configuration items are as detailed below:
      Parameter Required Description
      Error condition Yes It is the condition for judging whether a request is considered an error. For more information, see the table below.
      Number of errors Yes It is the threshold of number of errors. If it is exceeded within the time window, the circuit breaker will be enabled.
      Judgment time window Yes It is the judgment time window ranging from 10 to 90 seconds.
      Circuit breaking duration Yes It is the circuit breaking duration ranging from 15 to 300 seconds.
      Downgraded backend type Yes Valid values: Disable, Public network URL/IP, VPC resources, SCF, Mock, and TSF.
      If you select **Disable**, after the circuit breaker is enabled, the backend will not be downgraded, and the error will be reported to the client; if you select another option, the selected backend will be downgraded to after the circuit breaker is enabled.
      Downgraded backend configuration Yes if the downgraded backend type is not `Disable` Enter an API backend configuration YAML file. For more information, see How to Write YAML for Downgraded Backend Configuration.
      The circuit breaker plugin supports the following conditions:
      Error Condition Type Description
      Backend response time If the backend response time exceeds the configured threshold, the request will be considered an error.
      Backend error code If the backend error codes include or do not include certain error codes, the request will be considered an error. Multiple error codes should be separated with commas.
      Backend timeout If the backend timeout period set in the API backend configuration is exceeded, the request will be considered an error.

    Step 2. Bind an API and make the plugin effective

    1. Select the just created plugin in the list and click Bind API in the Operation column.
    2. In the Bind API pop-up window, select the service, environment, and the API to which the plugin needs to be bound.
    3. Click OK to bind the plugin to the API. At this time, the configuration of the plugin has taken effect for the API.

    PluginData

    {
      "failure_condition":"latency_seconds>1 or func_not_in(status_code,{200,201,202})",  // Error condition
      "failure_threshold":20,   // Maximum number of errors for circuit breaker triggering, which must be a positive integer
      "time_window":50,    // Error count judgment time window in seconds. Value range: 10–90 seconds
      "open_state_duration":69,    // Circuit breaking duration in seconds. Value range: 15–300 seconds
      "backend_type":"MOCK",    // Downgraded backend type. Valid values: MOCK, HTTP, SCF, VPC, UPSTREAM, TSF
      "backend_config":{    // Downgraded backend configuration
          "ServiceType":"MOCK",
          "ServiceMockReturnMessage":"hello mock from strategy"
      }
    

    How to Write YAML for Downgraded Backend Configuration

    Write the YAML file by referring to the YAML template on the plugin creation page. The fields are in one to one correspondence to the backend configuration fields in the CreateApi API.

    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support