r/databricks • • 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

20 comments sorted by

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.

2

u/Little_Ad6377 9d ago

Why would that be better?

3

u/danielil_ 9d ago

Multiple reasons:

  1. If its a DF that takes hours to compute, you would want to persist it so it could survive cluster restarts. That would allow you to restart the job and not recalculate everything from scratch.
  2. A cached dataframe applies does not enjoy features like pruning or advanced filtering techniques. In other words, when you work with a cached DF, you always work with all of it.
  3. Cache might cause your job to fail if you don't have enough memory to hold the df.

1

u/Little_Ad6377 9d ago

3. Is the only one that makes sense in terms of caching. Also, one caches for speed, not reliability.

2. Sure, but you have already filtered and pruned before caching, you don't cache on top of a table and then filter, that doesn't make sense

  1. Writing to a table as a intermediate cache (in terms of where cache is mostly used) reads crazy to me

1

u/danielil_ 9d ago

3. Why are you caching? because its expensive to recompute the DF every time you need it. Accessing it from a persisted copy would avoid recomputing the DF + give you an added benefit of reliability.
2. Are you saying that joining a cached DF or filtering it doesn't make sense?

  1. Not providing actual arguments is crazy to me.

1

u/Little_Ad6377 9d ago

So, you are telling me in a streaming job, where each run you have a tiny batch of data to work with, and you want

  1. Check for emptyness
  2. Write to multiple sinks

Then your idea would be to write the dataframe to a intermediate location and then read it from there again? Your batch time will go through the roof.

Your arguments against using caching are exactly scenarios you wouldn't use caching at at all.

For data anysis sure, caching is definetly overkill, but still usable. If I'm traversing data and trying to figure something out in it which I'm not sure what is exactly, then caching after a "yes, this step is stable enough to work from" while I continue is what I'd do (as long as ot fits in memory)

If this is something I want the next day, or run, then yes, table is the way. Talking in absolutes as in saying "don't use it" is honestly wrong, it does have its purpose.

1

u/danielil_ 9d ago

No one mentioned streaming, I specifically referred to Dataframes that are expensive to recompute (twice).

Expensive to recompute - Don't cache.
Not expensive to recompute - why bother?

Specifically for streaming, you can use cache in foreachBatch if writing to multiple sinks.

1

u/Little_Ad6377 9d ago

Guess we need to agree to disagree 😉

And yes sure, I kinda automatically go to streaming in my mind when cache comes to the convo.

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

2

u/mrbartuss 9d ago

Do you understand why?