RATELIMIT module
Admin Guide
Section titled “Admin Guide”Overview
Section titled “Overview”This module implements rate limiting for SIP requests. In contrast to the PIKE module this limits the flow based on a per SIP request type basis and not per source IP. The latest sources allow you to dynamically group several messages into some entities and limit the traffic based on them. The MI interface can be used to change tunables while running OpenSIPS.
This module is also integrated with the OpenSIPS Key-Value Interface, providing support for distributed rate limiting using Redis or Memcached CacheDB backends.
To achieve a distributed ratelimit feature, the module can replicate its pipes counters to different OpenSIPS interfaces using the binary replicate interface (BIN). To do that, define the repl_* parameters in your configuration script.
Use Cases
Section titled “Use Cases”Limiting the rate messages are processed on a system directly influences the load. The ratelimit module can be used to protect a single host or to protect an OpenSIPS cluster when run on the dispatching box in front.
Distributed limiting is useful when the rate limit should be performed not only on a specific node, but on the entire platform. The internal limiting data will no longer be kept on each OpenSIPS instance. It will be stored in a distributed Key-Value database and queried by each instance before deciding if a SIP message should be blocked or not.
A sample configuration snippet might look like this:
... if (!rl_check("$rU", "50", "TAILDROP")) { sl_send_reply("503", "Server Unavailable"); exit; };...Upon every incoming request listed above rl_check is invoked and the entity identified by the R-URI user is checked. It returns an OK code if the current per request load is below the configured threshold. If the load is exceeded the function returns an error and an administrator can discard requests with a stateless response.
Static Rate Limiting Algorithms
Section titled “Static Rate Limiting Algorithms”The ratelimit module supports two different static algorithms to be used by rl_check to determine whether a message should be blocked or not.
Tail Drop Algorithm (TAILDROP)
Section titled “Tail Drop Algorithm (TAILDROP)”This is a trivial algorithm that imposes some risks when used in conjunction with long timer intervals. At the start of each interval an internal counter is reset and incremented for each incoming message. Once the counter hits the configured limit rl_check returns an error.
The downside of this algorithm is that it can lead to SIP client synchronization. During a relatively long interval only the first requests (i.e. REGISTERs) would make it through. Following messages (i.e. RE-REGISTERs) will all hit the SIP proxy at the same time when a common Expire timer expired. Other requests will be retransmissed after given time, the same on all devices with the same firmware/by the same vendor.
Random Early Detection Algorithm (RED)
Section titled “Random Early Detection Algorithm (RED)”Random Early Detection tries to circumvent the synchronization problem imposed by the tail drop algorithm by measuring the average load and adapting the drop rate dynamically. When running with the RED algorithm OpenSIPS will return errors to the OpenSIPS routing engine every n’th packet trying to evenly spread the measured load of the last timer interval onto the current interval. As a negative side effect OpenSIPS might drop messages although the limit might not be reached within the interval. Decrease the timer interval if you encounter this.
Slot Based Taildropping (SBT)
Section titled “Slot Based Taildropping (SBT)”SBT holds a window consisting of one or more slots. You can set the window_size parameter(seconds) which means for how long we should look back to count the calls and slot_period parameter(miliseconds) which tells how granular the algorithm should be. The number of slots will be window_size/slot_period. If, for example, you have window_size= slot_period=1 second, then after each second you shall lose the call count, but if you set the slot_period to 100 milliseconds, then when your call will be outside the window, the calls in the first 100 milliseconds shall be dropped, and the rest in the next 900 shall be kept.
Network Algorithm (NETWORK)
Section titled “Network Algorithm (NETWORK)”This algorithm relies on information provided by network interfaces. The total amount of bytes waiting to be consumed on all the network interfaces is retrieved once every timer_interval seconds. If the returned amount exceeds the limit specified in the modparam, rl_check returns an error.
Dynamic Rate Limiting Algorithms
Section titled “Dynamic Rate Limiting Algorithms”When running OpenSIPS on different machines, one has to adjust the drop rates for the static algorithms to maintain a sub 100% load average or packets start getting dropped in the network stack. While this is not in itself difficult, it isn’t neither accurate nor trivial: another server taking a notable fraction of the cpu time will require re-tuning the parameters.
While tuning the drop rates from the outside based on a certain factor is possible, having the algorithm run inside ratelimit permits tuning the rates based on internal server parameters and is somewhat more flexible (or it will be when support for external load factors - as opposed to cpu load - is added).
Feedback Algorithm (FEEDBACK)
Section titled “Feedback Algorithm (FEEDBACK)”Using the PID Controller model (see Wikipedia page), the drop rate is adjusted dynamically based on the load factor so that the load factor always drifts towards the specified limit (or setpoint, in PID terms).
As reading the cpu load average is relatively expensive (opening /proc/stat, parsing it, etc), this only happens once every timer_interval seconds and consequently the FEEDBACK value is only at these intervals recomputed. This in turn makes it difficult for the drop rate to adjust quickly. Worst case scenarios are request rates going up/down instantly by thousands - it takes up to 20 seconds for the controller to adapt to the new request rate.
Generally though, as real life request rates drift by less, adapting should happen much faster.
Dependencies
Section titled “Dependencies”OpenSIPS Modules
Section titled “OpenSIPS Modules”The following modules must be loaded before this module:
- No dependencies on other OpenSIPS modules.
External Libraries or Applications
Section titled “External Libraries or Applications”The following libraries or applications must be installed before running OpenSIPS with this module loaded:
- None.
Exported Parameters
Section titled “Exported Parameters”timer_interval (integer)
Section titled “timer_interval (integer)”The timer interval in seconds when the Network and Feedback algorithms run their queries, and the other algorithms reset their counters.
Default value is 10.
...modparam("ratelimit", "timer_interval", 5)...expire_time (integer)
Section titled “expire_time (integer)”This parameter specifies how long a pipe should be kept in memory after it becomes idle (no more operations are performed on the pipe) until deleted.
Default value is 3600.
...modparam("ratelimit", "expire_time", 1800)...hash_size (integer)
Section titled “hash_size (integer)”The size of the hash table internally used to keep the pipes. A larger table is much faster but consumes more memory. The hash size must be a power of 2 number.
Default value is 1024.
...modparam("ratelimit", "hash_size", 512)...default_algorithm (string)
Section titled “default_algorithm (string)”Specifies which algorithm should be assumed in case it isn’t explicitly specified in the rl_check function.
Default value is “TAILDROP”.
...modparam("ratelimit", "default_algorithm", "RED")...cachedb_url (string)
Section titled “cachedb_url (string)”Enables distributed rate limiting and specifies the backend that should be used by the CacheDB interface.
Default value is “disabled”.
...modparam("ratelimit", "cachedb_url", "redis://root:root@127.0.0.1/")...db_prefix (string)
Section titled “db_prefix (string)”Specifies what prefix should be added to the pipe name. This is only used when distributed rate limiting is enabled.
Default value is “rl_pipe_“.
...modparam("ratelimit", "db_prefix", "ratelimit_")...repl_buffer_threshold (string)
Section titled “repl_buffer_threshold (string)”Used to specify the length of the buffer used by the binary replication, in bytes. Usually this should be big enough to hold as much data as possible, but small enough to avoid UDP fragmentation. The recommended value is the smallest MTU between all the replication instances.
Default value is 1400 bytes.
...modparam("ratelimit", "repl_buffer_threshold", 500)...repl_timer_interval (string)
Section titled “repl_timer_interval (string)”Timer in milliseconds, used to specify how often the module should replicate its counters to the other instances.
Default value is 10 ms.
...modparam("ratelimit", "repl_timer_interval", 100)...repl_timer_expire (string)
Section titled “repl_timer_expire (string)”Timer in seconds, used to specify when the counter received from a different instance should no longer be taken into account. This is used to prevent obsolete values, in case an instance stops replicating its counters.
Default value is 10 s.
...modparam("ratelimit", "repl_timer_expire", 10)...replicate_pipes_to (integer)
Section titled “replicate_pipes_to (integer)”Used to specify the instances, that belong to a certain cluster, where the pipes should be replicated.
Default value is 0. (no replication destinations)
...modparam("ratelimit", "replicate_pipes_to", 1)...accept_pipes_from (integer)
Section titled “accept_pipes_from (integer)”Used to specify the instances, that belong to a certain cluster, from which we should expect incoming packets.
Default value is 0. (disabled)
...modparam("ratelimit", "accept_pipes_from", 1)...accept_pipes_timeout (integer)
Section titled “accept_pipes_timeout (integer)”The time between two succesive incoming packets.
Default value is 10.
...modparam("ratelimit", "accept_pipes_timeout", 1)...repl_pipes_auth_check (int)
Section titled “repl_pipes_auth_check (int)”Authentication check for incoming packets.
Default value is “0” (disabled).
...modparam("ratelimit", "repl_pipes_auth_check", 1)...window_size (int)
Section titled “window_size (int)”How long the history in SBT should be in seconds.
Default value is “10”.
...modparam("ratelimit", "window_size", 5)...slot_period (int)
Section titled “slot_period (int)”Value of one slot in milliseconds. This parameter determines how granular the algorithm should be. The number of slots will be determined by window_size/slot_period.
Default value is “200”.
...modparam("ratelimit", "window_size", 5)#we will have 50 slots of 100 millisecondsmodparam("ratelimit", "slot_period", 100)...Exported Functions
Section titled “Exported Functions”rl_check(name, limit[, algorithm])
Section titled “rl_check(name, limit[, algorithm])”Check the current request against the pipe identified by name and changes/updates the limit. If no pipe is found, then a new one is created with the specified limit and algorithm, if specified. If the algorithm parameter doesn’t exist, the default one is used.
The method will return an error code if the limit for the matched pipe is reached.
Meaning of the parameters is as follows:
- name - this is the name that identifies the pipe which should be checked. This parameter accepts both strings and pseudovariables.
- limit - this specifies the threshold limit of the pipe. It is strongly related to the algorithm used. This parameter accepts an integer or a pseudovariable. Note that the limit should be specified as per-second, not per-timer_interval.
- algorithm - this is parameter is optional and reffers to the algorithm used to check the pipe. If it is not set, the default value is used. It accepts a string or a pseudovariable.
This function can be used from REQUEST_ROUTE, FAILURE_ROUTE, ONREPLY_ROUTE, BRANCH_ROUTE, ERROR_ROUTE, LOCAL_ROUTE, TIMER_ROUTE and EVENT_ROUTE.
... # perform a pipe match for all INVITE methods using RED algorithm if (is_method("INVITE")) { if (!rl_check("pipe_INVITE", "100", "RED")) { sl_send_reply("503", "Server Unavailable"); exit; }; };... # use default algorithm for each different gateway $var(limit) = 10; if (!rl_check("gw_$ru", "$var(limit)")) { sl_send_reply("503", "Server Unavailable"); exit; };... # count only successful calls if (!rl_check("gw_$ru", "100")) { rl_dec_count("gw_$ru"); sl_send_reply("503", "Server Unavailable"); exit; };...rl_dec_count(name)
Section titled “rl_dec_count(name)”This function decreases a counter that could have been previously increased by rl_check function.
Meaning of the parameters is as follows:
- name - identifies the name of the pipe.
This function can be used from REQUEST_ROUTE, FAILURE_ROUTE, ONREPLY_ROUTE, BRANCH_ROUTE, ERROR_ROUTE, LOCAL_ROUTE, TIMER_ROUTE and EVENT_ROUTE.
... if (!rl_check("gw_$ru", "100", "TAILDROP")) { exit; } else { rl_dec_count("gw_$ru"); };...rl_reset_count(name)
Section titled “rl_reset_count(name)”This function resets a counter that could have been previously increased by rl_check function.
Meaning of the parameters is as follows:
- name - identifies the name of the pipe.
This function can be used from REQUEST_ROUTE, FAILURE_ROUTE, ONREPLY_ROUTE, BRANCH_ROUTE, ERROR_ROUTE, LOCAL_ROUTE, TIMER_ROUTE and EVENT_ROUTE.
... if (!rl_check("gw_$ru", "100", "TAILDROP")) { exit; } else { rl_reset_count("gw_$ru"); };...Exported MI Functions
Section titled “Exported MI Functions”rl_list
Section titled “rl_list”Lists the parameters and variabiles in the ratelimit module.
Name: rl_list
Parameters:
- pipe - indicates the name of the pipe. This parameter is optional. If it doesn’t exist, all the active pipes are listed. Otherwise only the one specified.
MI FIFO Command Format:
:rl_list:_reply_fifo_file_gw_10.0.0.1_empty_line_:rl_list:_reply_fifo_file__empty_line_rl_reset_pipe
Section titled “rl_reset_pipe”Resets the counter of a specified pipe.
Name: rl_reset_pipe
Parameters:
- pipe - indicates the name of the pipe whose counter should be reset.
MI FIFO Command Format:
:rl_reset_pipe:_reply_fifo_file_gw_10.0.0.1_empty_line_rl_set_pid
Section titled “rl_set_pid”Sets the PID Controller parameters for the Feedback Algorithm.
Name: rl_set_pid
Parameters:
- ki - the integral parameter.
- kp - the proportional parameter.
- kd - the derivative parameter.
MI FIFO Command Format:
:rl_set_pid:_reply_fifo_file_0.50.50.5_empty_line_rl_get_pid
Section titled “rl_get_pid”Gets the list of in use PID Controller parameters.
Name: rl_get_pid
Parameters: none
MI FIFO Command Format:
:rl_get_pid:_reply_fifo_file__empty_line_rl_bin_status
Section titled “rl_bin_status”Dumps each destination used for replication, as well as the timestamp of the last message received from them.
Name: rl_bin_status
Parameters: none
MI FIFO Command Format:
:rl_bin_status:_reply_fifo_file__empty_line_Exported Pseudo-Variables
Section titled “Exported Pseudo-Variables”$rl_count(name)
Section titled “$rl_count(name)”Returns the counter of a pipe. The variable is read-only.
NULL will be returned if the pipe does not exist.
doc copyrights:
Contributors
Section titled “Contributors”By Commit Statistics
Section titled “By Commit Statistics”Top contributors by DevScore(1), authored commits(2) and lines added/removed(3)
| # | Name | DevScore | Commits | Lines++ | Lines— |
|---|---|---|---|---|---|
| 1. | Razvan Crainea (@razvancrainea) | 112 | 42 | 2982 | 2625 |
| 2. | Ovidiu Sas (@ovidiusas) | 41 | 17 | 2503 | 66 |
| 3. | Bogdan-Andrei Iancu (@bogdan-iancu) | 29 | 24 | 137 | 148 |
| 4. | Stefan Darius (@dariusstefan) | 17 | 5 | 946 | 188 |
| 5. | Eseanu Marius Cristian (@eseanucristian) | 15 | 6 | 409 | 275 |
| 6. | Daniel-Constantin Mierla (@miconda) | 9 | 7 | 24 | 18 |
| 7. | Liviu Chircu (@liviuchircu) | 9 | 6 | 45 | 114 |
| 8. | Ionut Ionita (@ionutrazvanionita) | 5 | 1 | 265 | 24 |
| 9. | Henning Westerholt (@henningw) | 4 | 2 | 16 | 14 |
| 10. | Walter Doekes (@wdoekes) | 4 | 2 | 2 | 2 |
All remaining contributors: Arnaud Boussus, Ionel Cerghit (@ionel-cerghit), Sergio Gutierrez, Stanislaw Pitucha, Bill Hau, Julián Moreno Patiño, Konstantin Bokarius, Vlad Paiu (@vladpaiu), Edson Gellert Schubert.
(1) DevScore = author_commits + author_lines_added / (project_lines_added / project_commits) + author_lines_deleted / (project_lines_deleted / project_commits)
(2) including any documentation-related commits, excluding merge commits
(3) ignoring whitespace edits, renamed files and auto-generated files
By Commit Activity
Section titled “By Commit Activity”| # | Name | Commit Activity |
|---|---|---|
| 1. | Stefan Darius (@dariusstefan) | Jun 2026 - Jul 2026 |
| 2. | Razvan Crainea (@razvancrainea) | Sep 2011 - Jun 2026 |
| 3. | Liviu Chircu (@liviuchircu) | Mar 2014 - May 2019 |
| 4. | Bogdan-Andrei Iancu (@bogdan-iancu) | Feb 2008 - Mar 2019 |
| 5. | Julián Moreno Patiño | Feb 2016 - Feb 2016 |
| 6. | Ionut Ionita (@ionutrazvanionita) | Dec 2015 - Dec 2015 |
| 7. | Eseanu Marius Cristian (@eseanucristian) | Aug 2015 - Sep 2015 |
| 8. | Ionel Cerghit (@ionel-cerghit) | Jul 2015 - Jul 2015 |
| 9. | Bill Hau | Jul 2014 - Jul 2014 |
| 10. | Walter Doekes (@wdoekes) | Apr 2010 - Jun 2014 |
All remaining contributors: Vlad Paiu (@vladpaiu), Ovidiu Sas (@ovidiusas), Stanislaw Pitucha, Arnaud Boussus, Sergio Gutierrez, Henning Westerholt (@henningw), Daniel-Constantin Mierla (@miconda), Konstantin Bokarius, Edson Gellert Schubert.
(1) including any documentation-related commits, excluding merge commits
Documentation
Section titled “Documentation”Contributors
Section titled “Contributors”Last edited by: Razvan Crainea (@razvancrainea), Liviu Chircu (@liviuchircu), Bogdan-Andrei Iancu (@bogdan-iancu), Ionut Ionita (@ionutrazvanionita), Eseanu Marius Cristian (@eseanucristian), Walter Doekes (@wdoekes), Arnaud Boussus, Daniel-Constantin Mierla (@miconda), Konstantin Bokarius, Henning Westerholt (@henningw), Ovidiu Sas (@ovidiusas), Edson Gellert Schubert.
License
Section titled “License”All documentation files (i.e. .md extension) are licensed under the Creative Common License 4.0