Page MenuHomePhabricator

Implement default expiration for staging and intermediate dbt models
Open, Needs TriagePublic

Description

We don't need to keep staging and intermediate dbt models around for very long, and they can easily cause strain to our HDFS infrastructure if we don't manage them.

For example, in T437733: User and team Hive database cleanup tracking for Fall 2026, we found that one of the largest consumers of storage, and also with a large number of files, was an intermediate dbt model.

Acceptance criteria

  • Materialized staging and intermediate dbt models are deleted after 3 days.

Implementation notes

We considered, but haven't yet decided on, moving staging and intermediate models to a different database schema to simplify this. If we go this way, I'd like to make sure that dbt can abstract this away from model owners (e.g. having ref() resolve to the right schema). The fact that dbt has support for custom schemas seems promising.

Event Timeline

hey @GGoncalves-WMF , We would appreciate getting some time to think about the impact of this change. Ive added it to the dbt office hours agenda to discuss.
I will be on pto, but im hoping MI members will join and provide inputs

To provide a little more detail on what @Mayakp.wiki said, we (the Movement Insights team) aren't ready for this to happen.

We definitely understand the pressure from HDFS capacity and don't have a fundamental problem with the idea, but we have not been labeling models in a consistent way. So, many of the models we've labeled "staging" and "intermediate" are designed for re-use and intended to be kept permanently and should actually be categorized as marts.

Here are some example, all from the contributors metrics folder we have, for example:

  • staging folder
    • base_moderator_actions / base_moderator_actions_weekly: mart table with daily counts of per-user moderation actions; reused for many different models including active UWERs and queried directly for the charts in the Daily Moderators dashboard
    • base_moderator_actions_checkuser / base_moderator_actions_checkuser_weekly: like the other moderator action submodels, this is properly categorized as an intermediate table, but unlike the others, we would not want it to be purged because it's sourced from wmf_raw.mediawiki_private_cu_log, which is deleted after 90 days.
    • base_account_registration: mart table with a row for each global account registrations, which is very useful for building other models and doing analysis of registrations
  • intermediate folder
    • int_edit_reverted_in_30_days: mart table containing a row for for each reverted edit; queried directly for some Superset charts and designed to make analysis of reverts and reverted edits easier

