Rewritten in October 2026. An earlier version of this post quoted event volumes, a latency reduction and real-time alert timings that the code and the measurements I can share don't support. This version sticks to what the system actually does.
Twiscope is an Arabic/English media-monitoring, analysis and decision-support platform. I joined as its only backend developer in October 2023 and took on team leadership in August 2024 while remaining the sole backend developer until October 2025. At the point covered by this article, the shared Django/DRF and Celery backend supported five product areas: on-demand reports (Deep-In), influencer monitoring, decision dashboards that score brands against competitors (Sadeed / Decision Hub), an alert engine, and notifications. The public product suite has continued to evolve since then.
These are the decisions that held up, and the ones I'd make earlier next time.
Async from the first endpoint
Collecting and analysing posts from the X API takes anywhere from seconds to minutes, so no report endpoint does that work inline. The request validates input, creates a pending report and enqueues a Celery task; the task checks the subscription quota, collects, analyses, persists and charges usage.
# Simplified
from django.db import transaction
class KeywordReportSerializer(serializers.ModelSerializer):
def create(self, validated_data):
report = KeywordReport.objects.create(status="pending", **validated_data)
transaction.on_commit(
lambda: keyword_analysis.delay(report.id)
) # enqueue after commit
return report
@shared_task
def keyword_analysis(report_id):
report = KeywordReport.objects.get(pk=report_id)
ensure_quota(report.owner, platform="x") # subscription gate
posts = collect(report.query, strategy="report")
save_results(report, analyse(posts))
charge_usage(report.owner, len(posts))Enqueueing after commit matters when report creation runs inside a transaction; otherwise a fast worker can try to read the report before the row is visible to it.
One collection strategy, several consumers
Reports, alerts and Decision Hub all need posts from X, but they need different things afterwards. Instead of three copies of the collection code, there is one strategy selected per caller: reports persist everything, alerts fetch and evaluate without persisting, and Decision Hub collects under its own reply-enrichment budget. Fixes to rate-limit handling or pagination land in one place.
Rate limits are a design input
The X API is rate limited and paid per request, which shapes everything above it. The client honours the API's rate-limit reset header and falls back to capped exponential backoff; subscriptions carry daily and monthly quotas per product and platform; and Decision Hub runs have a per-run collection budget. Alerts are evaluated on schedules (every 15 minutes by default) rather than streamed, which keeps cost predictable at the price of not being real-time.
Chords when scores must be comparable
A Decision Hub centre compares a brand with its competitors. If each entity were collected and scored on its own schedule, the scores would describe different windows. So each run fans out collection for every entity in parallel and scores them all in one callback, at the same instant.
from celery import chord
def run_center(center_id):
entity_ids = Entity.objects.filter(center_id=center_id).values_list("id", flat=True)
chord(collect_entity.s(eid) for eid in entity_ids)(aggregate_center.s(center_id))The scores themselves are deterministic weighted formulas over normalised components — sentiment share, authority, volume, engagement, momentum — not a trained model. When a component can't be computed it is marked absent and the remaining weights are renormalised, instead of silently counting as zero.
Deduplicate where the data lives
Alert scans overlap by design, so the same post can match twice. Deduplication is a unique constraint on (alert, item) in PostgreSQL, with each insert in its own savepoint so a duplicate is a cheap no-op. Events are then indexed in Elasticsearch under the database id, which makes re-indexing idempotent, and a repair command re-indexes anything whose Elasticsearch write failed.
Cache by how stale data is allowed to be
The slowest screens were influencer dashboards aggregating whole categories. The fix was boring and effective: composite indexes built concurrently, a per-user Redis response cache with 5–15 minute TTLs for aggregates that only change when new reports land, a burst throttle, and cache hit/miss metrics so the cache's value is visible rather than assumed.
What I'd do differently
- Make application-test results an explicit deployment gate from the first month. The reviewed workflows enforce commit-bound security checks; application testing is part of the broader delivery process, but its status as a mandatory deployment gate is not independently verified here.
- Give heavy queues their own workers early. One worker container still consumes every main queue, so a burst of one kind of work can delay the others.
- Decide which numbers you'll want to quote before you need them, and instrument for them. Claims you can't measure end up being claims you shouldn't make.
- Sign outgoing webhooks from the start rather than retrofitting it.
Full case study, including the gateway and AWS architecture: /case-studies/twiscope