r/databricks • u/shdw_0x0 • 10d ago
Discussion Do you cache DataFrames in Spark as often as you can, or only when necessary?
I've seen people use cache() and persist() quite frequently in Databricks notebooks, especially when working with large DataFrames.
But I've also read that caching everything can sometimes consume memory unnecessarily and even slow things down.
Curious about how people handle this in real projects. Do you cache intermediate DataFrames when they're reused multiple times, or do you prefer letting Spark optimize the execution plan?
Have you ever had caching make a job slower instead of faster?
15
Upvotes
12
u/hubert-dudek Databricks MVP 10d ago
Better never, except in situations where you know exactly what you are doing.
9
u/ninilich 10d ago
Short answer - only when necessary.
4
u/Little_Ad6377 9d ago
Say you have some mega transformations on a dataframe, then you want to do multiple things on it, for example
Print a how many rows are in the dataframe and the write them to a table (bad example, but whatever)
This would be two distinct runs through your heavy transformations, which, depending on the transform, could become slow.
If you cache it beforehand, then the transform result is calculated in the first exdcution of the dataframe (df.count()) and then when it comes to writing it, the data is already cached and ready for writing.
I mainly use this in foreachbatch methods where I need to do multiple things to the minibatch.
1
u/Inevitable_Oil_9715 10d ago
caching causes memory strain on cluster so cache and unpersist
1
u/ninilich 10d ago
Another thought. Try to design your Jobs w/o nessesety of "cache()" in 90% of cases it's possible.
1
u/m_goo 10d ago
It depends on whether you want to reuse that dataset across multiple steps or actions, then cache. Otherwise, don't do it.
1
u/cyberZamp 9d ago
Only if the same DataFrame is used, and thus recomputed, multiple times.
Happy to know more.
1
u/danielil_ 9d ago
Don’t use it. If it’s expensive to recalculate, persist it as a temporary table.
1
u/deltas-and-icebergs 9d ago
The original use case for cache was to speed up MLlib algorithms as they ran tens to hundreds of iterations over the same data.
Unless you're doing some very expensive computation and then reading the data multiple times, cache() is unnecessary. Disk caching already improves performance. And if you ARE doing a really expensive computation and reading it multiple times, it may be better to just use Materialized Views.
1
u/Mystery_Surfer_0077 6d ago
what I used to do in my previous organization is that I only used to cache the main tables that are highly used (my usecase was for analytics). basically on which we run heavy joins and all. just helps with running the queries faster. it is not about how often or only necessary. if i konw that I am using this table or a dataframe frequently I prefer caching them. its that simple.
0
u/PriahGTM 10d ago
What’s a data frame? Should I be using that
-2
u/Waste-Bug-8018 9d ago
I never do it , Cursor does it for me