How to load data from Recurly to Redshift
Extract your data from Recurly
The Recurly API allows applications to interact directly with its platform. In general, Recurly offers the following ways of integrating with their services:
- Hosted Payment Pages. With this product, merchants can start immediately subscribing to their customers with minimal effort and without requiring significant technical skills.
- js. A JavaScript library that can be used to integrate the functionality of Recurly into your product securely.
- API. A Web API that exposes the full functionality of Recurly through its interface for integrating with it programmatically.
From the above three methods, we are interested in the last one as it is the only way that we can use to pull data from the Recurly platform. There’s also a plethora of SDKs so you can interact with the API from your language or framework of choice:
- PHP (official)
- Ruby (official)
- Python (official)
- .NET (official)
- Recurly SDK iOS (official)
- Android (official)
- Java (unofficial)
- Go (unofficial)
- Node.js (unofficial)
The Recurly API is a RESTful web service. As a RESTful API, interacting with it can also be achieved by using tools like CURL or Postman or Apirise or by using http clients for your favorite language or framework, instead of one of the previously mentioned SDKs. A few suggestions:
- Apache HttpClient for Java
- Spray-client for Scala
- Hyper for Rust
- Ruby rest-client
- Python http-client
Recurly API Authentication
Recurly is using HTTP Basic Authentication using your API Key for credentials. All data are transferred over a secure SSL channel. Below an example using Curl that demonstrates how authentications work for Recurly:
JAVASCRIPT
curl -H 'Accept: application/xml' \-H 'X-Api-Version: 2.1' \-H 'Content-Type: application/xml; charset=utf-8' \-u [API Key]: https://[subdomain].recurly.com/v2/accounts
Recurly supports the use of multiple Private API keys, which can be used to integrate third-party services using unique, controlled credentials. With the following limitations:
- Core & grandfathered Recurly plans are granted 5 private API keys.
- Professional plan grants 10 API keys.
Rate Limits
By default, new Recurly sites have the following API rate limits:
- Sandbox sites: 400 requests/min. All requests count towards the rate limit.
- Production sites: 1,000 requests/min. Only GET requests to count towards the rate limit.
Once your site moves into production mode, Recurly will only rate limit GET requests. New subscriptions, account modifications, and other requests using POST PUT or DELETE methods will not count against your rate limit.
The rate limit is calculated over a sliding 5-minute window. This means a production site could make 4,000 requests within one minute and not hit the rate limit so long as the site made less than 1,000 requests during the prior 4 minutes.
If an API request exceeds the rate limit, the API returns a 429 status code indicating Too Many Requests.
Prepare your Recurly Data for Amazon Redshift
Amazon Redshift is built around industry-standard SQL with added functionality to manage very large datasets and high-performance analysis. So, to load your data into it, you will have to follow its data model, a typical relational database model. The data you extract from your data source should be mapped into tables and columns where you can consider the table as a map to the resource you want to store and columns the resource attributes. Also, each attribute should adhere to the datatypes that are supported by Redshift. Currently, the datatypes that are supported are the following:
- SMALLINT
- INTEGER
- BIGINT
- DECIMAL
- REAL
- DOUBLE PRECISION
- BOOLEAN
- CHAR
- VARCHAR
- DATE
- TIMESTAMP
As your data are probably coming in a representation like JSON that supports a much smaller range of data types you have to be really careful about what data you feed into Redshift and make sure that you have mapped your types into one of the datatypes that is supported by Redshift. Designing a Schema for Redshift and mapping the data from your data source to it is a process that you should take seriously as it can both affect the performance of your cluster and the questions that you can answer. It’s always a good idea to have in your mind the best practices that Amazon has published regarding the design of a Redshift database. When you have concluded on the design of your database you need to load your data on one of the data sources that are supported as input by Redshift, these are the following:
Load data from Recurly to Redshift
The first step to load your Recurly data to Redshift is to put them in a source that Redshift can pull it from. As it was mentioned earlier there are three main data sources supported, Amazon S3, Amazon DynamoDB, and Amazon Kinesis Firehose, with Firehose being the most recent addition as a way to insert data into Redshift.
To upload your data to Amazon S3, you will have to use the AWS REST API. As we see again APIs play an important role in both the extraction but also the loading of data into our data warehouse. The first task that you have to perform is to create a bucket. You do that by executing an HTTP PUT on the Amazon AWS REST API endpoints for S3. You can do this by using a tool like CURL or Postman or Apirise. Or use the libraries provided by Amazon for your favorite language. You can find more information by reading the API reference for the Bucket operations on Amazon AWS documentation.
After you have created your bucket you can start sending your data to Amazon S3, using again the same AWS REST API but by using the endpoints for Object operations. As in the Bucket case, you can either access the HTTP endpoints directly or use the library of your preference.
DynamoDB imports data again from S3. It adds another step between S3 and Amazon Redshift so if you don’t need it for other reasons, you can avoid it.
Amazon Kinesis Firehose is the latest addition as a way to insert data into Redshift and offers a real-time streaming approach to data importing. The necessary steps for adding data to Redshift through Kinesis Firehose are the following:
- create a delivery stream
- add data to the stream
Whenever you add new data to the stream, Kinesis takes care of adding this data to S3 or Redshift, again going through S3, in this case, is redundant if your goal is to move your data to Redshift. The execution of the previous two steps can be performed either through the REST API or through your favorite library just as in the previous two cases. The difference here is that for pushing your data into the stream you’ll be using a Kinesis Agent.
Amazon Redshift supports two methods for loading data into it. The first one is by invoking an INSERT command. You can connect to your Amazon Redshift instance with your client, using either a JDBC or ODBC connection and then you perform an INSERT command for your data.
JAVASCRIPT
insert into category_stage values(12, 'Concerts', 'Comedy', 'All stand-up comedy performances');
The way you invoke the INSERT command is the same as you would do with any other SQL database. For more information, you can check the INSERT examples page on the Amazon Redshift documentation.
Redshift is not designed for INSERT-like operations. On the contrary, the most efficient way of loading data into it is by doing bulk uploads using a COPY command. You can perform a COPY command for data that lives as flat files on S3 or from an Amazon DynamoDB table. When you perform COPY commands, Redshift can read multiple files simultaneously, and it automatically distributes the workload to the cluster nodes and performs the load in parallel. COPY is quite flexible as a command and allows for many different ways of using it, depending on your use case. Performing a COPY on amazon S3 is as simple as the following command:
JAVASCRIPT
copy listingfrom 's3://mybucket/data/listing/'credentials 'aws_access_key_id=;aws_secret_access_key=';
For more examples of invoking a COPY command, you can check the COPY examples page on Amazon Redshift documentation. As in the INSERT case, the way to perform the COPY command is by connecting to your Amazon Redshift instance using a JDBC or ODBC connection and then invoke the commands you want using the SQL Reference from Amazon Redshift documentation.
The best way to load data from Reculry to Amazon Redshift and possible alternatives
So far, we just scraped the surface of what can be done with Amazon Redshift and how to load data into it. The way to proceed relies heavily on the data you want to load, from which service they are coming from, and the requirements of your use case. Things can get even more complicated if you want to integrate data coming from different sources. A possible alternative, instead of writing, hosting, and maintaining a flexible data infrastructure, is to use a product like RudderStack that can handle this kind of problem automatically for you.
RudderStack integrates with multiple sources or services like databases, CRM, email campaigns, analytics, and more.