Redesigning Pinterest’s Advert Serving Methods with Zero Downtime | by Pinterest Engineering | Pinterest Engineering Weblog | Jun, 2024

Pinterest Engineering
Pinterest Engineering Blog

Ning Zhang; Principal Engineer | Ang Xu; Principal Machine Studying Engineer | Claire Liu; Employees Software program Engineer | Haichen Liu; Employees Software program Engineer | Yiran Zhao; Employees Software program Engineer | Haoyu He; Sr. Software program Engineer | Sergei Radutnuy; Sr. Machine Studying Engineer | Di An; Sr. Software program Engineer | Danyal Raza; Sr. Software program Engineer | Xuan Chen; Sr. Software program Engineer | Chi Zhang; Sr. Software program Engineer | Adam Winstanley; Employees Software program Engineer | Johnny Xie; Sr. Employees Software program Engineer | Simeng Qu; Software program Engineer II | Nishant Roy; Supervisor II, Engineering | Chengcheng Hu; Sr. Director, Engineering |

The ads-serving platform is the highest-scale advice system at Pinterest, accountable for delivering >$3B in yearly income and making it one of the crucial enterprise vital programs on the firm! From late 2021 to mid-2023, the Advertisements Infra crew, together with a number of key collaborators, redesigned and rewrote this technique completely from scratch to deal with years of tech debt and lay the foundations for the following 5+ years of audacious enterprise objectives. On this weblog submit, we’ll describe the motivations and challenges of this rewrite, together with our wins and learnings from this two yr journey.

Overview of the Pinterest Advertisements Serving System

The advert serving service sits within the middle of Pinterest’s advert supply funnel. Determine 1 (under) depicts a excessive degree overview of Pinterest’s first model of the adverts serving system known as “Mohawk”. It took a request from the natural aspect and returned top-k advert candidates to be blended into natural outcomes earlier than being despatched to customers for rendering. Internally it acted as a middleware that related different providers, reminiscent of function expander, retrieval, and rating, and at last returned the top-k adverts to customers.

Determine 1. Overview of the Pinterest advert serving system


Rewriting the service on the coronary heart of the enterprise is an costly and dangerous endeavor. This part describes how we arrived at this resolution.

Mohawk, applied in 2014, was Pinterest’s first advert serving system. Throughout its eight-year lifespan, Mohawk turned one of the crucial advanced programs at Pinterest. As of 2022, Mohawk:

  • Served greater than 2 billion advert impressions per day and generated $2.8 billion in advert income
  • Dealt with advert requests from a dozen user-facing surfaces, serving tons of of thousands and thousands of Pinners in over 30 nations
  • Relied on 70+ backends for function/knowledge fetching, predictions, candidate technology, bidding/pacing/price range administration, and many others.
  • Has greater than 380K strains of code and 200+ experiments which are modified by greater than 100 engineers from totally different groups

As our advert enterprise and engineering crew grew quickly, Mohawk accrued important complexities and tech debt. These complexities made the system more and more brittle, leading to a number of eng-weeks misplaced in resolving outages.

Lots of the incidents weren’t due to apparent code bugs, which made them laborious to be captured by unit assessments and even integration assessments. They have been brought on by basic design flaws within the platform reminiscent of:

  1. Shut coupling of infra frameworks and enterprise logic: Easy software logic modifications required a deep data of the infra frameworks.
  2. Lack of correct modularization and possession: Options or performance that ought to have lived in particular person modules have been collocated in the identical directories/information/strategies, making it laborious to outline a superb code possession construction. It additionally resulted in conflicting modifications and code bugs.
  3. No ensures of knowledge integrity: The Mohawk framework didn’t help the enforcement of knowledge integrity constraints, e.g., guaranteeing that ML options are constant between serving and logging.
  4. Unsafe multi-threading: All builders may freely add multi-threaded code to the system with none correct frameworks for error dealing with or race situations, leading to latent software program bugs that have been laborious to detect.

In Q3 2021, we began a working group to resolve whether or not a whole rewrite or a significant refactor was due.

Choice Making

It took us three months to analysis, survey, prototype, and scrutinize totally different choices earlier than lastly making a call to rewrite Mohawk right into a Java-based service. The ultimate resolution was primarily based mostly on two factors:

  1. A significant refactor in place might take extra time than rewriting from scratch. One motive is that the refactor of an internet service must be damaged down into many small code modifications, lots of which have to undergo rigorous experiments to verify they don’t trigger any regressions or outages. This could take days to weeks for every experiment. Then again, a whole rewrite can obtain larger throughput earlier than the ultimate A/B experiment section.
  2. Pinterest natural mixers are all constructed on a Java-based framework. Rewriting the AdMixer service utilizing the identical framework would open the door to unifying natural and adverts mixing for deeper optimization.

With settlement from all Monetization stakeholders, the AdMixer Rewrite mission was kicked off on the finish of 2021.

