how to set Hadoop DistributedCache?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Hadoop's old DistributedCache API was designed to copy small read-only files to task nodes so mappers and reducers could use shared reference data locally. In modern Hadoop code, you usually do not call the old DistributedCache class directly anymore. Instead, you add cache files through the Job API and let YARN localize them for the task.
Know the Modern API Path
Many examples still mention DistributedCache, but the modern pattern uses job.addCacheFile(...) or job.addCacheArchive(...).
The fragment after # creates a symlink name inside the task's working directory. That makes the localized file easier to open.
This is the practical replacement for the older DistributedCache.addCacheFile(...) style.
Access the Localized File in the Mapper or Reducer
Cached files are typically read during setup, not for every individual record.
This pattern keeps lookup data in memory after one local read, which is the normal reason for using the cache in the first place.
Use It for Small, Read-Only Reference Data
The cache is best for things such as:
- lookup tables
- static configuration files
- dictionaries
- archived helper resources needed by every task
It is not for large mutable datasets. If the file is large enough to dominate startup or localization cost, it may belong in ordinary HDFS reads or a different data distribution design.
The important assumption is that every task sees the same read-only file content.
Archives and Executables Need Slightly Different Handling
If you need to distribute a zip or tar archive, use a cache archive so Hadoop localizes and unpacks it.
Then the unpacked directory is accessible locally through the alias name. This is useful for shipping auxiliary binaries or structured reference resources, but you should keep them small and deterministic.
Prefer Explicit HDFS Paths and Stable Aliases
Use clear HDFS URIs and stable alias names. Debugging gets harder when the localized filename is unpredictable or when code assumes a path that was never aliased.
A good pattern is:
- put the reference file in HDFS first
- add it with a clear alias
- read it in
setup - fail fast if the file is missing or malformed
That makes task behavior repeatable across nodes.
Remember That the Old API Name Is Mostly Historical Now
If you search older blog posts, you will see direct references to DistributedCache. The concept still exists, but the recommended coding style has moved into the Job API. When maintaining older code, do not be surprised if both styles appear. The operational idea is the same: localize shared read-only resources for each task.
Common Pitfalls
- Using the deprecated old
DistributedCacheAPI style when theJobAPI already provides the modern entry points. - Reading the localized file for every record instead of loading it once in
setup. - Putting large or frequently changing datasets into the cache when it is meant for small read-only resources.
- Forgetting the alias fragment and then opening the wrong local filename in the task.
- Assuming cache localization replaces proper HDFS input design for ordinary large data access.
Summary
- In modern Hadoop, add cached resources through
job.addCacheFileorjob.addCacheArchive. - Use the cache for small read-only files that every task needs locally.
- Load the localized resource once during task setup.
- Use clear HDFS URIs and stable alias names for predictable access.
- Treat
DistributedCacheas a concept that survives, even though the older class-based API is mostly legacy.

