Limit number of calls to RESTful service

谁说胖子不能爱 提交于 2021-02-08 10:53:28

问题


we have a RESTful service deployed on multiple nodes and we want to limit the number of calls coming to our service from each client with different quota for each client per minute. our stack : Jboss application server, Java/Spring RESTful service.

What cloud be the possible technique to implement this?


回答1:


Sometimes ago I read a good article where the same theme was highlighted. The idea is to move this logic into load balancing proxy and here some good reasons to do it:

  1. Eliminates technical debt - If you’ve got rate limiting logic coupled in with app logic, you’ve got technical debt you don’t need. You can lift and shift that debt

  2. Efficiency gains - You’re offloading logic upstream, which means all your compute resources are dedicated to compute. You can better predict

  3. Security - It’s well understood that application layer (request-response) attacks are on the rise, including denial of service. By leveraging an upstream proxy with greater capacity for connections you can stop those attacks in their tracks, because they never get anywhere near the actual server.




回答2:


If the only way to access your API is through a UI client which you manages , then you can add a check on the client code (javascript in case of web app) to make a call only when the limit is not crossed by that user. Else there is no way, since a user can always access your API and the only thing at the server level which you can do is to check whether to send an error or valid result as a part of API response.




回答3:


To limit the stack, it means you need to keep state, at least based on some specific client identification. This may require you to maintain a central counter e.g. db (cassandra) which can allow you to look up the current request count per minute, and then within a java filter, you can restrict request counts as necessary.

Or if you can track the client's session, then you can track and then use sticky session, enforcing clients to use specific node for the duration of the client session, and hence you can simply track within a java filter, the number of requests per client, and send 503 code or something more relevant.



来源:https://stackoverflow.com/questions/38995643/limit-number-of-calls-to-restful-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!