Add batched database writes for offers and impressions via Redis#1167
Open
ericholscher wants to merge 2 commits intomainfrom
Open
Add batched database writes for offers and impressions via Redis#1167ericholscher wants to merge 2 commits intomainfrom
ericholscher wants to merge 2 commits intomainfrom
Conversation
Reduces per-request database load by accumulating offer inserts and AdImpression counter updates in Redis, then flushing them in bulk via a periodic Celery task. This can be enabled globally via ADSERVER_BATCH_DB_WRITES=True or per-publisher via the new Publisher.batch_db_writes field for gradual rollout. Key changes: - New adserver/batch_writer.py module with Redis-backed batching - Offer records queued in Redis and bulk_created on flush - AdImpression increments accumulated as Redis hash counters - Pending offers resolved from Redis on view/click if not yet flushed - New flush_batched_db_writes Celery task with configurable interval - ADSERVER_BATCH_SIZE and ADSERVER_BATCH_FLUSH_SECONDS settings https://claude.ai/code/session_01VSGX1CJZeq6VZsspxFAUeh
Allows enabling impression counter batching independently from offer batching so we can test the lower-risk impression path first. - Publisher.batch_db_writes -> batch_impression_writes + batch_offer_writes - ADSERVER_BATCH_DB_WRITES -> ADSERVER_BATCH_IMPRESSION_WRITES + ADSERVER_BATCH_OFFER_WRITES - is_batch_enabled() -> is_impression_batch_enabled() + is_offer_batch_enabled() - Added historicalpublisher fields to migration - Added tests for edge cases (coverage 94%) https://claude.ai/code/session_01VSGX1CJZeq6VZsspxFAUeh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR implements a batched database write system that accumulates offer and impression counter updates in Redis and flushes them to the database in bulk periodically. This significantly reduces per-request database load for high-traffic ad serving scenarios.
Key Changes
New
batch_writer.pymodule: Core batching logic with functions to:batch_incr_impressions(): Queue impression counter increments in Redis instead of immediate DB writesbatch_create_offer(): Queue offer creation in Redis with automatic flush when batch size is reachedget_pending_offer(): Look up offers still in the Redis queue before they're flushed to DBflush_offer_queue(): Bulk create all pending offers usingbulk_create()flush_impression_counters(): Apply accumulated counter increments to AdImpression records using F() expressionsConfiguration options:
ADSERVER_BATCH_DB_WRITESsetting to enable batching globallybatch_db_writesfield for fine-grained controlADSERVER_BATCH_SIZEsetting to control flush threshold (default 100)ADSERVER_BATCH_FLUSH_SECONDSfor periodic flush schedulingIntegration points:
Advertisement.incr()to use batched writes when enabledAdvertisement._record_base()to queue Offer creation instead of immediate insertget_offer()view to check Redis queue for pending offers before querying DBflush_batched_db_writes()for periodic flushingDatabase migration: Added
batch_db_writesBooleanField to Publisher modelAdmin interface: Exposed
batch_db_writesfield in PublisherAdminComprehensive test suite: 344 lines of tests covering:
Implementation Details
flush_offer_queue()to handle offers flushed individually viaget_pending_offer()get_or_create()+ F() expressions for atomic counter increments on existing AdImpression recordshttps://claude.ai/code/session_01VSGX1CJZeq6VZsspxFAUeh