Hadoop 1.2.1 - using Distributed Cache
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
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. A pivotal feature in this ecosystem, particularly in Hadoop 1.2.1 and earlier versions like it, is the Distributed Cache. This component plays a crucial role in enhancing the efficiency of Hadoop applications by caching the files needed by applications.
Understanding Distributed Cache
Distributed Cache in Hadoop is used to distribute large, read-only files that are necessary to execute some applications. By caching these files once and reusing them across multiple jobs, it saves significant time and network bandwidth that would otherwise be spent in transferring these files multiple times.
Files put in Hadoop's Distributed Cache can be shared by all nodes running the job and hence, they are ideal for sharing common configuration settings or library files.
How Does Distributed Cache Work?
When you set up a job to use the Distributed Cache, you specify the files you want to cache via the job's configuration. Hadoop framework will then internally copy the specified files to the slave node’s local storage, before any tasks for the job execute. This means that when the task runs, the file can be read from the local disk rather than being read from a distant location, which can be significantly faster.
Technical Implementation
To use Distributed Cache in your Hadoop MapReduce programs, you can utilize the DistributedCache class available in the Hadoop API. Here’s a step-by-step guide:
- Add files to the cache: Use methods like
DistributedCache.addCacheFile(URI, Configuration)to add files to the cache. - Retrieve cached files in the MapReduce program: In your Mapper or Reducer, use
DistributedCache.getLocalCacheFiles(Configuration)to access the cached files.
Here is a simple example using Distributed Cache:
This example points out the simplicity in setting up the Distributed Cache which can dramatically reduce the data read-write traffic across the nodes.
Best Practices and Uses of Distributed Cache
- Storing shared libraries: If your application needs external libraries or jars, placing these in the Distributed Cache can avoid the overhead of loading these resources multiple times.
- Config files: Common configurations needed across all tasks can be placed in the cache.
- Small reference data: Frequently accessed small datasets can be efficiently fetched from the cache.
Summary of Key Points
| Feature | Description |
| Purpose | Caches necessary files on local nodes to save network bandwidth and time. |
| Implementation Class | DistributedCache in the Hadoop API |
| Benefits | Reduces the amount of data transferred across the network. |
| Best Used For | Libraries, configuration files, and small reference data. |
Conclusion
Utilizing Distributed Cache efficiently can potentially offer substantial performance optimizations in Hadoop by minimizing network congestion and reducing the load on file systems. Ensuring that you understand and implement this feature correctly can greatly enhance the speed and efficiency of your Hadoop jobs.
Related reading
- Hadoop cache file for all map tasks
- Hadoop Distributed Cache - modify file
- Hadoop Distributed Cache don't work
- Hadoop Distributed Cache error message interpretation
- Hadoop Distributed Cache file not found exception
- Hadoop distributed cache using -libjars How to use external jars in your code
- Hadoop Distributed Cache via Generic Options -files
- Hadoop Distributed file system vs distributed cache

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.