Lesson 5 of 13 Amazon Redshift

Querying Live Operational Data with Redshift Federated Queries

This lesson explains how federated queries let Redshift read live data from RDS and Aurora databases in the same SQL statement, without an ETL pipeline, when a question needs warehouse history and current operational state together.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe how a federated query reads a live database.
  • Decide when federation is appropriate.
  • Configure the network and credentials federated queries require.

How federated queries work

A federated query reads live data from an operational database at the moment the query runs. An external schema in Redshift maps to a database in Amazon RDS or Aurora, for PostgreSQL or MySQL, and the credentials for that database are held in AWS Secrets Manager rather than in SQL.

Queries can then join tables in the external schema with local Redshift tables. Redshift pushes filters down to the source database, so the database returns only the rows that match, which reduces both the data transferred and the work done by the warehouse. The source database still executes part of every federated query, which is why federation suits light workloads.

The following diagram shows a federated query that joins a Redshift table with an Aurora PostgreSQL table, and what crosses to Aurora.

The federated query for late orders. Redshift joins its late_orders table with the ops.orders table in Aurora PostgreSQL through an external schema whose credentials are in Secrets Manager. Redshift pushes the join keys down, so Aurora returns only the few hundred matching rows.
The scenario is illustrative.

When to federate

A federated query reads live data from an Aurora or RDS PostgreSQL or MySQL database at query time. That is convenient but sends load to the operational database, so it suits light work. Good uses include small lookups of current values, such as an order's status, and exploring operational data before building a pipeline. Federated queries can also drive simple incremental loads with INSERT INTO ... SELECT from the source.

Heavy scans and aggregations, queries that many users run frequently, and any workload that would slow the operational database are better served by loading or replicating the data into Redshift, for example with a zero-ETL integration. The following table summarises the choice.

Use federated queries forLoad or replicate the data instead for
Small lookups of current values, such as order statusHeavy scans and aggregations
Exploring operational data before building a pipelineQueries run frequently by many users
Incremental loads with INSERT INTO ... SELECT from the sourceWorkloads that would slow the operational database
Sort into groups

Federate or replicate?

Exam skills 2.1.5

Decide whether to use a federated query or load the data.

Log in to try this activity Interactive activities are free with an account.

Requirements

Federated queries need network and security configuration, because Redshift connects directly to the database. The warehouse must have network connectivity to the database, normally by being in the same VPC or in connected VPCs, and the database's security group must allow inbound connections from the warehouse on the database port.

For credentials, a secret in AWS Secrets Manager holds the database user name and password, and an IAM role attached to Redshift must have permission to read that secret. The external schema refers to both the secret and the role, so no credentials appear in the schema definition or in queries.

Select all that apply

Federated query set-up

Exam skills 2.1.5 · 4.2.2

Identify what a Redshift federated query needs.

Log in to try this activity Interactive activities are free with an account.
Worked example

Live order status in a warehouse report

A daily warehouse report must show each late order's current status, which changes throughout the day in an Aurora PostgreSQL database. Rather than building a pipeline, the team adds a federated schema, as follows.

CREATE EXTERNAL SCHEMA ops FROM POSTGRES DATABASE 'orders' SCHEMA 'public'
URI 'orders.cluster-abc.eu-west-1.rds.amazonaws.com' PORT 5432
IAM_ROLE 'arn:aws:iam::111111111111:role/RedshiftFederated'
SECRET_ARN 'arn:aws:secretsmanager:eu-west-1:111111111111:secret:orders-ro';

SELECT f.order_id, f.promised_date, o.status
FROM analytics.late_orders f JOIN ops.orders o ON o.order_id = f.order_id;

Only the few hundred late orders are looked up, and Redshift pushes the join keys down to Aurora, so the operational database is barely affected. If the report needed to scan millions of orders, a zero-ETL integration would be the better choice.

Check yourself

Which databases can Redshift federated queries read?

Show answer

Amazon RDS and Aurora PostgreSQL and MySQL databases.

Exam Tips

Remember for the exam

  • To join live data from an operational database, use a federated query.
  • Federated queries support RDS and Aurora PostgreSQL and MySQL.
  • Credentials for federated queries are stored in Secrets Manager.
  • Use federation for small lookups, not heavy scans of the source.
  • For continuous analytics on Aurora data, zero-ETL is usually better than federation.