4

RavenDB 5.3 Features: Incremental time series, throttling and rate limits

 2 years ago
source link: https://ayende.com/blog/195297-C/ravendb-5-3-features-incremental-time-series-throttling-and-rate-limits?Key=102b4eb8-ea20-40bb-bd3d-74a2f19a4198
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Nov 16 2021

RavenDB 5.3 Features: Incremental time series, throttling and rate limits

time to read 2 min | 253 words

About a year and a half ago Kamran wrote an article showing how you can implement rate limits in RavenDB. He used both counters and expiration to handle this scenario. I think that incremental time series make it so much easier to work with, it’s not even funny.

Consider the following code:

using var session = GetDocumentStore().OpenAsyncSession(); var now = DateTime.UtcNow;

var result = await session.Query<Watch>() .Where(x => x.Id == "api/stock-exchange") .Select(w => RavenQuery.TimeSeries(w, "INC:Usage", now.AddSeconds(-30), now) // past 30 seconds .GroupBy(x => x.Days(1)) // only single result .Select(x => x.Sum()) .ToList() ).SingleOrDefaultAsync(); if (result?.Results[0]?.Sum[0] > 50) { throw new RateLimitedExceededException("More than 50 requests in the last 30 seconds, try again later"); }

// do work here....

session.IncrementalTimeSeriesFor("api/stock-exchange","INC:Usage") .Increment(1); await session.SaveChangesAsync();

Those are ~20 lines of code and that is all you need to handle a sliding window for rate limits. This will work quite nicely even with high levels of concurrency, you can also establish more interesting scenario, such as maximum 50 requests in the past 30 seconds, but also maximum of 200 requests in the past 5 minutes, etc. If you ever tried to make sense of the way Let’s Encrypt is handling rate limits, for example, you can see how you can get some really crazy complexity.

That is now all handled for you. We have the part where we record the number of operations under the limit, and the rest is limited only by your imagination.

For fun, you can now decide what you want to do with this data. If you are only interested in that for rate limiting purposes, you can tell RavenDB to just delete the old data once it is no longer of use using time series retention. You can also just roll them up to a 5 minute boundary and keep that information (for example, monitoring, audits and billing purposes come to mind).

The whole thing is far more cohesive and easier to work with.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK