30 Mins

4. Amazon Redshift & Distribution Styles

Redshift Cluster Architecture: Leader and Compute Nodes

Amazon Redshift operates on a massively parallel processing (MPP) architecture. The cluster is composed of a single leader node and one or more compute nodes. The leader node manages external communication, query parsing, and query planning. When a query is submitted, the leader node optimizes the plan and distributes the workload across the compute nodes, which execute the actual data processing tasks. This architecture ensures that computational power scales linearly with the number of nodes.

The compute nodes are organized into slices, where each slice operates as a separate unit with its own dedicated memory and disk space. The efficiency of your cluster depends heavily on how the leader node distributes data across these slices. If the data is poorly distributed, you experience data skew, where one compute node does significantly more work than others, leading to a performance bottleneck proportional to $Time_{Total} = Time_{Max(Slice)}$. Balancing the workload across all available compute nodes is the fundamental goal of effective data modeling in Redshift.

Operator Note: Always monitor the STV_SLICEMAP and STL_QUERY_PLAN views to identify bottlenecks in your parallel execution flow.
SQL
-- Check the distribution of rows across slices to detect data skew
SELECT slice, COUNT(*) as row_count
FROM stl_scan
WHERE table = (SELECT table_id FROM stv_tbl_perm WHERE name = 'sales_data')
GROUP BY 1
ORDER BY 2 DESC;

Advanced Data Distribution Styles

Data distribution determines how rows are partitioned across cluster slices. Choosing the right style (AUTO, KEY, ALL, EVEN) is critical for minimizing data movement (redistribution) during join operations. KEY distribution places rows with the same value in the distribution key column on the same slice. This is ideal for frequently joined tables, as it enables local joins and avoids network shuffle. However, improper selection of the key can lead to significant data skew if the chosen column has low cardinality.

ALL distribution copies the entire table to every compute node, which is extremely efficient for small dimension tables that are joined with large fact tables. This eliminates the need for data movement entirely. EVEN distribution cycles through the slices in a round-robin fashion, ensuring uniform storage, which is the default for most tables. By balancing the storage overhead against the network traffic costs, you can optimize query performance, following the logic where $Cost = DataMovement + Computation$.

Operator Note: If a table is smaller than 10MB, Redshift often promotes it to ALL distribution automatically, regardless of your DDL specification.
SQL
-- Create a fact table distributed by key and a dimension table replicated to all nodes
CREATE TABLE sales_fact (
    transaction_id INT, 
    customer_id INT
) DISTSTYLE KEY DISTKEY (customer_id);

CREATE TABLE customer_dim (
    customer_id INT, 
    name VARCHAR(100)
) DISTSTYLE ALL;

Sort Keys and Redshift Spectrum

Sort keys dictate the physical order of data on disk. A 'Compound' sort key is composed of all columns listed in the order specified, ideal for queries that filter on prefixes of the key. 'Interleaved' sort keys give equal weight to each column, making them highly effective for queries that filter on any subset of the columns but require more maintenance for vacuuming. Efficient sorting allows Redshift to use zone maps, which store min/max values per data block to skip scanning irrelevant data blocks, drastically reducing I/O.

Redshift Spectrum allows you to query data directly from Amazon S3 without loading it into the cluster. This is powerful for cold data storage. Spectrum uses the same query optimizer as the local Redshift engine, but pushes computation out to a fleet of externalized processing nodes. By partitioning S3 files (e.g., /year=2023/month=10/), you can leverage partition pruning to avoid scanning the entire data lake, effectively treating S3 like a massive virtual partition.

Operator Note: Use ANALYZE and VACUUM commands regularly to maintain zone map efficiency after significant data mutations.
SQL
-- Defining a table with an interleaved sort key for high-dimensional filtering
CREATE TABLE logs (
    event_date TIMESTAMP,
    event_type VARCHAR(20),
    user_id INT
) INTERLEAVED SORTKEY (event_date, event_type);

-- Creating an external table pointing to S3 data
CREATE EXTERNAL TABLE spectrum_schema.raw_logs (
    user_id INT,
    event_data VARCHAR(100)
) STORED AS PARQUET LOCATION 's3://my-bucket/logs/';

Knowledge Check

Ready to test your understanding of 4. Amazon Redshift & Distribution Styles?

Back