What Is Dynamic Sampling and How It Works
Sometimes you may want to limit the amount of analytics data coming into your API management program. Learn to optimize your data with dynamic sampling!
Join the DZone community and get the full member experience.
Join For FreeSometimes you may want to limit the amount of analytics data coming into your API management tool. This could be because you want to exclude specific traffic, such as internal or health check traffic, or you may want to reduce unnecessary data to control cost. Dynamic sampling does just this. Dynamic sampling lets you control which API calls are logged to your API management tool based on customer or API behavior. Intelligently extrapolate metrics for accurate reporting even with multiple sample rates so that no matter what rules or sample rates you have set up, you can be sure you are still seeing an accurate representation of your data.
Dynamic sampling is controlled by setting up sampling rules in the API management tool's dashboard. You can create rules based on criteria such as request path, response status codes, user or company behaviors, and more. You can also control the applied sample rate for each rule. The sample rate is the percentage of events that will make their way into your API management tool. For example, if you set a rule to sample 50% of events, your management tool will log half of the matching events intelligently (and not just log every other event to meet the 50% sampling rate).
Why Use Dynamic Sampling?
Dynamic sampling serves two use cases:
- Gives users the capability to trim down excessive or uninteresting calls populating while retaining the insight you gain from the platform.
- Allows users to reduce subscription costs and save money.
When Not to Use Dynamic Sampling
It would be best if you avoided dynamic sampling on specific events moving into your API management tool.
Dynamically sampling the number of API calls coming into your API management tool would impact a monetized API because it would fail to track the exact usage, likely missing some events depending on the sampling rate applied. This will affect your billing meters and the invoices being sent to customers. However, a way around this is to apply dynamic sampling to endpoints that are not monetized and let events counted within your Billing Meter sample at 100%.
Another reason not to use dynamic sampling is when your logged API calls are less than the number of calls your subscription plan supports. For example, suppose you are making fewer API calls than your subscription plan includes. In that case, dynamic sampling is unnecessary unless you are simply trying to exclude data you don’t want in your management tool to keep data cleaner. Examples of this may be not including traffic from health check probes or internal testing.
Four Types of Sample Rates
Dynamic sampling rules are categorized into four kinds of sample sets and are prioritized in the following manner:
- Sample rates for a single user or company
- Sample rates based on regex rules
- Sample rates based on user behaviors and demographics (Saved Cohorts)
- Sample rates applied globally
This means that if a sample rate is set for a specific customer, this will take priority over a sample rate associated with a behavioral cohort that a customer belongs to. Global sample rates have the lowest priority and are, by default, set to 100% for all customers. If an API call matches multiple rules, it gets processed by the first kind of sample rate in the prioritization hierarchy.
In conclusion, utilizing dynamic sampling for API products offers a smart and effective strategy to maximize the benefits of your API offerings. By adapting to evolving data requirements and usage patterns in real-time, dynamic sampling ensures resource efficiency, cost control, and responsive performance, all while maintaining data security and compliance. This approach not only enhances user experience but also positions your users to harness the full potential of your API in a sustainable and agile manner, making it a crucial tool for the success of modern API products.
Published at DZone with permission of Dylan Frankcom. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments