I'm working with an API that allows a maximum of 4 requests per second and 8 concurrent requests, which really isn't sufficient for larger scale operations. To manage this, I've implemented a caching strategy where I:
1. Check a cache table using the exact query (ignoring case and whitespace).
2. If the cache contains the result, I return that.
3. If not, I query the API, save the fresh result to the cache, and return it.
Since cached data can become outdated after a certain period, I hit the API again to refresh the data once the cache is older than X days. I'm looking for feedback - is there a better approach to caching results? Should my cache be structured around unique queries? Any insights on this would be appreciated!
5 Answers
It sounds like you're on the right track, but keep in mind what will happen if you hit the rate limit. If you have 10 unique requests coming in at once, you'll exceed that 4 requests per second limit. Implementing a request queue could help manage the load and prevent hitting those limits.
Managing stale data is crucial! One approach I've taken is checking the API's last updated timestamp and conditionally making fresh calls based on a defined interval. If the data doesn't change frequently, a longer recheck interval could be beneficial to reduce load.
It's great that you're implementing caching! A couple of things to think about: does the API impose any restrictions on how long you can cache the data? Legally, they might have guidelines you need to follow, even if it's not a physical restriction. It's always best to check their documentation just to be safe!
It sounds like you've got a good caching plan! If your API parameters vary greatly, you might want to consider a more sophisticated caching strategy to avoid reaching the limits. Also, thinking about an asynchronous cleanup for older entries in your database can keep things streamlined.
Using a cache key based on the input is definitely the way to go! Make sure it incorporates the URL or any relevant parameters. Consider setting an expiration time for your cache entries, which most languages offer through various packages. That way, you'll keep your data fresh without too much hassle.
Yeah, I plan on doing that! Better to stay ahead of stale data.

Good point! The API actually recommends caching, so I think I'm in the clear there.