GOWRI SANKAR A S
Sep 1, 2018 · 1 min read

Great article! thanks for sharing

I was thinking of an approach like this. it could be bad or lead to race conditions; let me know your thoughts, please.

incr mykey-> returns 1 for the first time else the incremented value

expire mykey 120 -> set the key to live during our window, if the value returned above is 1. This could be done asyncly so as not to block the main requesting thread.

if the returned value is > ratelimit, throw an error. We don’t have to bother that the mykeyhas gone over the limit, as long as it’s crossed the limit. After the window is over, the key would’ve been removed by Redis. Then start over by incr the key as if it existed but it will return 1 indicating it didn’t exist before this op. And this keeps happening.