Back-end/Spring-핵심& webMVC

Rate Limiting Mechanism

prden 2023. 8. 31. 21:17

Spring Boot REST API to prevent users from making too many requests within a short period of time

서버가 제공할 수 있는 자원에는 제한이 있기 때문에 안정적으로 서비스를 제공하기 위해 예를 들어api 호출횟수를 1분당 60회로 제한하고, 60회를 넘어서는 순간 요청을 처리하지 않고 리턴한다. 이를 Throttling이라고도 한다.

 

Rate Limit가 필요한 사례

1. 서비스의 안정성 및 성능보장 : 서버가 다운되는 등의 사고를 미연에 방지하기 위해

2. 서비스의 가용성 확보 : 과도한 트래픽으로부터 서비스를 보호

3. 보안 : 로그인, 프로모션 코드와 같이 보안적으로 중요한 기능을 brute force attck으로부터 보호한다.

4. 운영 비용 관리 : 트래픽이 몰릴 때 auto scaling 하지 않고 scale의 virtual cap을 두고 제한

5. 특정 클라이언트가 서버의 자원을 독점하지 못하도록 

6. rate에 따라 요금을 다르게 부과하는 비즈니스 모델 활용 가능.

1. Spring Boot에서 Interceptor로 등록해서 해결

@Component
public class RateLimitFilter extends OncePerRequestFilter {
    private final IpAddrUtils ipAddrUtils;

    private Map<String, Long> requestTimestamps = new ConcurrentHashMap<>();
    private static final long RATE_LIMIT_INTERVAL = 60000; // 1 minute
    private static final int MAX_REQUESTS_PER_INTERVAL = 5;

    public RateLimitFilter(IpAddrUtils ipAddrUtils) {
        this.ipAddrUtils = ipAddrUtils;
    }

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {

        String ipAddress = ipAddrUtils.getClientIP(request);

        long currentTime = System.currentTimeMillis();
        if (requestTimestamps.containsKey(ipAddress)) {
            long lastRequestTime = requestTimestamps.get(ipAddress);
            if (currentTime - lastRequestTime < RATE_LIMIT_INTERVAL) {
                response.setStatus(HttpStatus.TOO_MANY_REQUESTS.value());
                return;
            }
        }

        // Update the timestamp for this request
        requestTimestamps.put(ipAddress, currentTime);

        filterChain.doFilter(request, response);
    }

}

2. Guava RateLimiter  라이브러리 사용하기 

 

3. Distributed Rate Limiting

application이 분산환경에서 운영중일 때 분산된 rate limiting 처리하기 위해 Redis나 memcached 사용하기

 

https://etloveguitar.tistory.com/126

 

[Rate Limit - step 1] Rate Limit이란? (소개, Throttling, 구현시 주의사항 등)

필자는 현재 API서버를 열심히 개발 하고 있다. 개발한 서비스가 트래픽을 많이 받다보니, 트래픽을 적절히 제한하지 않았을 때 장애가 발생했고, 그에 따라 API의 Rate Limit을 구현해야 했다. 공부

etloveguitar.tistory.com