Skip to content

Instantly share code, notes, and snippets.

Scaling your API with rate limiters

The following are examples of the four types rate limiters discussed in the accompanying blog post. In the examples below I've used pseudocode-like Ruby, so if you're unfamiliar with Ruby you should be able to easily translate this approach to other languages. Complete examples in Ruby are also provided later in this gist.

In most cases you'll want all these examples to be classes, but I've used simple functions here to keep the code samples brief.

Request rate limiter

This uses a basic token bucket algorithm and relies on the fact that Redis scripts execute atomically. No other operations can run between fetching the count and writing the new count.

@alexey-milovidov
alexey-milovidov / rounding_time.txt
Created June 20, 2016 21:00
Example of rounding time to 5-second interval in ClickHouse.
:) SELECT toDateTime(intDiv(toUInt32(EventTime), 5) * 5) AS k, count(), uniq(UserID) FROM hits_layer WHERE CounterID = 29761725 AND EventDate = today() AND EventTime >= now() - 600 GROUP BY k ORDER BY k
SELECT
toDateTime(intDiv(toUInt32(EventTime), 5) * 5) AS k,
count(),
uniq(UserID)
FROM hits_layer
WHERE (CounterID = 29761725) AND (EventDate = today()) AND (EventTime >= (now() - 600))
GROUP BY k
ORDER BY k ASC
@hassanazimi
hassanazimi / disposable_email_addresses
Last active December 22, 2022 06:51
Notify me of any update
0815.ru
0815.ru0clickemail.com
0815.ry
0815.su
0845.ru
0clickemail.com
0-mail.com
0wnd.net
0wnd.org
10mail.com
@pnc
pnc / observer.md
Last active September 9, 2023 23:32
Using Erlang observer/appmon remotely

Using OTP's observer (appmon replacement) remotely

$ ssh remote-host "epmd -names"
epmd: up and running on port 4369 with data:
name some_node at port 58769

Note the running on port for epmd itself and the port of the node you're interested in debugging. Reconnect to the remote host with these ports forwarded:

$ ssh -L 4369:localhost:4369 -L 58769:localhost:58769 remote-host
@juriansluiman
juriansluiman / benchmark-beanstalkd.pull.php
Created January 24, 2014 07:24
Benchmarks ran for comparing Beanstalkd and Gearman
<?php
include 'vendor/autoload.php';
$pheanstalk = new Pheanstalk_Pheanstalk('127.0.0.1');
$pheanstalk->useTube('default');
$n = 1000000;
$start = microtime(true);
@danmackinlay
danmackinlay / supervisord.sh
Created August 27, 2009 07:07
an init.d script for supervisord
#! /bin/sh
### BEGIN INIT INFO
# Provides: supervisord
# Required-Start: $remote_fs
# Required-Stop: $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Example initscript
# Description: This file should be used to construct scripts to be
# placed in /etc/init.d.