rooftopcms / rooftop-cms

Rooftop CMS is an API-first WordPress CMS for developers and content creators
http://www.rooftopcms.com
GNU General Public License v3.0
220 stars 23 forks source link

API accounting proposal #23

Closed edtjones closed 7 years ago

edtjones commented 8 years ago

Here's my plan for API request accounting:

Overview

We'll use Nginx and Lua to increment a counter in Redis, synchronously with the request. It's very fast.

Data structure in Redis

There are various options for data structures. If we just want this month's usage, that's pretty simple:

Simple counter

key: api_accounting:[sitename].rooftopcms.io:[api_key]
value: [int]

You can INCR on the value to keep the counter up to date.

Sorted set for timeseries

We could use sorted sets to store timeseries data:

key: api_accounting:[sitename].rooftopcms.io:[api_key]
score: [timestamp]
value: {
   timestamp: [timestamp,
   count: 1
}

We don't need to increment the counter; a new member is added to the sorted set for each request. Querying ranges will be very inexpensive because the timestamp means we already have the data date-sorted. I don't know how to SUM the count, though.

Displaying the counters

In its simplest form, we could just show the current month's counters against the API key. It would be nice to poll the count periodically and raise an alert (webhook and/or email) if it went over a predefined threshold.

Using the nested set approach, if we can cheaply sum, we could graph requests over time.

edtjones commented 7 years ago

We've got Nginx and some Lua doing the accounting into Redis.