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.

@fisheuler
fisheuler / latency.txt
Created July 16, 2022 08:05 — forked from jboner/latency.txt
Latency Numbers Every Programmer Should Know
Latency Comparison Numbers (~2012)
----------------------------------
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns 14x L1 cache
Mutex lock/unlock 25 ns
Main memory reference 100 ns 20x L2 cache, 200x L1 cache
Compress 1K bytes with Zippy 3,000 ns 3 us
Send 1K bytes over 1 Gbps network 10,000 ns 10 us
Read 4K randomly from SSD* 150,000 ns 150 us ~1GB/sec SSD
游戏的白名单数据查询中,需要从后端数据库中load数据至redis缓存。
白名单数据在redis中的数据格式是 key:value对。
key的拼接规则是: 公共前缀(MOBILE_ASSISTANT_PACKAGE_LIST_INFO_)+包名
value的规则是:json串,包括三个字段gameid,packageName,gameName,如果字段为空值的话,则不保存此字段。
@fisheuler
fisheuler / ub-eppoll
Last active August 29, 2015 14:07
ub eppoll 模型分析
1.
通过pstack命令可以打印出当前进程的堆栈信息,这是其中一个进程的堆栈信息:
112 #0 0x00000034e700e54d in read () from /lib64/libpthread.so.0
113 #1 0x00007fdc5db90ad0 in redisBufferRead () from /home/work/lib/libhiredis.so
114 #2 0x00007fdc5db90d88 in redisGetReply () from /home/work/lib/libhiredis.so
115 #3 0x00007fdc5db90f35 in redisvCommand () from /home/work/lib/libhiredis.so
116 #4 0x00000000027737b2 in RedisConn::runCommandQuery(char const*, ...) ()
117 #5 0x00000000027967f5 in DuokooSession::getUserInfoByUserId(long, UserInfo&) ()
1.
分布式锁服务,是在系统的某个endpoint提供一个供程序沟通和协同的方法。
具体实现方法,可以在redis中来实现,也可以用zookeeper来做。
@fisheuler
fisheuler / haystack
Last active August 29, 2015 14:02
HayStack notes
facebook 用于存储图片的系统
主要设计点
1.
系统主要分为三块
Directory(相当于manager,维护图片获取url地址到物理地址的映射,以及对于物理机器的管理和维护)
cache 层(负责缓存图片数据)
1. 某机器的tcp状态统计图
TIME_WAIT 5002
FIN_WAIT1 449
FIN_WAIT2 5786
ESTABLISHED 2609
CLOSING 5
LAST_ACK 81
LISTEN 1
1
线上程序,某日报警一晚上。报打开文件数目过多。
在测试机上跑。
用系统监控命令可以看到一些端倪。
用pstree命令可以看该jvm进程跑一段时间之后竟然开启了上万个线程。
pstree -p 可以显示进程相应的进程号以及调用关系。
1. from man pages
sendfile
sendfile copies data between one file descriptor and another. Because this copying is done within
the kernel, sendfile is more efficient than the combination of read and write, which would require
transferring data to and from user space.
1
TServer :抽象的server基类。
1.1
AbstractNonblockingServer 继承TServer的非阻塞基类 。
在此类里定义了一个AbstractSelectThread 类,此类是基于reactor模型的实现,在单线程里实现整套处理逻辑。