Service flood / denial of service protection with scala and Google Guava

A common problem for webservices that are exposed to the public is the threat of beeing flooded or atttacked by a denial of service attack. To be honest I think that especially for distributed dos there is no real protection and even for dos the protection should be mainly in components like load balancers, firewalls, … and not in the application itself. But anyway it is useful to be able to handle that also in the application and at least that also provides a good way to restrict access to certain service endpoints in a given amount of time.

This solution is based on Google Guava RateLimiter and therefore works only within one application server instance. If you need such a solution of multiple instances Hazelcast or Redis might be good solutions for you. Both have an easy to use TTL feature.

Anyway here is my solution for a trait that can be easly mixed in any service class:

maven dependency:

scala code:




GD Star Rating