MPP Data Warehouses: Snowflake & Amazon Redshift
Why standard JDBC fails miserably on petabyte-scale MPP databases. Learn the intermediate cloud storage staging paradigm, high-throughput UNLOAD/COPY execution, and Catalyst query pushdown optimization.
The MPP Staging Architecture: Why Direct JDBC Fails
Decoupled Compute & Storage MechanicsIn MPP architectures like Snowflake (Virtual Warehouses + Centralized Storage) and Amazon Redshift (Leader Node + Slices + Managed Storage), streaming billions of rows over direct JDBC connections causes severe leader-node bottlenecks, query timeouts, and out-of-memory crashes.
The Direct JDBC Fallacy
Every record travels over a single TCP connection, serializing objects into Java JDBC result sets.
- Leader Node Overload: Redshift leader or Snowflake cloud services must serialize all rows.
- Row-by-Row Slowness: Max throughput rarely exceeds 15–30 MB/sec.
- No Cluster Parallelism: Spark executors sit idle waiting on cursor batches.
The Staged Bulk MPP Pattern
Spark and MPP engine leverage intermediate cloud object storage (S3 / Snowflake Stage) via Parquet.
- Reading: MPP parallel UNLOAD to S3/Stage → Spark reads Parquet concurrently.
- Writing: Spark parallel writes Parquet to S3/Stage → MPP runs parallel COPY INTO.
- Throughput: Scales to 5,000+ MB/sec utilizing all MPP compute slices.
Interactive MPP Pipeline & Pushdown Simulator
Simulate Data Transfer, Network Ingestion & Catalyst PushdownLive Pipeline Metrics
Data Flow Visualization: Spark Cluster ↔ Intermediate Stage ↔ MPP Engine
Snowflake Spark Connector: Key-Pair Auth & Production Read/Write
Connector: net.snowflake:spark-snowflake_2.12The official Snowflake Spark connector automatically negotiates with Snowflake Cloud Services to orchestrate internal or external stages. For enterprise security, always use RSA 2048-bit Key-Pair Authentication instead of hardcoding passwords.
from pyspark.sql import SparkSession
from pyspark.sql import functions as F
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.backends import default_backend
# 1. Initialize SparkSession with Snowflake Maven Coordinates
# --packages net.snowflake:spark-snowflake_2.12:2.12.0-spark_3.4,net.snowflake:snowflake-jdbc:3.14.4
spark = (
SparkSession.builder
.appName("SnowflakeHighThroughputIngestion")
.config("spark.sql.execution.arrow.pyspark.enabled", "true")
.getOrCreate()
)
# 2. Secure RSA Key-Pair Auth Decryption (Never put plaintext passwords in code)
with open("/secrets/snowflake/rsa_key.p8", "rb") as key_file:
p_key = serialization.load_pem_private_key(
key_file.read(),
password=None, # Or provide passphrase from vault
backend=default_backend()
)
pkcs8_bytes = p_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.NoEncryption()
)
private_key_str = pkcs8_bytes.decode("utf-8").replace("-----BEGIN PRIVATE KEY-----\n", "").replace("\n-----END PRIVATE KEY-----", "").replace("\n", "")
# 3. Connection Configuration Options
sf_options = {
"sfURL": "https://xy12345.us-east-1.snowflakecomputing.com",
"sfAccount": "xy12345",
"sfUser": "ETL_SPARK_SVC",
"pem_private_key": private_key_str,
"sfRole": "SYSADMIN",
"sfWarehouse": "COMPUTE_WH_XL",
"sfDatabase": "PROD_ENTERPRISE_DW",
"sfSchema": "PUBLIC"
}
# 4. HIGH-THROUGHPUT READ with Catalyst Pushdown
# Query filters and aggregations are automatically pushed into Snowflake SQL engine
orders_df = (
spark.read
.format("snowflake")
.options(**sf_options)
.option("dbtable", "FACT_SALES_ORDERS")
.load()
# Spark Catalyst pushes this WHERE filter into Snowflake COPY/SELECT directly
.filter(F.col("order_date") >= "2026-01-01")
.select("order_id", "customer_id", "order_date", "total_amount", "country")
)
orders_df.printSchema()
# 5. HIGH-SPEED WRITE via Snowflake Internal Stage (Direct Parquet COPY INTO)
# No single driver bottleneck; all executors stream partitions directly into stage
(
orders_df.repartition(16)
.write
.format("snowflake")
.options(**sf_options)
.option("dbtable", "SUMMARY_SALES_AGG")
.option("autopushdown", "on") # Enable advanced Catalyst tree pushdown
.mode("append")
.save()
)
Amazon Redshift Spark Connector: S3 Staging & IAM Role COPY
Connector: io.github.spark-redshift-community:spark-redshift_2.12
The Redshift connector requires an Amazon S3 intermediate staging folder (`tempdir`). On a write, Spark stages snappy-compressed Parquet files in S3 and issues an authenticated SQL COPY command to the Redshift cluster. On read, Redshift executes a parallel UNLOAD to S3, which Spark then reads into DataFrame partitions.
from pyspark.sql import SparkSession
# 1. SparkSession with AWS SDK & Redshift Community Coordinates
# --packages io.github.spark-redshift-community:spark-redshift_2.12:6.2.0-spark_3.4,com.amazon.redshift:redshift-jdbc42:2.1.0.25
spark = (
SparkSession.builder
.appName("RedshiftS3StagedIngestion")
.config("spark.hadoop.fs.s3a.impl", "org.apache.hadoop.fs.s3a.S3AFileSystem")
.config("spark.hadoop.fs.s3a.aws.credentials.provider", "com.amazonaws.auth.DefaultAWSCredentialsProviderChain")
.getOrCreate()
)
# 2. Connection Parameters with IAM Role Authentication
REDSHIFT_JDBC_URL = "jdbc:redshift://redshift-prod-cluster.xyz.us-east-1.redshift.amazonaws.com:5439/analytics_dw"
TEMP_S3_STAGE = "s3a://company-datalake-staging/spark-redshift-temp/"
REDSHIFT_IAM_ROLE = "arn:aws:iam::123456789012:role/RedshiftS3FullAccessRole"
# 3. HIGH-THROUGHPUT READ from Redshift (UNLOAD to S3 -> Spark parallel load)
redshift_df = (
spark.read
.format("io.github.spark_redshift_community.spark.redshift")
.option("url", REDSHIFT_JDBC_URL)
.option("user", "spark_etl_user")
.option("password", "SecureSecret#2026")
.option("dbtable", "dw_public.fact_customer_transactions")
.option("tempdir", TEMP_S3_STAGE)
.option("aws_iam_role", REDSHIFT_IAM_ROLE)
.option("tempformat", "PARQUET") # Use columnar Parquet for 3x faster unload
.load()
)
# 4. Filter & Transform inside Spark
filtered_df = redshift_df.filter("transaction_status = 'APPROVED' and amount > 50.0")
# 5. HIGH-SPEED WRITE to Redshift (Spark write Parquet -> Redshift COPY INTO)
(
filtered_df.write
.format("io.github.spark_redshift_community.spark.redshift")
.option("url", REDSHIFT_JDBC_URL)
.option("user", "spark_etl_user")
.option("password", "SecureSecret#2026")
.option("dbtable", "dw_public.fact_approved_transactions")
.option("tempdir", TEMP_S3_STAGE)
.option("aws_iam_role", REDSHIFT_IAM_ROLE)
.option("tempformat", "PARQUET")
.option("extracopyoptions", "TRUNCATECOLUMNS COMPUPDATE OFF") # Optimize Redshift COPY
.mode("append")
.save()
)
Catalyst Pushdown Optimization: What Runs in Spark vs MPP
Predicate, Projection & Aggregate Pushdown Guide| Operation Type | Pushed Down to MPP? | How Catalyst Translates It | Performance Impact |
|---|---|---|---|
Column Projection (.select("id", "amount")) |
Yes (100%) | Converts SELECT * into SELECT id, amount in MPP SQL |
Eliminates 90% of I/O network bandwidth for wide tables. |
Predicate Filters (.filter("country = 'US'")) |
Yes (100%) | Appends WHERE country = 'US' to MPP query before unload |
Leverages Snowflake micro-partition pruning & Redshift sort keys. |
Basic Aggregation (.groupBy().agg(sum())) |
Supported | With autopushdown=true, compiles aggregation to remote SQL |
Reduces million-row transfer to a tiny summary dataset. |
Complex PySpark UDFs (@udf def custom_fn) |
No | Cannot be translated to SQL; executed inside Python worker processes | Forces full table download before running Python logic. |