Hadoop
Distributed Cache
Data Management
File reusability
Big Data Analytics

Re-use files in Hadoop Distributed cache

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Apache Hadoop is an open-source framework that allows for the distributed processing of large data sets across clusters of computers using simple programming models. One of its powerful features is the Hadoop Distributed Cache, which enhances the efficiency of map/reduce jobs by caching files (text, archives, jars) needed by applications. When you use the Distributed Cache, it copies the necessary files to the slave node, where your job is running, and saves time because the files do not need to be downloaded each time.

Understanding Hadoop Distributed Cache

Hadoop’s Distributed Cache is designed to cache files when needed by applications, so they don’t have to fetch them from a distant source repeatedly. In use, if multiple map/reduce jobs require the same files, they benefit from fetching these cached copies. Files are typically cached on a per-job basis and are specified at job submission time. When a job is configured, these files are then copied to each node running tasks for the job before the actual map/reduce tasks are executed, thus greatly reducing the load time and network congestion.

How to Use Distributed Cache

To utilize the Distributed Cache, developers include files (like data, libraries, etc.) necessary for the job. Files can be distributed as follows:

  • Text files/Archives: Shared read-only resources can be distributed using the cache.
  • JAR files: Classes required by the job can be shared via the cache.

In the code, developers reference these resources relative to the local file system's path (often using symbolic links), not by their original HDFS paths.

Example Scenario: Caching a Text File

Imagine having to share a large read-only reference file (ref_data.txt) across all nodes to perform some data enrichment during your map/reduce tasks. Here's a basic outline of how you might set it up:

java
// In your Driver class or within your Job setup
Job job = Job.getInstance(new Configuration());
job.addCacheFile(new URI("/path/to/hdfs/ref_data.txt#ref_data"));

This code fragment tells Hadoop to add ref_data.txt into the distributed cache and makes it available locally on the cluster nodes as ref_data. Within the mapper or reducer, reference it like so:

java
Path localCached = new Path("./ref_data");
FileSystem fs = FileSystem.get(context.getConfiguration());
BufferedReader br = new BufferedReader(new InputStreamReader(fs.open(localCached)));

The above example demonstrates caching a single file, but similar methods can be used for multiple files, or different types of files (archives, jars), depending on the requirements.

Benefits of Using Distributed Cache

The Hadoop Distributed Cache offers significant benefits including:

  • Reduced Network Congestion: Since the files are transferred to each node only once per job, rather than multiple times per task, the network is much less congested.
  • Decreased Latency: Data needed is already present on the node, reducing the time tasks wait for data.
  • Resource Optimization: Saves bandwidth and speeds up the overall processing time since each node has a local copy of frequently accessed data.

Key Points Summary Table

FeatureDescriptionBenefit
Local AvailabilityFiles are copied to the node's local storage.Reduces data fetching time per task.
One-time transferFiles are transferred once per job, not per task.Decreases network congestion.
Support for multiple file typesText, archives, and JAR files can be cached.Flexibility in types of cached data.
Automatic DeletionCached files are automatically deleted after job completion.Optimizes storage across the cluster.

Conclusion

Leveraging the Hadoop Distributed Cache effectively allows developers to enhance the performance and efficiency of their Hadoop jobs. By understanding and employing this feature, you can significantly improve job turnaround times, especially for data-intensive tasks distributed across a large number of nodes.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.