Database Reference
In-Depth Information
In order to execute the ad choice with the lowest latency possible, we need to maintain a com-
pound index on site_id , zone_id :
>>>
>>> db . ad . zone . ensure_index ([
...
...
( 'site_id' , 1 ),
...
( 'zone_id' , 1 ) ])
Operation: Make an Ad Campaign Inactive
One case we need to deal with is making a campaign inactive. This may happen for a variety
of reasons. For instance, the campaign may have reached its end date or exhausted its budget
for the current time period. In this case, the logic is fairly straightforward:
def
def deactivate_campaign ( campaign_id ):
db . ad . zone . update (
{ 'ads.campaign_id' : campaign_id },
{ ' $pull' : { 'ads' , { 'campaign_id' : campaign_id } } },
multi = True )
This update statement first selects only those ad zones that had available ads from the given
campaign_id and then uses the $pull modifier to remove them from rotation.
To execute the multiupdate quickly, we'll keep an index on the ads.campaign_id field:
>>>
>>> db . ad . zone . ensure_index ( 'ads.campaign_id' )
Sharding Concerns
Inordertoscalebeyondthecapacityofasinglereplicaset,youwillneedtoshardthe ad.zone
collection. To maintain the lowest possible latency (and the highest possible throughput) in
the ad selection operation, the shard key needs to be chosen to allow MongoDB to route the
ad.zone query to a single shard. In this case, a good approach is to shard on the site_id ,
zone_id combination:
>>>
>>> db . command ( 'shardcollection' , 'dbname.ads.ad.zone' , {
...
... 'key' : { 'site_id' : 1 , 'zone_id' : 1 } })
{ "collectionsharded": "dbname.ads.ad.zone", "ok": 1 }
Search WWH ::




Custom Search