Luke
Rajlich.

No. 016

RedTrack – A system for scalable real-time analytics.

RedTrack, Toro's open-source system for near-real-time event loading into Redshift: architecture, loader and best practices.

Behind the metrics-based features at Toro is a system we used to collect a large volume of data, store it, and process it. This system is called RedTrack and it provides near real-time loading of data into RedShift. The project is publicly available on Github (here) and is licensed under under the MIT license.

Context

For some context, Toro provides a product that simplifies creating ad campaigns and tracks performance of those ad campaigns on a number of factors. There’s a large amount of data that the product has to collect and later use to display to users, for example, we track install rates and CPI over time and across a number of demographic segments:

toro-dashboard-sliced_1024

Requirements & Design Considerations

1) Provide near real-time query access for a large amount of incoming event data. As part of this requirement, we wanted to be able to produce events to the system in real time with little-to-no application performance penalty. In addition, we wanted the ability to query data from the warehouse intra-day, with tolerance for a bit of data lag.

  1. Provide a simple & flexible API that can support a wide variety of events. We also want an API that is flexible in order to support more event types in the future with minimal amounts of work. Adding additional event types should require as little re-configuration or provisioning of new resources as possible.

  2. Minimize ongoing support cost. Primarily, the motivation here was to reduce the development & operational overhead of running the system. The system is composed of managed AWS services, specifically Redshift, S3 and Kinesis, the cost of ongoing operation of the system is greatly reduced vs alternatives.

Architecture

At a high level, the architecture is inspired by the system built at Zynga for analytics, ZTrack. The core components of RedTrack are Api Client(RedTrack::Client), Data Broker(AWS Kinesis), Aggregator/Loader(RedTrack::Loader), Amazon S3, and Data Warehouse(AWS Redshift). Laid out, a high level system & data flow diagram looks like this:

RedTrack Data Flow (3)

Api Client – RedTrack::Client This is written in Ruby. The api client validates that the event produced by the application conforms to the Redshift schema and then writes it, as JSON, to Kinesis.

Data Broker – Kinesis Under the covers, Kinesis is a hosted Kafka fork [news.ycombinator]. Apache Kafka itself is a highly scalable distributed pub-sub messaging system. In terms of how RedTrack uses Kinesis, Redtrack expects is a single stream per Redshift table, which simplifies loader logic. Kinesis streams can have any number of shards and RedTrack will properly handle additional shards without any reconfiguration.

Aggregator/Loader – RedTrack::Loader Written in Ruby, this components determines the last event loaded from Kinesis (by sequence number) into Redshift, reads subsequent events from Kinesis, compresses data, uploads to S3, and issues a COPY command to Redshift. This implements a number of loading best practices which are detailed in a subsequent section. Performance was an important consideration and we performed a handful of tests around loading performance, by benchmarking loading

Amazon S3 The compressed files are uploaded from the host running the loader to S3 via a PUT command. RedTrack keeps a convention as to how it stores data in S3 – as files under s3://{bucket}/redtrack/{cluster}/{database}/{table}/{date}/{unique_file_name} The loader verifies the size of the file in S3 before doing a COPY into Redshift.

Data Warehouse – Redshift The core component of the system. Redshift is an MPP warehouse, managed by Amazon that can be accessed using a SQL interface (Postgres wire format, specifically). We decided to go with Redshift for several reasons: 1) the simplicity of accessing data via SQL (as opposed to a Hadoop/MapReduce), 2) its excellent query performance [1,2,3,4], 3) it’s a hosted solution, thus the ongoing support cost is less than alternative solutions, and 4) it has an attractive TCO [1].

Loader Details

The loader is an important component of the system that deserves some additional detail. The Loader is invoked as a batch job, typically you can run a cron job to kick it off or schedule a task with a job queue. The loader loads 1 table at a time and at a high level, it does the following:

Loader Flow (1)

Redtrack Best Practices

There are a number of best practices to consider when using RedTrack (and Redshift in general).

  1. Load data in sortkey order in order to avoid expensive VACUUM operations. In the case of Redtrack, where data is being loaded incrementally, you want to use a timestamp sort key. [Reference]
  2. You want to periodically run VACUUM in order to improve query performance [1]. At Toro, This is done at the start of the nightly ETL job.
  3. We used an integer timestamp (eg, unix timestamp) instead of the built in timestamp type due to the fact that Redshift does not support storing a timezone with the timestamp [1,2], thus we wanted to avoid potential timezone conversion issues.

Redshift Best Practices implemented by Redtrack

Redtrack implements a number of Redshift best practices to ensure that data is loaded without error and can scale to a large event volume efficiently.

  1. Load data using COPY command. RedTrack does this by default
  2. Uses a single COPY command. RedTrack Reads multiple shards and loads data into Redshift issues a single COPY command at the end with a manifest of all the files generated by the shard read.
  3. Verify data file size in S3 before performing COPY. Amazon S3 is eventually consistent in US-East region [Reference, Pinterest issues with eventual consistency]. The RedTrack loader verifies files PUT to S3 match local file sizes before executing a COPY.
  4. Use a unique object key in S3 to alleviate issues around S3 eventual consistency. The RedTrack loader does this, using Ruby’s Tempfile class to create a unique filename locally and then uploading to S3 with the same name.
  5. Compress data files before COPY into Redshift using gzip. Redtrack loads data into Redshift using compressed JSON. I benchmarked Redshift COPY and found that file size was the single most important factor in loading performance [Benchmarking Redshift COPY]
  6. Split input data into multiple files and run COPY via a manifest. To get the best performance, you want the # of files in the manifest to be a multiple of the the number of “slices” in the Redshift cluster so that Redshift can evenly distribute the workload to each slice, which is the number of VPUs in the Redshift cluster [Reference]. RedTrack does this by calculating the number of slices in the Redshift cluster via the Redshift AWS API and then splitting each shard read into a handful of files (equaling the number of slices in the redshift cluster). I found that splitting and loading by manifest resulted in a 15% performance improvement for a 1 node cluster.
  7. Use transactions to copy data into Redshift [Presentation, see slide 43]. Redtrack updates load history and Redshift COPY command in a single transaction. This is valuable since a subsequent loader run will look at the load history to determine the last loaded Kinesis sequence number. Keeping the updates in a single transaction ensures the data stays consistent and prevents duplicated loads.
  8. Load multiple kinesis shards at once / in parallel. RedTrack grabs metadata about the Kinesis stream and, in forked processes, reads data from each shard in parallel. Each Kinesis shard can support up to 1MB/sec write, 2MB/sec read, after which point you’d want to have additional shards.
  • Amazon Kinesis Connector Library. Implements a number of kinesis connectors which provide some basic functionality for getting data out of kinesis into other AWS data stores. Written in Java.
  • Flydata. Service that can ingest data into Redshift from Mysql replication or log data.

Additional Reading