Facebook/Instagram Ad System design

Dilip Kumar
5 min readSep 6, 2024

--

Overview

Paid advertising on social media is an immediate way to boost the reach of your content — particularly when it comes to a power platform like Facebook/Instagram.

Ad System Requirements

We would like to design Ad system at Facebook/Instagram scale with following supporting features.

  1. Allow Advertisers to manage their Ads.
  2. Ad should be accurately target customers based on gender, location, age, language and interests.
  3. The cost Advertiser pay is based on the user made clicks on the Ad.
  4. Support to configure Daily vs Lifetime budget which allow to run the Ad to target right audience until either Ad expired or budget exceeded.
  5. Allow to upload Ad media and write text for Ad.
  6. Ad should be placed as Feeds.
  7. Once Ad is publish, system should run check for policy violations for example obscene or terrorism or fraud ad should be blocked.
  8. Advertisers should compete against each other in real-time auctions to determine which ads appear for a given user.
  9. If a better Ad is available then old Ad should be replaced withing one second.
  10. Provide realtime metrics on Ad performance, including reach, engagement, Ad clicks, and more metrics.

Not in scope

  1. Support video stream, search result, reels, messages etc as Ad placement.
  2. In App instant Experience. Instead we will only support basic media with clickable external link.
  3. Ad click aggregation is a separate design itself, so not a focus for this topic.

High level system design

On high level, we can come up with following system design for Ad System.

  1. Ad Manager: Advertiser uses Ad manager to create and publish Ad. This is a central system can be used by Facebook or Instagram or other system.
  2. Ad Exchange matches advertisers’s Ad with suitable ad spaces based on various criteria. This is a central system can be used by Facebook or Instagram or other system. In essence, the ad exchange acts as a middleman between advertisers and publishers (Facebook or Instagram), facilitating the efficient delivery of targeted ads to users.
  3. Ad space in the context of an ad exchange refers to the specific location on a webpage or app where an advertisement can be displayed. It can be a banner, a sidebar, a pop-up, or any other designated area within the content.
  4. Ad Inventory The entire collection of these ad spaces across the website (Facebook, Instagram etc) constitutes its ad inventory.
  5. Ad Service: When a user loads a page (Facebook or Instagram), the Ad Service sends an ad request to the ad exchange. The ad exchange then evaluates the available ad spaces and selects the most suitable one based on various factors, such as the user’s interests, demographics, and the content of the page.
  6. Render Ad: The ad space is then filled with an advertisement that has been selected through a real-time auction or other bidding process.

Ad Manager

Ad manager should expose following API to create the Ad.

POST /ads/create
Request {
AdvertiserId: xxx
AdAsset: {
Image: xxx
Text: xxx
OnClickUrl: xxxx
}
Budget: {
PerClickPrice: $$$
DailyBudget: $$$
TotalBudget: $$$
}
AdExpirationTime: xxxx
TargetAudience: {
AgeGroup: xxx
Langauge: xxx
Gender: xxx
Location: xxx
}
}

Ad data is written into Ads table. Following is schema for this table.

- AdId  
- AdvertiserId
- AdImage
- AdText
- AdOnClickUrl
- BudgetPerClick
- BudgetDaily
- BudgetTotal
- ExpirationTime
- TargetAgeGroup
- TargetLanugage
- TargetGender
- TargetLocation

AdId will be the primary key for this table and AdvertiserId will be the shardKey.

Ad Exchange

Ad Exchange takes care of matching of Ad to the target audience based on realtime auction.

What is Realtime bidding?

When user loads Facebook page with the Ad Spaces, then following become the target for bidding.

  1. Age group
  2. Gender
  3. Location
  4. Language

It means, if we have x Ads matching these criteria then we can’t display all to the user. At a time we can only display one Ad space to the user. It means for y ad spaces for user, we need to choose y ads from x matching user’s criteria.

Therefore Ad exchange system runs an auction algorithm(bid mechanism) behind scene to find out the winner Ad at realtime.

What should be algorithm for matching?

Generally a machine learning algorithm is used to match the user’s criteria with the Ad target audience criteria. In this post, we will design matching algorithm based on the database system to keep it simple.

Ad Exchange expose following API to fetch the matching.

GET ads/matching
Request {
AgeGroup: xxx
Langauge: xxx
Gender: xxx
Location: xxx
}
Response { AdId: xxx}

It will run following query to run the match.

SELECT AdId, BudgetPerClick
FROM Ads
WHERE TargetAgeGroup = $XX
AND TargetLanugage = $XX
AND TargetGender = $XX
AND TargetLocation = $XX
ORDER BY BudgetPerClick DESC
LIMIT 1

How to display better alternate Ad?

User doesn’t keep refreshing the page again and again. Therefore a bi-directional streaming connection is required so that server can push the new Ads if available. Will take following apporach.

  1. Every time Ad service requests for Ad, Ad Exchange returns the target Ad.
  2. Ad Service also make an entry into RefreshAdQueue with delayed message delivery of 10 seconds. The handler of RefreshAdQueue will return the same query to AdExchange and will push to connected user to display the new Ad.

How to mange expired Ad?

To manage the expired Ad, we will add a message into AdExpirationQueue with delayed delivery of expiration time. Once Ad is expired then Ad will be removed from the Ads table and move into historical storage.

Next time when RefreshAdQueue handler will query Ad Exchange for fresh Ad then this Ad will not be included.

Note: We can optimize it further to notify target user about expired Ad so that it can also be removed from Ad space to replace with new Ad.

How to manage Ad based on user click rate?

If user doesn’t click on Ad then it would be a good idea to show different Ad to the user. We should also publish event into ClickRateTrackQueue with delayed delivery of 24 hours.

Every time user clicks on Ad, we should capture it into AdClicks table. The handler of ClickRateTrackQueue will lookup into AdClicks and if there is no clicks happened in 24 hours for that user then it will remove from Ads table and move into AdsHistory table so that this Ad will no longer will be served to user.

Following is overall design for Ad System’s core functionality.

Note: We we can further optimize Ad matching based on hash. Refer Google F1 paper used by Google to store Ad data.

Ad Metrics

We can come up with following metrics

  1. Number of Ads published by Advertisers
  2. Number of Ad found match with the target users.
  3. Number of time user clicked on Ad
  4. Number of times Ad got unnoticed by user and got expired.
  5. Number of times Ad was replaced by better alternate Ad.

Happy learning :-)

--

--

Dilip Kumar
Dilip Kumar

Written by Dilip Kumar

With 18+ years of experience as a software engineer. Enjoy teaching, writing, leading team. Last 4+ years, working at Google as a backend Software Engineer.

Responses (1)