Apache Iceberg is an open table format for large datasets in Amazon Simple Storage Service (Amazon S3), and provides fast query performance over large tables, atomic commits, concurrent writes, and SQL-compatible table evolution. With Amazon EMR 6.5+, you can use Apache Spark on EMR clusters with the Iceberg table format.
Iceberg helps data engineers manage complex challenges such as continuously evolving datasets while maintaining query performance. Iceberg allows you to do the following:
- Maintain transactional consistency on tables between multiple applications where files can be added, removed, or modified atomically with full read isolation and multiple concurrent writes
- Implement full schema evolution to track changes to a table over time
- Issue time travel queries to query historical data and verify changes between updates
- Organize tables into flexible partition layouts with partition evolution, enabling updates to partition schemes as queries and data volume changes without relying on physical directories
- Roll back tables to prior versions to quickly correct issues and return tables to a known good state
- Perform advanced planning and filtering in high-performance queries on large datasets
In this post, we show you how to improve the performance of Iceberg’s metadata file operations using Amazon FSx for Lustre and Amazon EMR.
Performance of metadata file operations in Iceberg
The catalog, metadata layer, and data layer of Iceberg are outlined in the following diagram.
Iceberg maintains metadata across multiple small files (metadata file, manifest list, and manifest files) to effectively prune data, filter data, read the correct snapshot, merge delta files, and more. Although Iceberg has implemented fast scan planning to make sure that metadata file operations don’t take a large amount of time, the time taken is slightly high for object storage like Amazon S3 because it has a higher read/write latency.
In use cases like a high-throughput streaming application writing data into an S3 data lake in near-real time, snapshots are produced in microbatches at a very fast rate, resulting in a high number of snapshot files and causing degradation in performance of metadata file operations.
As shown in the following architecture diagram, the EMR cluster consumes from Kafka and writes to an Iceberg table, which uses Amazon S3 as storage and AWS Glue as the catalog.
In this post, we dive deep into how to improve query performance by caching metadata files in a low-latency file system like FSx for Lustre.
Overview of solution
FSx for Lustre makes it easy and cost-effective to launch and run the high-performance Lustre file system. You use it for workloads where speed matters, such as high throughput streaming writes, machine learning, high performance computing (HPC), video processing, and financial modelling. You can also link the FSx for Lustre file system to an S3 bucket, if required. FSx for Lustre offers multiple deployment options, including the following:
- Scratch file systems, which are designed for temporary storage and short-term processing of data. Data isn’t replicated and doesn’t persist if a file server fails. Use scratch file systems when you need cost-optimized storage for short-term, processing-heavy workloads.
- Persistent file systems, which are designed for long-term storage and workloads. The file servers are highly available, and data is automatically replicated within the same Availability Zone in which the file system is located. The data volumes attached to the file servers are replicated independently from the file servers to which they are attached.
The use case with Iceberg’s metadata files is related to caching, and the workloads are short-running (a few hours), so the scratch file system can be considered as a viable deployment option. A Scratch-2 file system with 200 MB/s/TiB of throughput is sufficient for our needs because Iceberg’s metadata files are small in size and we don’t expect a very high number of parallel connections.
You can use FSx for Lustre as a cache for the metadata files (on top of an S3 location) to offer better performance in terms of metadata file operations. To read/write files, Iceberg provides a capability to load a custom FileIO dynamically during runtime. You can pass the
FSxForLustreS3FileIO reference using a Spark configuration, which takes care of reading/writing to appropriate file systems (FSx for Lustre for reads and Amazon S3 for writes). By enabling the catalog properties
data.repository.path, Iceberg resolves the S3 path to FSx for Lustre path at runtime.
As shown in the following architecture diagram, the EMR cluster consumes from Kafka and writes to an Iceberg table that uses Amazon S3 as storage and AWS Glue as the catalog. Metadata reads are redirected to FSx for Lustre, which updates asynchronously.
Pricing and performance
We took a sample dataset across 100, 1,000, and 10,000 snapshots, and could observe up to 8.78 times speedup in metadata file operations and up to 1.26 times speedup in query time. Note that the benefit was observed for tables with a higher number of snapshots. The environment components used in this benchmark are listed in the following table.
|Iceberg Version||Spark Version||Cluster Version||Master||Workers|
|0.14.1-amzn-0||3.3.0-amzn-1||Amazon EMR 6.9.0||m5.8xlarge||15 x m5.8xlarge|
The following graph compares the speedup for each amount of snapshots.
You can calculate the price using the AWS Pricing Calculator. The estimated monthly cost of an FSx for Lustre file system (scratch deployment type) in the US East (N. Virginia) Region with 1.2 TB storage capacity and 200 MBps/TiB per unit storage throughput is $336.38.
The overall benefit is significant considering the low cost incurred. The performance gain in terms of metadata file operations can help you achieve low-latency read for high-throughput streaming workloads.
For this walkthrough, you need the following prerequisites:
Create an FSx for Lustre file system
- On the Amazon FSx console, create a new file system.
- For File system options, select Amazon FSx for Lustre.
- Choose Next.
- For File system name¸ enter an optional name.
- For Deployment and storage type, select Scratch, SSD, because it’s designed for short-term storage and workloads.
- For Throughput per unit of storage, select 200 MB/s/TiB.You can choose the storage capacity according to your use case. A Scratch-2 file system with 200 MB/s/TiB of throughput is sufficient for our needs because Iceberg’s metadata files are small in size and we don’t expect a very high number of parallel connections.
- Enter an appropriate VPC, security group, and subnet.Make sure that the security group has the appropriate inbound and outbound rules enabled to access the FSx for Lustre file system from Amazon EMR.
- In the Data Repository Import/Export section, select Import data from and export data to S3.
- Select Update my file and directory listing as objects are added to, changed in, or deleted from my S3 bucket to keep the file system listing updated.
- For Import bucket, enter the S3 bucket to store the Iceberg metadata.
- Choose Next and verify the summary of the file system, then choose Create File System.
When the file system is created, you can view the DNS name and mount name.
Create an EMR cluster with FSx for Lustre mounted
This section shows how to create an Iceberg table using Spark, though we can use other engines as well. To create your EMR cluster with FSx for Lustre mounted, complete the following steps:
- On the Amazon EMR console, create an EMR cluster (6.9.0 or above) with Iceberg installed. For instructions, refer to Use a cluster with Iceberg installed.
To use the AWS CLI, refer to create-cluster.
- Keep the network (VPC) and EC2 subnet the same as the ones you used when creating the FSx for Lustre file system.
- Create a bootstrap script and upload it to an S3 bucket that is accessible to EMR.Refer to the following bootstrap script to mount FSx for Lustre in an EMR cluster (the file system gets mounted in the
/mnt/fsxpath of the cluster). The file system DNS name and the mount name can be found in the file system summary details.
- Add the bootstrap action script to the EMR cluster.
- Specify your EC2 key pair.
- Choose Create cluster.
- When the EMR cluster is running, SSH into the cluster and launch the
spark-sqlusing the following code:
Note the following:
- At a Spark session level, the catalog properties
data.repository.pathhave been set.
io-implsets a custom FileIO implementation that resolves the FSx for Lustre location (from the S3 location) during reads.
lustre.mount.pathis the local mount path in the EMR cluster,
lustre.file.system.pathis the FSx for Lustre file system path, and
data.repository.pathis the S3 data repository path, which is linked to the FSx for Lustre file system path. After all these properties are provided, the
data.repository.pathis resolved to the concatenation of
lustre.file.system.pathduring reads. Note that FSx for Lustre is eventually consistent after an update in Amazon S3. So, in case FSx for Lustre is catching up with the S3 updates, the
FileIOwill fall back to appropriate S3 paths.
write.metadata.pathis configured, make sure that the path doesn’t contain any trailing slashes and
data.repository.pathis equivalent to
- At a Spark session level, the catalog properties
- Create the Iceberg database and table using the following queries:
Note that to migrate an existing table to use FSx for Lustre, you must create the FSx for Lustre file system, mount the same while starting the EMR cluster, and start the Spark session as highlighted in the previous step. The Amazon S3 listing of the existing table is updated in the FSx for Lustre file system eventually.
- Insert the data into the table using an
INSERT INTOquery and then query the same:
- You can now view the metadata files in the local FSx mount, which is also linked to the S3 bucket
- You can view the metadata files in Amazon S3:
- You can also view the data files in Amazon S3:
When you’re done exploring the solution, complete the following steps to clean up the resources:
- Drop the Iceberg table.
- Delete the EMR cluster.
- Delete the FSx for Lustre file system.
- If any orphan files are present, empty the S3 bucket.
- Delete the EC2 key pair.
- Delete the VPC.
In this post, we demonstrated how to create an FSx for Lustre file system and an EMR cluster with the file system mounted. We observed the performance gain in terms of Iceberg metadata file operations and then cleaned up so as not to incur any additional charges.
Using FSx for Lustre with Iceberg on Amazon EMR allows you to gain significant performance in terms of metadata file operations. We observed 6.33–8.78 times speedup in metadata file operations and 1.06–1.26 times speedup in query time for Iceberg tables with 100, 1,000, and 10,000 snapshots. Note that this approach reduces the time for metadata file operations and not for the data operations. The overall performance gain would be dependent on the number of metadata files, size of each metadata files, amount of data that is being processed, and so on.
About the Author
Rajarshi Sarkar is a Software Development Engineer at Amazon EMR. He works on cutting-edge features of Amazon EMR and is also involved in open-source projects such as Apache Iceberg and Trino. In his spare time, he likes to travel, watch movies and hang out with friends.