Industry 4.0 applications foster new business opportunities, but they also pose new and challenging requirements, such as low latency communications and highly reliable systems. They would likely exploit novel wireless technologies (5G), but it would also become crucial using architectures that appropriately support them. In this sense, the combination of fog and cloud computing represents a potential solution, since it can dynamically allocate the workload depending on the specific needs of each application. In this paper, our main goal is to provide a highly reliable and dynamic architecture, which minimizes the time that an end node or user, spends in downloading the required data. In order to achieve this, we have developed an optimal distribution algorithm that decides the amount of information that should be stored at, or retrieved from, each node, to minimize the overall data download time. Our scheme is based on various parameters and it exploits Network Coding (NC) as a tool for data distribution, as a key enabler of the proposed solution. We compare the performance of the proposed scheme with other alternative solutions, and the results show that there is a clear gain in terms of the download time.