Everything You Need to Know About Redshift Spectrum, Athena, and S3
Last week, Amazon announced Redshift Spectrum — a feature that helps Redshift users seamlessly query arbitrary files stored in S3. Learn more about it here.
Join the DZone community and get the full member experience.Join For Free
last week at the aws san francisco summit , amazon announced a powerful new feature: redshift spectrum . spectrum offers a set of new capabilities that allow redshift users to seamlessly query arbitrary files stored in s3 as though they were normal redshift tables, delivering on the long-awaited requests for separation of storage and compute within redshift. this option opens up a ton of new use-cases that were either impossible or prohibitively costly before.
in essence, spectrum feels like a powerful integration between redshift and athena that provides the following features:
- new sql commands to create external schemas and tables .
- ability to query these external tables and join them with the rest of your redshift cluster.
this is simple but very powerful. amazon just made redshift much bigger without compromising on performance or other database semantics.
so, how does it all work? it starts by defining external tables. one limitation this setup currently has is that you can’t split a single table between redshift and s3. one use case that we cover in panoply where such separation would be necessary is when you have a massive table (think clickstream time series) but only want the most recent events, i.e. the past three months, to reside in redshift, as that covers most of your queries. then, you might want to have the rest of the data in s3 and have the capability to seamlessly query this table. while this is not yet part of the new redshift features, i hope that it will be something that redshift team will consider in the future. in the meantime, panoply’s auto-archiving feature provides an (almost) similar result for our customers.
this means that every table can either reside on redshift normally or be marked as an external table. “external table” is a term from the realm of data lakes and query engines, like apache presto, to indicate that the data in the table is stored externally — either with an s3 bucket or hive metastore. effectively, the table is virtual. it’s only a link with some metadata. let’s consider the following table definition:
create external table external_schema.click_stream ( time timestamp, user_id int ) stored as textfile location 's3://myevents/clicks/'
see: sql reference for create external table .
basically, what we’ve told redshift is to create a new external table — a read-only table that contains the specified columns and has its data located in the provided s3 path as text files. that’s it. we can start querying it as if it had all of the data pre-inserted into redshift via normal copy commands.
this has several important implications:
. querying this table is bound to be slower than querying data that resides within redshift as it involves reading the files and parsing them on every query. see below to understand how it’s done.
- scale . since we’re not storing the data in redshift, there’s a clear separation of storage and compute. you can scale as far as you’d like, both in data size and in computation power, independently of one another. this can easily be as big as s3 allows — exabytes in scale.
- cost . if you’re not querying this table, you wouldn’t pay for it (well, beyond the s3 costs). also, see below more about cost.
the more experienced reader will notice that i’ve skipped the schema/database creation phase. there’s one technical detail i’ve skipped: external schemas. as you might’ve noticed, in no place did we provide redshift with the relevant credentials for accessing the s3 file. quite cleverly, instead of having to define it on every table (like we do for every copy command), these details are provided once by creating an external schema and then assigning all tables to that schema. i will not elaborate on it here, as it’s just a one-time technical setup step, but you can read more about it here .
as stated above, this new feature-set is merely an integration between redshift and aws athena. the latter is aws’s smart wrapper around apache presto, which is a query engine that allows sql code against arbitrary data files — be it hdfs, s3, local file-system, etc.
virtually you can think of it this way: when you run a query against an external redshift table it goes through redshift => athena => presto. that means that at least some of the computation, especially the low-level table scans, happens within presto, which needs to parse the raw data files into a tabular format. in order to do that, presto needs to know ahead of time how the data is structured, is it a parquet file? a csv or tsv file?
that’s where the aforementioned “stored as” clause comes in. when we initially create the external table, we let redshift (and subsequently athena and presto) know how the data files are structured.
now that the table is defined. we can query it just like any other redshift table. but more importantly, we can join it with other non-external tables. so if we have our massive click stream external table and we want to join it with a smaller & faster users table that resides on redshift, we can issue a query like:
select clicks.time, clicks.user_id, users.user_name from external_schema.click_stream as clicks join users on (clicks.user_id = users.users_id);
redshift will construct a query plan that joins these two tables like so:
basically what happens is that the user table is scanned normally within redshift by distributing the work among all nodes in the cluster. in parallel, redshift will ask athena to scan the clicks table. athena itself is an automatically distributed system that will assign more nodes to participate in the workload based on how much data you’re querying. finally, the data is collected from both scans, joined and returned.
performance and cost
naturally, queries running against s3 are bound to be a bit slower. it’s still interactively fast, as the power of presto and the elastic scaling of athena allows great parallelism, but it’s not going to be as fast as having your data pre-compressed, pre-analyzed data stored within redshift. that’s not just because of s3 i/o speed compared to ebs or local disk reads, but also due to the lack of caching, ad-hoc parsing on query-time and the fact that there are no sort keys.
but that’s fine. external tables cover a different use-case. you use them for data your need to query infrequently, or as part of an elt process that generates views and aggregations. in fact, in panoply we’ve simulated these use-cases in the past similarly — we would take raw arbitrary data from s3 and periodically aggregate/transform it into small, well-optimized materialized views within panoply.
as for the cost... this is a tricky one. at the get-go, external tables cost nothing (beyond the s3 storage cost), as they don’t actually store or manipulate data in any way. it’s just a bunch of metadata. but as you start querying, you’re basically using athena’s cost model of paying per scanned data size. so if, for example, you run a query that needs to process 1tb of data, you’d be billed for $5 for that query. this model isn’t unique, as is quite convenient when you indeed query these external tables infrequently but can become problematic and unpredictable when your team query it often.
finally, using a columnar data format like parquet can improve both performance and cost tremendously, as presto wouldn’t need to read and parse the whole table, but only the specific columns that are part of the query. this saves the costs of i/o, due to file size, especially when compressed, but also the cost of parsing.
it’s clear that the world of data analysis is undergoing a revolution. it started out with presto, which was arguably the first tool to allow interactive queries on arbitrary data lakes. then, google’s big query provided a similar solution except with automatic scaling. and finally, aws athena and spectrum bring these same capabilities to aws.
but here at panoply, we still believe the best is yet to come. these new awesome technologies illustrate the possibilities, but the performance is still a bit off compared to classic data warehouses like redshift and vertica that had decades to evolve and perfect. technically, there’s little reason for these new systems to not provide competitive query performance despite their limitations and conceptual differences from the classic data warehouses. (yeah, i said it. more on this topic to come...)
in any case, we’ve been already simulating some of these features for our customers internally for the past year and a half. having these new capabilities baked into redshift makes it easier for us to deliver more value like auto-archiving faster and easier. in the near future, we can expect to see teams learn more from their data and utilize it better than ever before by using capabilities that, until very recently, were outside of their reach.
Published at DZone with permission of Roi Avinoam, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.