---
title: "Create a new request throttling strategy"
slug: "create-a-new-request-throttling-strategy"
abstract: "How to implement a custom strategy"
---
# Create a new request throttling strategy

You can create a new request throttling strategy. You must:

- Create a strategy item template
- Implement a strategy class
- Apply the strategy

<details>
<summary><h2>Create a strategy item template</h2></summary>

To add a strategy item template:

- Create a new template under */sitecore/templates/System/Services/Throttle*, using the */System/Templates/Template* template. Add fields and set the base template to */sitecore/templates/System/Services/Throttle/Strategy* template.
- To configure the insert rules, add the template you created to the list in the **Masters** field in the */sitecore/templates/System/Services/Throttle/Strategy Folder/__Standard Values* item.
- Add standard values for the template you created and set the default values for the fields:

    ![The standard values of the new template](https://doc.sitecore.com/api/markdown-image/xp/en/developers/105/sitecore-experience-manager/images/Create_a_new_request_throttling_strategy-Picture_17-rId9-595221051.png)

</details>

<details>
<summary><h2>Implement a strategy class</h2></summary>

To create a strategy class:

1. In Visual Studio, create a project, and create a strategy class that inherits from the `DataThrottleStrategy` base class.
2. You must implement these three methods:
    - *InitCore* – Put all initialization steps here (for example, binding the strategy properties to strategy item fields.)
    - *VerifyCore* – Put the logic that specifies whether a request is allowed or not here. You can save the internal state in the **CustomData** field of the *ThrottleStrategyData* class. An instance of this class is passed as an argument to the method.
    - *CommitCore* – Use this method if all the strategies allowed the request and the strategy can change its internal state if it needs to do it.
3. [Register the new strategy](https://doc.sitecore.com/xp/en/developers/105/sitecore-experience-manager/dependency-injection.html "Dependency injection") by creating the class implementing `IServicesConfigurator` interface, then patch the `services` section to add the following:

    ```text
    <configurator type="CustomService.Throttle.Strategies.CustomStrategyInjections, YourDLLName" />
    ```

4. Put the binary assembly file in the *bin* folder of your Sitecore instance.

The following is an example of a strategy class implementation:

```csharp
using System;
using System.Globalization;
using Sitecore.Diagnostics;
using Sitecore.Services.Infrastructure.Throttle;
using Sitecore.Services.Infrastructure.Throttle.Strategies;
using Microsoft.Extensions.DependencyInjection;
using Sitecore.DependencyInjection;
namespace CustomService.Throttle.Strategies
{
    public class CustomStrategyInjections : IServicesConfigurator   
    {
        public void Configure(IServiceCollection serviceCollection)
        {
            serviceCollection.AddTransient<MyCustomStrategy>();.
        }
    }
    public class MyCustomStrategy : DataThrottleStrategy
    {
        private int _customField;
        private int _result;
        private MyCustomData _myCustomData;
        public MyCustomStrategy(ThrottleStrategyDataStorage storage) : base(storage)
        {
        }
        protected override void InitCore(ThrottleInitializationContext throttleInitializationContext)
        {
            // get parameters from item
            Assert.ArgumentNotNull(throttleInitializationContext.StrategyItem, nameof(throttleInitializationContext.StrategyItem));
            _customField = int.Parse(throttleInitializationContext.StrategyItem["{D55875C2-D5CA-48D9-8FBD-8092887F980F}"], CultureInfo.InvariantCulture);
            Assert.ArgumentCondition(_customField > 0, nameof(_customField), "Custom parameter is lower or equal 0");
        }
        protected override ThrottleStrategyResult VerifyCore(ThrottleStrategyData throttleStrategyData)
        {
            _myCustomData = throttleStrategyData.CustomData as MyCustomData;
            if (_myCustomData != null)
            {
                _result = _myCustomData.Rnd.Next(0, _customField);
                // if random number is in second half of 0.._customField span then the request is not passed
                if (_result > _customField / 2)
                {
                    return new ThrottleStrategyResult
                    {
                        Passed = false,
                        RetryAfter = 5
                    };
                }
            }
            return ThrottleStrategyResult.PassedResult;
        }
        protected override void CommitCore()
        {
            _myCustomData.Result = _result;
        }
    }
    internal class MyCustomData
    {
        internal readonly Random Rnd = new Random();
        internal int Result;
    }
}

```

</details>

<details>
<summary><h2>Apply the strategy</h2></summary>

To apply the strategy:

- Create a strategy item from the strategy template you created:

    ![A strategy item](https://doc.sitecore.com/api/markdown-image/xp/en/developers/105/sitecore-experience-manager/images/2-Create_a_new_request_throttling_strategy-Picture_18-rId10-1606959239.png)

Now you can apply the strategy with [the methods described](https://doc.sitecore.com/xp/en/developers/105/sitecore-experience-manager/throttle-requests.html "Throttle requests").

</details>