We are using this gem at ShippingEasy to manage rate limiting API endpoints. It works great, but while reviewing the source I found a few areas where its performance could be improved:
evalsha should be preferred to eval, since it reduces script compilation and network overhead (490b7be28138cbf207828b324646648263e68601 and 6474c7aace4a411f7e5159a1a3e490fbb4b1feb9)
independent adjacent calls to redis should be pipelined to avoid multiple round trips (035d6ed40a52a38aa74bb18c011ee85012ee4a2b)
A combination of SETNX and PEXPIRE is equivalent to and can be replaced with a single SET call with NX and PX options (722c516cdc01b067a3ba4083be0e1207fbb0587f). As a beneficial side effect, not only does this perform better but it turns set_if_not_exists_with_ttl into an atomic operation.
We are using this gem at ShippingEasy to manage rate limiting API endpoints. It works great, but while reviewing the source I found a few areas where its performance could be improved:
evalsha
should be preferred toeval
, since it reduces script compilation and network overhead (490b7be28138cbf207828b324646648263e68601 and 6474c7aace4a411f7e5159a1a3e490fbb4b1feb9)SETNX
andPEXPIRE
is equivalent to and can be replaced with a singleSET
call withNX
andPX
options (722c516cdc01b067a3ba4083be0e1207fbb0587f). As a beneficial side effect, not only does this perform better but it turnsset_if_not_exists_with_ttl
into an atomic operation.