Reading the title, I was expecting to see a somewhat shady article on using multiple servers to dodge API rate limits.<p>Instead, they've got a pretty snazzy writeup of how to effectively keep your distributed processes working together properly. I'm curious why they didn't pass a token rather than the actual Lua over the wire via Redis, but it certainly seems to work for them.
I may be missing something but I think this could be done more cleanly with an atomic counter or semaphore [1] in Redis.<p><pre><code> 1. Have a task that releases 1 resource to the semaphore
every (1.0 / cps) seconds.
2. Workers wait to acquire a resource from the semaphore
before making a call.
</code></pre>
Due to the rate of release being fixed at 1/cps no worker can ever exceed the calls-per-second limit. Much simpler than sending Lua over the wire.<p>[1] <a href="https://github.com/dv/redis-semaphore" rel="nofollow">https://github.com/dv/redis-semaphore</a>
Interestingly enough, Alan Shreve gave a talk at RedisConf a couple of years back detailing something very similar used on the twilio stack to rate limit draining a queue: <a href="https://inconshreveable.com/talks.html" rel="nofollow">https://inconshreveable.com/talks.html</a>
So they send code to Redis to make sure that they won't increment too high? I know Redis already has a ton of functions prepackaged but this seems like it would be a good one: INCRIF.