The objective of the AdMixer Rewrite mission was to construct an adverts platform that enabled tons of of builders to construct new merchandise and algorithms for fast enterprise development whereas minimizing the danger to manufacturing well being. We recognized the next Engineering Design ideas to assist us construct a system that may obtain this objective:

  1. Simply extensible: The framework and APIs have to be versatile sufficient to help extensions to new functionalities in addition to deprecation of outdated ones. Design-for-deprecation is commonly an omitted function, which is why technical programs change into bloated over time.
  2. Separation of issues: Separation of infra framework by defining excessive degree abstractions that enterprise logic can use. Enterprise logic owned by totally different groups must be modularized and remoted from one another.
  3. Protected-by-design: Our framework ought to help the secure use of concurrency and the enforcement of knowledge integrity guidelines by default. For instance, we wish to allow builders to leverage concurrency for performant code whereas guaranteeing there are not any race situations which will trigger ML function discrepancy throughout serving and logging.
  4. Improvement velocity: The framework ought to present well-supported improvement environments and easy-to-use instruments for debugging and analyses.

Design Choices

With these ideas in thoughts, designing a fancy software program programs required us reply these two key questions:

  1. How can we manage the code in order that one crew’s change doesn’t break one other crew’s code?
  2. How can we handle knowledge to ensure correctness and desired properties all through the service?

To answer the above questions, we have to absolutely perceive the present enterprise logic, how knowledge is manipulated, after which construct a excessive degree abstraction on high of it. Determine 1 depicts such a excessive degree instance of code group. Code might be represented right into a directed acyclic graph (DAG) construction. Every node represents a logically coherent piece of enterprise logic. The perimeters between them characterize knowledge dependencies between them. Information is handed from upstream to downstream nodes. With the graph construction, it’s attainable to realize extensibility and improvement velocity as a consequence of higher modularity. To realize safe-by-design, we additionally want to ensure that the info handed by the graph is thread-safe.

Primarily based on the above desired finish state, we made two main design selections:

  1. use an in-house graph execution framework known as Apex to arrange the code into DAGs, and
  2. construct an progressive knowledge mannequin that’s handed within the graph to ensure secure execution.

Because of the area constraints, we merely summarize the ultimate outcomes right here. We encourage readers to consult with the second a part of the weblog submit for the detailed design, implementations, and migration verifications.


We’re proud to report that the AdMixer service has been operating dwell in manufacturing for nearly three full quarters, with no important outages as a part of the migration. This was an enormous achievement for the crew, since we launched proper earlier than the 2023 vacation season, which is historically essentially the most vital a part of the yr for our adverts enterprise.

Wanting again on the objectives we arrange at first: to hurry up product improvements safely with a big crew, we’re blissful to report that we now have achieved all objectives. The Monetization crew has already launched a number of new product options within the new system (e.g., our third get together adverts partnership with Google was developed completely on AdMixer). Now we have grown to have greater than 280 engineers contributing to the brand new codebase. Our developer satisfaction survey (NPS) rating has almost doubled from 46 to 90, indicating extraordinarily excessive developer satisfaction! Lastly, our new service can be operating on extra environment friendly {hardware} (AWS Graviton cases), which resulted in a number of million {dollars} of infra value discount.

Within the second a part of the weblog submit, we’re going to focus on the detailed design selections and the challenges we now have encountered throughout the migration. We hope a few of the learnings are useful to related tasks sooner or later.

We want to thank the next individuals who had important contributions to this mission:

Miao Wang, Alex Polissky, Humsheen Geo, Anneliese Lu, Balaji Muthazhagan Thirugnana Muthuvelan, Hugo Milhomens, Lili Yu, Alessandro Gastaldi, Tao Yang, Crystiane Meira, Huiqing Zhou, Sreshta Vijayaraghavan, Jen-An Lien,Nathan Fong,David Wu, Tristan Nee, Haoyang Li, Kuo-Kai Hsieh, Queena Zhang, Kartik Kapur, Harshal Dahake, Joey Wang, Naehee Kim, Insu Lee, Sanchay Javeria, Filip Jaros, Weihong Wang, Keyi Chen, Mahmoud Eariby, Michael Qi, Zack Drach, Xiaofang Chen, Robert Gordan, Yicheng Ren, Luman Huang, Soo Hyung Park, Shanshan Li, Zicong Zhou, Fei Feng, Anna Luo, Galina Malovichko, Ziyu Fan, Jiahui Ding, Andrei Curelea, Aayush Mudgal, Han Solar, Matt Meng, Ke Xu, Runze Su, Meng Mei, Hongda Shen, Jinfeng Zhuang, Qifei Shen, Yulin Lei, Randy Carlson, Ke Zeng, Harry Wang, Sharare Zehtabian, Mohit Jain, Dylan Liao, Jiabin Wang, Helen Xu, Kehan Jiang, Gunjan Patil, Abe Engle, Ziwei Guo, Xiao Yang, Supeng Ge, Lei Yao, Qingmengting Wang, Jay Ma, Ashwin Jadhav, Peifeng Yin, Richard Huang, Jacob Gao, Lumpy Lum, Lakshmi Manoharan, Adriaan ten Kate, Jason Shu, Bahar Bazargan, Tiona Francisco, Ken Tian, Cindy Lai, Dipa Maulik, Faisal Gedi, Maya Reddy, Yen-Han Chen, Shanshan Wu, Joyce Wang,Saloni Chacha, Cindy Chen, Qingxian Lai, Se Received Jang, Ambud Sharma, Vahid Hashemian, Jeff Xiang, Shardul Jewalikar, Suman Shil, Colin Probasco, Tianyu Geng, James Fish

To be taught extra about engineering at Pinterest, try the remainder of our Engineering Weblog and go to our Pinterest Labs web site. To discover and apply to open roles, go to our Careers web page.