liberapay / liberapay.com

Source code of the recurrent donations platform Liberapay
https://liberapay.com/
1.64k stars 207 forks source link

Rate limiting #658

Open Changaco opened 7 years ago

Changaco commented 7 years ago

In order to fix all throttling issues I've been reading up on rate-limiting. Here are the best articles I've found on the subject:

There are various modules on PyPI for rate-limiting and throttling, but there doesn't seem to be any that fits our technology stack. The only data store we have is our PostgreSQL DB, and that's enough to implement rate-limiting in a satisfactory way, we neither need nor want to set up Redis or Memcached.

So, what should be done:

Changaco commented 7 years ago

https://github.com/alisaifee/limits seems to be the project most likely to accept a pull request for PostgreSQL support.

Changaco commented 7 years ago

For the actual implementation I think a leaky/token bucket is okay, because with PostgreSQL we have transactions for atomic operations, and trying to avoid locks is pointless since every row update requires a lock.

However Cloudflare's algorithm is also worth considering. I'm not sure whether it would be more efficient than a bucket when implemented atop PostgreSQL.

Changaco commented 7 years ago

I've tried to implement both approaches, the leaky bucket is simpler and doesn't seem less efficient.