Approaching new data sources for ingestion provides a wealth of freedom in how we tackle them. I’m fond of iterative design from the trenches, but for complex APIs it can be the wrong move. I find it useful to take a step back and answer these 3 questions before designing the right approach.
Recently I’ve been toying with the API for Octopus Energy and like most APIs it had its quirks when you get under its skin, so I’ll show how these questions were answered in that context.
Access
How can I access and keep the credentials secure?
How we access the API is crucial, and a core part of that is authentication. API keys are common, typically using an Authorization header. There’s also OAuth which can add some extra legwork with handshakes. Others will stick to Basic authentication with a username and password combination.
How this authentication is implemented can limit potential solutions. We want a solution which is secure, and part of that means securing credentials securely too. For example this could be using a Key Vault, or using the Secure Input settings in a pipeline.
Access to the API also extends to usage limits and error codes. These can change how hard we drive the API such as parallelism, or how we respond to errors, like aborting for authentication issues, or retrying when throttled.
Example: In the case of Octopus, they use an API key and Basic authentication. However it’s notable the key is the username, not the password. In a Data Factory pipeline, we could parameterise the Web activity to achieve this. In Fabric, connection credentials aren’t parameterised in the same way, so a more natural fit could be securing the API key in a Key Vault and retrieving it via a Notebook to make the calls.
Authentication isn’t only about how we secure a connection to the API, but also securing the credentials in our solution.
Traversal
How do I call the endpoints to build a complete dataset?
Some APIs are straightforward – call an endpoint and retrieve a complete dataset, say a product listing. In other instances there are multiple requests required to achieve a complete result – traversing the API.
Here are some examples:
- Authentication may require a call prior to accessing desired endpoints
- Calls to multiple endpoints, for example one call to retrieve customers, followed by a separate call per customer for specific details
- Calls to the same endpoint if paging is used, usually having part of the response indicating another page is available
Understanding the journey through the API shapes the solution. Some portions may be prerequisites (like authentication), or repeatable (iterative, like paging) and need to be factored in accordingly. If you’re paging for example, a Dataflow may not be suitable due to lack of iteration.
Example: For the Octopus API, they have a combination of these. Access to consumption endpoints requires a serial number for the meter. I’d need to first query the account endpoint to retrieve a list of meters, and then use those to query subsequent endpoints. Next, the consumption data is transactional and granular, so although you can request a broad time period, the data will be paged and returned with next and previous attributes to walk through the paging.
The journey we take through the API directly affects our design choices. Beyond the dependencies and iteration we’ve covered, it can also impact performance, such as our ability to parallelise workloads.
Watermarking
How do I retrieve just what I need from a large dataset?
If we’re looking at a large volume of data – typically transactional – then I only want to receive the limited subset of new or changed records. This reduces workload on both the API and my ingestion process. Watermarking is how we determine the point our data runs to, and where to resume from on next execution.
The implementation of this can vary – sometimes a single parameter might indicate a reporting period, or more commonly a pair defining the start / end range which we want data for.
The two most common approaches to implement this are:
- Data is retrieved incrementally based on a watermark, and after successful processing the watermark is updated
- The process simply refreshes the last X days of data to remain up to date, filling any potentially recent retrieval gaps, at the expense of processing older changes which have already been seen and processed a few times
Backfilling historical data is a consideration. The first approach lends itself to incremental backfilling utilising the watermark to crawl the data. The second approach tends to be a more manual / bulk backfill as the process only maintains the head of the dataset. If you’re dealing with hard-deletes, lean towards the second approach as it allows you to identify records within the refresh window which were in the dataset previously, and are now gone.
The goal we want to arrive at with watermarking is understanding when and where to advance the watermark to. This should be when we know a dataset is complete, and indicate where the next run should start. In most cases I’d base this on the latest data retrieved from the API as opposed to the latest period queried as there may be latency in the underlying data.
Example: Octopus use period start and end date parameters to limit consumption data. Coupled with the paging above, this ensures we can retrieve only the subset we need. Once all pages are retrieved the watermark will be advanced to the latest consumption interval, and as each meter is retrieved separately, they’ll each require their own watermark.
When approaching a fresh API we need to understand how it operates to design effective ingestion into a bronze layer.
The API documentation will usually cover these comprehensively, but some will have nuances to challenge us. I’ve had systems that don’t track changes and ended up hand-rolling a delta process to optimise the load. They’re the exciting ones which aren’t just cookie cutter.
I still prefer to get my hands dirty with APIs, but one wrinkle you weren’t expecting can completely throw out your design. Ask these 3 questions before building, and you’ll start with more of a design than an idea.