DistributedCache in Hadoop 2.x
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
DistributedCache is a facility provided by the Apache Hadoop framework to cache files (text, archives, jars, etc.) needed by applications. Once a file is cached for a particular job, Hadoop makes it available on each data node where map/reduce tasks are running, avoiding the need to fetch these files from the file system repeatedly. This enhancement was particularly significant in Hadoop 2.x, improving the efficiency and performance of map/reduce jobs.
Understanding DistributedCache
DistributedCache is beneficial when dealing with large datasets and has extensive use in preprocessing operations and lookup datasets in map/reduce tasks. For example, if you have a common lookup table needed by all the mappers, instead of hitting a centralized service or storing in the HDFS multiple times, you can use DistributedCache. This not only saves network bandwidth but also reduces the load on the file system.
How DistributedCache Works
When a job is submitted, files specified for caching through the job's configuration are copied to the local file system of each node executor running tasks for the job. Hadoop takes care of maintaining the freshness of files, typically by implementing timestamps to check for updates. Files are then symlinked into the working directory of each task, ensuring that no conflicts occur between tasks and their dependencies.
Usage of DistributedCache
To use DistributedCache, files to be cached are specified in the job configuration. Here’s a simple example in a Hadoop program:
In this example, the specified file is added to the cache with a symbolic link name. It is then retrieved in the setup method of the mapper or reducer, and used as needed. Notice the use of URI with a fragment (#) which gives a symbolic name (datafile) to the cached file.
Benefits of Using DistributedCache
- Efficiency: Reduces the data fetched between the application and the file system, which can be crucial for bandwidth and overall performance.
- Speed: Local access to file compared to access from Hadoop Distributed File System (HDFS) or a remote source.
- Scalability: As the application scales, the cost of repeatedly accessing the data from a central server grows, making caching a better alternative.
Best Practices
- Compression: Cached files should be compressed to save space and minimize I/O transfer time.
- Cleanup: Proper cleanup of cached files to avoid stale data being used by newer versions of the jobs.
- Symlink: Use symlink capability for easier reference to cached files within the program.
Limitations of DistributedCache
- Memory management: Caching large datasets might lead to memory issues, especially if not managed properly.
- Version control: Ensuring all MapReduce tasks use the correct version of the cache file can sometimes be challenging.
- Configuration Overheads: Proper configuration is necessary to leverage efficiently, adding a layer of complexity to job setup.
Summary Table
| Feature | Description |
| Efficiency | Reduces network usage and file system load. |
| Speed | Provides quicker access to files by storing them locally on each node. |
| Scalability | Enhances application's scalability in processing large datasets. |
| Best Practices | Compress, cleanup, and symlink for optimal usage. |
| Limitations | Potential memory management issues and configuration overheads. |
Understanding and utilizing DistributedCache can significantly optimize the performance of Hadoop jobs b handling frequent read-only data efficiently. By ensuring files are available locally at each node, it reduces the dependence on central data storage mechanisms, thereby speeding up processing times for large scale data analyses.