So, before we implement this, I think we would need to:

  • Ensure all Dbt users have the same understanding of the staging-intermediate-mart-(metric) model and have renamed their models accordingly (this was also mentioned in T430622#12378676).
  • Provide a way for an individual staging or intermediate models, like base_moderator_actions_checkuser mentioned above, to opt out of expiration (we could also do it by not labelling them as staging or intermediate, but that seems like it will be confusing and opaque).

Thanks for the context, and yes, happy to discuss tomorrow! Indeed, if we "just" add an expiration policy which then pushes users to make everything into a mart, we haven't actually solved the storage problem.

With that said, it does sound like your examples could all reasonably be marked as marts. Let's discuss base_moderator_actions_checkuser a bit more deeply though. As a longer-tem goal, I'd be inclined to not expose wmf_raw tables to dbt, but rather have dbt models be built on well-supported, already-sanitized data with longer retention. So in addition to the discussion of whether it is an intermediate or a mart, I'd like to see what it would take to move it to a different source (which Data Engineering can provide), which would also avoid the retention problem.

Something we noticed elsewhere and wanted to bring to attention:

...

I did notice that there are many partioning strategies over that set of tables. From P96661:

-- Three partition shapes:
--   day DATE:                 int_known_clients_daily, int_traffic_pathways_daily, stg_known_clients_api_requests
--   year, month, day INT:     int_pageview_actor_daily, int_webrequest_requestctl_daily, stg_pageview_actor,
--                             stg_webrequest_requestctl_pageviews, stg_webrequest_upload_bandwidth
--   hour TIMESTAMP:           stg_pageview_actor_clients, stg_webrequest_media_clients, stg_webrequest_thumbnail_serving

We may want to unify for consistency, but also for ease of automation. But that is a problem for another day.

Recording here a couple of main points from last week's conversation at the dbt office hours. cc @KCVelaga_WMF @CMyrick-WMF

The staging, intermediate and mart distinction is flexible; while we have guidelines in the style guide, they focus on how to structure models of each kind, leaving space for us to still agree on things like "what is intermediate for, exactly?" in the context of our work. This influences the actual platform support (e.g. if we agree "intermediate" is for dashboards, they do need to be kept for longer!)

After looking at a couple of options, we ended up with the following proposal:

  • Staging models are ephemeral cleanup or convenience steps, therefore should be implemented as views, and when materialized, expire very quickly; let's say 1 day.
  • Intermediate models are primarily meant to support the development of marts; they're ephemeral debugging tools you'd check if a dashboard/mart looks off, but they're not meant to be exposed to dashboards or continuously used for analysis. dbt docs actually suggest defaulting to views here, but given the intent to support troubleshooting, I can see how having an actual table can be helpful. A 30 day retention for materialized intermediates seemed like a reasonable starting point, to be lowered if that creates too much storage pressure.
  • Marts are the public outputs that feed dashboards and analysis. Lowering retention for marts is not in scope here, but I think we shouldn't do anything by default.
  • The staging and intermediate stages are optional. Any of the three can be public to other dbt models.

In short, anything that needs to be retained for longer than 30 days should be a mart, and dashboards should rely on marts only. How does that sound?

@GGoncalves-WMF thanks for summarizing the discussion. All that sounds goods.

30 day retention for materialized intermediates seemed like a reasonable starting point

I am just wondering if it'd be good to add 3-5 days days as buffer.

  • dbt daily → produces int_ aggregated data each day → another monthly model run on top of all the intermediate data in the previous month.

In practice, if there is some delay and the monthly job runs on Day 2, the data for Day 1 of the previous month would have been deleted.

dbt daily → produces int_ aggregated data each day → another monthly model run on top of all the intermediate data in the previous month.

Hm, interesting, I understand the generic pattern but might need to know more about the specifics (we can go into more detail at office hours or so?). I'll ask a couple of questions about the pattern below; sorry if they're obvious, just making sure I understand the use case.

Is that last model/mart monthly because it needs the data for the entire preceding month? Like, would it make sense to run it more frequently, incrementally per week (for example)? It would still presumably be possible to have a monthly-aggregate mart produced from the weekly-aggregated mart, if we needed that.

Another alternative: Do we know that we would need a materialized intermediate in this case at all? Could this be just a monthly materialized mart?

I do agree that we should account for some transient failures in the various dbt runs, just making sure the pattern you're going for here is the one we should be encouraging!

Hm, interesting, I understand the generic pattern but might need to know more about the specifics (we can go into more detail at office hours or so?). I'll ask a couple of questions about the pattern below; sorry if they're obvious, just making sure I understand the use case.

No worries, it's helpful to think through the details :) Let me explain a specific example here:

  • dbt_dumps_analytics_daily
    • int_event_webrequest_dumps_enriched (builds from event.webrequest_dumps_v1) --> it has processed logs: determining dump type, wiki, snapshot, User-Agent joins etc.
    • (further produces to several other daily aggregated datasets), for example mrt_dumps_traffic_daily
  • dbt_dumps_analytics_monthly
    • mrt_dumps_traffic_topk_clients_monthly: to determine top 100 clients (UA+IP) from Dumps traffic during the previous month.
      • this runs based on the data in int_event_webrequest_dumps_enriched

Is that last model/mart monthly because it needs the data for the entire preceding month? Like, would it make sense to run it more frequently, incrementally per week (for example)? It would still presumably be possible to have a monthly-aggregate mart produced from the weekly-aggregated mart, if we needed that.

Although we can do it, I doubt if producing a top 100 clients dataset every week would be of much use, and they can't exactly be summed up to a month.

Also, the determining the top N clients by traffic is a usecase that I anticipate will come up frequently. Given all the API datasets we have now, I do want to automate the monthly API outliers (for WME lead generation) in the near future, which might have a similar setup.

We can discuss more during the office hours.