简体   繁体   English

为什么 Flask 限速方案使用 Redis?

[英]Why do Flask rate limiting solutions use Redis?

I want to rate limit my Flask API.我想对我的 Flask API 进行速率限制。 I found 2 solutions.我找到了 2 个解决方案。

  1. The Flask-Limiter extension. Flask-Limiter扩展。
  2. A snippet from the Flask website using Redis: http://flask.pocoo.org/snippets/70/使用 Redis 的 Flask 网站的片段: http : //flask.pocoo.org/snippets/70/

What is the significance of Redis when Flask-Limiter is able to rate limit the request on the basis of remote address without Redis?当 Flask-Limiter 能够在没有 Redis 的情况下基于远程地址对请求进行速率限制时,Redis 的意义是什么?

Redis allows you to store the rate-limiting state in a persistent store. Redis 允许您将限速状态存储在持久存储中。

This means you can:这意味着您可以:

  1. Restart your web server or web application and still have the rate-limitation work.重新启动您的 Web 服务器或 Web 应用程序,仍然可以进行速率限制工作。 You won't lose the records of the last requests made because of the working process being destroyed and a new one being created, instead.您不会因为工作进程被破坏并创建一个新进程而丢失上次请求的记录。
  2. Use multiple web servers or web applications.使用多个 Web 服务器或 Web 应用程序。 This is because the rate-limitation state is stored in an external data store that also solves the issue of shared data synchronisation and data races.这是因为速率限制状态存储在外部数据存储中,这也解决了共享数据同步和数据竞争的问题。 You can run as many web servers as you wish - the rate-limitation is shared among all of them.您可以根据需要运行任意数量的 Web 服务器——速率限制在所有服务器之间共享。
  3. Look at the rate-limitation state.查看速率限制状态。 Redis offers easy CLI tools that allow you to look at the current active data in an ad-hoc manner, even MONITORing the incoming commands and requests. Redis 提供了简单的 CLI 工具,允许您以特别的方式查看当前的活动数据,甚至可以监控传入的命令和请求。
  4. Let Redis manage TTL, LRU etc for rate-limitation algorithms.让Redis管理限速算法的TTL、LRU等。 Redis supports this intrinsically. Redis 本质上支持这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM