Lab#34 Redis rate limiter in the gateway and in accounts service.
Step#1 We will implement the redis rate limiter in the gateway. Add the dependency in the pom for the gateway.
| Gateway: pom.xml | |
|---|---|
40 41 42 43 | |
Step#2 Then we need to add two beans to the GatewayserverApplication class. We create a KeyResolver base on the user in the request header and if no user found default to anonymous. The other beans creates a RedisRateLimiter witharguments defaultReplenishRate, defaultBurstCapacity and defaultRequestedTokens. With values 1,1,1 the user can make 1 request per second.
| Gateway: GatewayserverApplication.java | |
|---|---|
46 47 48 49 50 51 52 53 54 55 | |
Step#3 Update the filters in the gateway for loans to add the redis rate limiter
39 40 41 42 43 44 | |
Step#4 Start redis as a docker container.
docker run -p 6379:6379 --name tusbank redis

Figure 1. Redis as Docker Container
Now gatewayserver needs the connection details for the redis server.
| Gateway: application.yml | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | |
Restart the gateway server.
Step#5 See link on installing apache bench
https://dev.to/gabriellaamah/load-testing-for-api-with-apache-benchmark-on-windows-58oj
On windows download and unzip.

Figure 2. Apache Benchmark

Figure 3. ab command

Figure 4. Output 1

Figure 5. Output 2

Figure 6. Output 2 404
ab -n 10 -c 2 -v 3 http://localhost:8072/tusbank/cards/api/cards/contact-info
Step#6 Now update accounts microservice for the get java-version endpointwith a fallback.
| Accounts: AccountsController.java | |
|---|---|
51 52 53 54 55 56 57 58 59 | |
And in the application.yml. This is one request for every 5 seconds.
resilience4j.circuitbreaker: # lab 31
configs:
default:
# slidingWindowSize: 10
# permittedNumberOfCallsInHalfOpenState: 2
# failureRateThreshold: 50
# waitDurationInOpenState: 10000 # 10 seconds
timeoutDuration: 1000 # Lab 34
limitRefreshPeriod: 5000 # Lab 34
limitForPeriod: 1 # Lab 34
To avoid timeout in the circuit breaker I commented out the circuitbreaker in the gateway
| Gateway: GatewayserverApplication.java | |
|---|---|
26 27 28 29 30 31 32 | |
Restart the accounts microservice and the gateway
http://localhost:8072/tusbank/accounts/api/java-version

Figure 8. Default Response
Refresh multiple times to see fallback message

Figure 8. Rate limiter fallback