I'm currently working on a project that involves implementing a frequency cap for user actions, and I'm facing some performance issues with my current solution. I would greatly appreciate your expertise and suggestions on how to optimize it. Here's an excerpt of my current code:
auto keyName = "cap:user_id";
increment.push_back(Redis::Command(
"INCR",
keyName
));
increment.push_back(Redis::Command(
"EXPIRE",
keyName,
"86400",
"NX"
));
Redis::Results results = redis.execMulti(increment);
Then, before every action, I perform a Redis GET on the key and check how many times that user ID performed the action. To provide a bit of context, I'm using C++ for this project, and I initially chose Redis for managing the distributed counter since it's a reliable tool. However, I've observed that the performance is not meeting the desired standards for our frequency cap code. Specifically, the counter updates are slower than expected, but also the GETs.
Given the constraints, I'm exploring alternative in-memory data structures that might offer better performance for maintaining a distributed counter. Or maybe is there a service faster than Redis for this type of thing? The goal is to efficiently count how many times a specific user ID performs a particular action. My current code runs in 2 threads, but could be more.
Since your work is in the read (GET frequency), compute (if not reaching the limit, increment it), write (update the counter and set expiration) pattern, you should use Lua scripting to avoid RTT (Round Trip Time).
Disclamer: I'm the author of redis-plus-plus