惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

U
Unit 42
博客园 - Franky
T
Tailwind CSS Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
月光博客
月光博客
人人都是产品经理
人人都是产品经理
雷峰网
雷峰网
Hugging Face - Blog
Hugging Face - Blog
有赞技术团队
有赞技术团队
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
阮一峰的网络日志
阮一峰的网络日志
C
Check Point Blog
爱范儿
爱范儿
T
The Blog of Author Tim Ferriss
aimingoo的专栏
aimingoo的专栏
Stack Overflow Blog
Stack Overflow Blog
博客园 - 聂微东
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
L
LangChain Blog
云风的 BLOG
云风的 BLOG
MyScale Blog
MyScale Blog
Microsoft Security Blog
Microsoft Security Blog
The Cloudflare Blog
博客园 - 三生石上(FineUI控件)

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Building Internal Events in Django Without Kafka or RabbitMQ
seyedali moradi (call me Sam) · 2026-06-26 · via DEV Community

seyedali moradi (call me Sam)

When we hear about event driven architecture, we usually jump into tools like Kafka or RabbitMQ.

But in practice, most backend systems don’t need distributed messaging infrastructure at first.
What is needed is something much simpler:
A way to decouple business logic inside a single application.
This is where internal events will help us.

The Problem: Tight Coupling in Django Services

When a Django application grows, service methods tend to accumulate responsibilities.
A single business action triggers multiple side effects:

  • Updating database records
  • Invalidating cache
  • Sending notifications
  • Writing audit logs

At first, this looks manageable:

def update_order(order_data):
    order = save_order(order_data)
    update_cache(order)
    send_notification(order)
    write_audit_log(order)
    return order

But over time, this pattern becomes painful:

  • Every new requirement modifies core logic
  • Testing becomes harder
  • Side effects are tightly coupled
  • Business logic is buried under orchestration code The real issue is not complexity itself — it’s dependency direction.

Introducing Internal Events (No Kafka Required)

To solve this problem we implemented a simple internal event system inside Django.
No external infrastructure needed.
No message broker.

Step 1: Define Event Types
We defined events as simple objects like this:

class OrderCreatedEvent:
    def __init__(self, order_id, user_id):
        self.order_id = order_id
        self.user_id = user_id

Step 2: Create an Event Bus
A basic in-memory dispatcher:

from collections import defaultdict

class EventBus:
    def __init__(self):
        self._handlers = defaultdict(list)

    def subscribe(self, event_type, handler):
        self._handlers[event_type].append(handler)

    def publish(self, event):
        event_type = type(event)
        for handler in self._handlers[event_type]:
            handler(event)

Step 3: Define Handlers (Consumers)
Each side effect becomes its own handler:

def update_cache(event):
    pass


def send_notification(event):
    pass


def write_audit_log(event):
    pass

Step 4: Wire Everything Together

event_bus = EventBus()
event_bus.subscribe(OrderCreatedEvent, update_cache)
event_bus.subscribe(OrderCreatedEvent, send_notification)
event_bus.subscribe(OrderCreatedEvent, write_audit_log)

Step 5: Publish Events From Business Logic
Now business logic becomes much cleaner:

def create_order(order_data):
    order = save_order(order_data)

    event_bus.publish(
        OrderCreatedEvent(order.id, order.user_id)
    )

    return order

What Changed?
So there is no complexity removing, instead we moved:
Before:

  • Business logic + side effects mixed together
  • Hard to extend without modifying core code

After:

  • Business logic focuses on what happened
  • Side effects are independent handlers

Why This Work:
We didn’t need:

  • Distributed messaging
  • Broker infrastructure
  • Event persistence
  • Network reliability guarantees

Because our scope was a single Django system.
Internal events are enough when:

  • You are inside a monolith
  • You want decoupling, not distribution
  • You want testable side effects
  • You want flexibility without infrastructure overhead

Important Limitations
This approach is not a replacement for Kafka or RabbitMQ.
It doesn't give you:

  • Persistence of events
  • Cross-service communication
  • Guaranteed delivery
  • Fault tolerance across machines

It is purely in-process. That’s the tradeoff.

When You Eventually Outgrow It
At some point, you may need:

  • Async processing
  • Distributed consumers
  • Event replay
  • High reliability guarantees

That’s when tools like Celery, RabbitMQ, or Kafka become relevant.
But the internal event model still helps because:
The architecture is already event-shaped.
So migration becomes easier.
And definitely helps you ship faster.

Finally
Many problems can be solved by changing structure, not adding tools.
Internal events are one of those cases.
They give you:

  • Decoupling
  • Flexibility
  • Cleaner business logic Without operational overhead.

And sometimes, that’s what a growing system needs.