One thing I had to figure out when building my API product was how to track API usage per user efficiently. I wanted a way to:
Track how many requests each user makes daily
Update their usage limits without slowing down API responses
Have a reliable way to bill based on usage
My Setup: Redis + A Daily Job
After trying a few options, I went with Redis for tracking real-time API usage and a daily job that updates the database. Why Redis? It’s fast, easy to set up, and doesn’t add latency to API requests. (You also pay as you gtow)
Here’s how it works:
Each API request increments a Redis key → usage:{date}:{projectId}
I check the usage before processing a request → If they hit their limit, I block them.
A daily job runs → It grabs all usage data from Redis, updates MongoDB (I use AWS lambda with event scheduler).
The Daily Job in Action
Every 24 hours, the job:
Fetches all usage records stored in Redis for the previous day.
Maps them to projects in my database.
Updates each project’s monthly and total usage stats.
If anything fails, I retry it (max 3 times :) )
Other Ways to Track API Usage
Redis works great for me, but there are other ways to do this:
Cloudflare Analytics – Logs all requests, and you can aggregate them for billing.
A Time-Series Database – Like TimescaleDB or InfluxDB, which is great if you need detailed usage tracking over time.
Directly in MongoDB/Postgres – Just log each request, but that can get slow if traffic is high.
For now, Redis + a simple job is working well for my needs, keeping things fast and efficient. 🚀
If you’re curious about the API product I built, you can check it out here: CaptureKit :)
How do you track API usage? Would like to know how others implement it :)
Top comments (0)