Django va Celery: Xu ly Tac vu Bat dong bo va Cau hoi Phong van 2026

Huong dan Django Celery day du voi ma code thuc te, task routing, lap lich Celery Beat, cau hinh production va cau hoi phong van ky thuat 2026.

Django va Celery xu ly tac vu bat dong bo

Django va Celery tao thanh nen tang cua xu ly tac vu bat dong bo trong cac ung dung web Python. Voi Celery 5.6 va Django 6.0 deu phat hanh cac ban cap nhat lon trong nam 2026, viec hieu ro hang doi tac vu phan tan van la ky nang quan trong cho phong van backend va he thong san xuat.

Diem Mau Chot

Celery tach roi cac thao tac ton thoi gian khoi chu ky request-response. Mot view Django gui tac vu den message broker (Redis hoac RabbitMQ), va mot tien trinh worker rieng biet thuc thi no mot cach bat dong bo. Mo hinh nay xu ly gui email, tao bao cao, xu ly hinh anh, va bat ky khoi luong cong viec nao ma binh thuong se chan nguoi dung.

Cach Celery Tich hop voi Django

Celery hoat dong nhu mot tien trinh doc lap chia se codebase cua du an Django. Viec tich hop dua vao ba thanh phan: ung dung Django (producer), message broker (Redis hoac RabbitMQ), va mot hoac nhieu Celery worker (consumer). Khi mot view Django goi .delay() hoac .apply_async(), tac vu duoc serialize va day vao hang doi broker. Worker nhan no va thuc thi ham trong tien trinh rieng cua minh.

Thiet lap bat dau voi module celery.py tai thu muc goc du an Django:

python
# myproject/celery.py
import os
from celery import Celery

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')

app = Celery('myproject')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()

Loi goi autodiscover_tasks() quet tat ca cac ung dung Django da cai dat de tim module tasks.py, dang ky tat ca cac ham duoc decorate nhu tac vu Celery mot cach tu dong.

Trong settings.py, broker va result backend duoc cau hinh:

python
# myproject/settings.py
CELERY_BROKER_URL = 'redis://localhost:6379/0'
CELERY_RESULT_BACKEND = 'redis://localhost:6379/1'
CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = 'UTC'

Redis dam nhan ca hai vai tro broker va result backend trong hau het cac deployment Django. RabbitMQ van la lua chon tot hon cho cac ung dung can quorum queues hoac routing nang cao, tinh nang duoc ho tro day du tu Celery 5.5.

Viet va Gui Tac vu Celery

Mot tac vu Celery la mot ham Python binh thuong duoc decorate bang @shared_task. Decorator shared_task tranh viec hardcode instance ung dung Celery, giup tac vu co the tai su dung tren cac ung dung Django.

python
# orders/tasks.py
from celery import shared_task
from django.core.mail import send_mail
from orders.models import Order

@shared_task(bind=True, max_retries=3, default_retry_delay=60)
def send_order_confirmation(self, order_id: int) -> str:
    """Send confirmation email for a completed order."""
    try:
        order = Order.objects.select_related('user').get(id=order_id)
        send_mail(
            subject=f'Order #{order.id} Confirmed',
            message=f'Your order totaling {order.total} has been confirmed.',
            from_email='noreply@example.com',
            recipient_list=[order.user.email],
        )
        return f'Email sent for order {order_id}'
    except Order.DoesNotExist:
        return f'Order {order_id} not found'
    except Exception as exc:
        # Retry on transient failures (SMTP timeout, etc.)
        raise self.retry(exc=exc)

Ba mo hinh quan trong xuat hien o day. Thu nhat, bind=True cho tac vu quyen truy cap vao self, cho phep retry. Thu hai, ham nhan integer order_id thay vi doi tuong ORM — instance model khong the serialize mot cach dang tin cay qua ranh gioi tien trinh. Thu ba, self.retry(exc=exc) day tac vu tro lai hang doi voi backoff luy thua khi gap loi tam thoi.

Gui tu view Django:

python
# orders/views.py
from orders.tasks import send_order_confirmation

def complete_order(request, order_id):
    # ... process payment, update order status ...
    send_order_confirmation.delay(order_id)
    return redirect('order_success', order_id=order_id)

Loi goi .delay() tra ve ngay lap tuc. Nguoi dung thay trang thanh cong trong khi tac vu email chay o nen.

Task Routing va Quan ly Hang doi

He thong san xuat khong nen day tat ca tac vu vao mot hang doi mac dinh duy nhat. Mot tac vu tao bao cao cham co the lam doi tac vu thong bao nhanh neu chung chia se cung pool worker. Celery giai quyet dieu nay bang task routing.

python
# myproject/settings.py
CELERY_TASK_ROUTES = {
    'orders.tasks.send_order_confirmation': {'queue': 'notifications'},
    'reports.tasks.generate_monthly_report': {'queue': 'reports'},
    'images.tasks.resize_upload': {'queue': 'media'},
}

Worker sau do duoc khoi dong theo tung hang doi:

bash
# Start notification worker (fast tasks, high concurrency)
celery -A myproject worker -Q notifications -c 8 --loglevel=info

# Start report worker (slow tasks, limited concurrency)
celery -A myproject worker -Q reports -c 2 --loglevel=info

# Start media worker (CPU-bound, prefork pool)
celery -A myproject worker -Q media -c 4 -P prefork --loglevel=info

Viec tach biet nay ngan chan tranh chap tai nguyen. Worker thong bao xu ly throughput cao voi 8 thread dong thoi, trong khi worker bao cao chi chay 2 tac vu dong thoi de tranh can kiet bo nho tu cac truy van dataset lon.

Sẵn sàng chinh phục phỏng vấn Django?

Luyện tập với mô phỏng tương tác, flashcards và bài kiểm tra kỹ thuật.

Tac vu Dinh ky voi Celery Beat

Celery Beat la bo lap lich tich hop cho cac tac vu lap lai. No chay nhu mot tien trinh rieng biet day tac vu den broker theo cac khoang thoi gian da cau hinh.

python
# myproject/settings.py
from celery.schedules import crontab

CELERY_BEAT_SCHEDULE = {
    'cleanup-expired-sessions': {
        'task': 'accounts.tasks.cleanup_expired_sessions',
        'schedule': crontab(hour=3, minute=0),  # Daily at 3 AM UTC
    },
    'sync-inventory': {
        'task': 'inventory.tasks.sync_external_inventory',
        'schedule': 300.0,  # Every 5 minutes
    },
    'generate-weekly-digest': {
        'task': 'notifications.tasks.send_weekly_digest',
        'schedule': crontab(hour=9, minute=0, day_of_week='monday'),
    },
}

Beat duoc khoi dong cung voi worker:

bash
celery -A myproject beat --loglevel=info

Loi thuong gap trong san xuat: chay nhieu instance Beat gay ra thuc thi tac vu trung lap. Chi mot tien trinh Beat nen chay cho moi deployment. Cong cu nhu django-celery-beat luu tru lich trinh trong database, cho phep thay doi runtime thong qua Django admin.

Giam sat va Quan sat voi Flower

Flower cung cap dashboard web thoi gian thuc de giam sat worker, tac vu, va hang doi Celery. Flower hien thi ty le thanh cong tac vu, thoi gian thuc thi, do sau hang doi, va trang thai worker.

bash
# Install and run Flower
pip install flower
celery -A myproject flower --port=5555

Trong san xuat, cac so lieu Flower nen duoc dua vao he thong canh bao. Do sau hang doi tang voi cac tac vu cu chi ra van de cong suat worker. Ty le retry cao tren cac tac vu cu the cho thay su khong on dinh cua dich vu ben ngoai.

Celery 5.6 cung gioi thieu cac cai tien structured logging tich hop voi cac bo tong hop log JSON nhu Datadog hoac ELK stack, mang lai hieu qua debug tot hon 30% theo ghi chu phat hanh.

Framework Tac vu Tich hop Django 6.0 so voi Celery

Django 6.0 phat hanh framework tac vu nen native, dat ra cau hoi lieu Celery con can thiet hay khong. Cau tra loi phu thuoc vao truong hop su dung.

Framework tac vu tich hop cua Django xu ly cac thao tac nen don gian — gui email, xoa cache, xu ly du lieu nhe — ma khong can co so ha tang broker rieng. Task runner duoc xay dung ben trong chinh Django.

Celery van can thiet cho:

  • Xu ly phan tan tren nhieu may
  • Hang doi uu tien va task routing
  • Rate limiting va chinh sach retry nang cao
  • Lap lich dinh ky (Celery Beat)
  • Theo doi ket qua voi cac backend co the cau hinh
  • Canvas workflows (chains, groups, chords) cho dieu phoi tac vu phuc tap

Doi voi cac ung dung chi can cong viec nen fire-and-forget, giai phap tich hop cua Django 6.0 giam do phuc tap van hanh. Doi voi bat ky thu gi lien quan den worker phan tan, lap lich, hoac to hop tac vu, Celery 5.6 la lua chon da duoc chung minh.

Cau hoi Phong van: Django va Celery

Phong van ky thuat cho vai tro backend thuong kiem tra kien thuc Celery. Duoi day la cac cau hoi lay tu cac tinh huong phong van Django thuc te tai cac cong ty tu startup den FAANG.

H: Tai sao tac vu nen nhan primary key thay vi instance model?

Instance model Django mang theo ket noi database, queryset, va cac quan he lazy-loaded khong the ton tai qua serialization. Truyen order_id thay vi doi tuong Order dam bao tac vu lay du lieu moi tu database, tranh trang thai cu va loi serialization.

H: self.retry() khac gi so voi viec gui lai tac vu thu cong?

self.retry() bao toan so lan retry, ap dung gioi han max_retries da cau hinh, va su dung backoff luy thua theo mac dinh. Goi .delay() thu cong lan nua tao mot tac vu hoan toan moi voi bo dem retry duoc dat lai, co the dan den vong lap retry vo han khi gap loi keo dai.

H: Dieu gi xay ra khi Celery worker bi crash giua chung tac vu?

Hanh vi phu thuoc vao thiet lap acks_late. Voi acks_late=True, broker gui lai thong diep den worker khac vi acknowledgment chua bao gio duoc gui. Voi mac dinh acks_late=False, thong diep duoc acknowledge truoc khi thuc thi, nen crash co nghia la tac vu bi mat. He thong san xuat xu ly khoi luong cong viec quan trong nen su dung acks_late=True ket hop voi thiet ke tac vu idempotent.

H: Giai thich su khac biet giua .delay(), .apply_async(), va goi tac vu truc tiep.

.delay(*args) la syntactic sugar cho .apply_async(args=args). .apply_async() chap nhan cac tuy chon bo sung nhu countdown, eta, queue, priority, va expires. Goi ham tac vu truc tiep (khong dung .delay()) thuc thi no dong bo trong tien trinh hien tai — huu ich cho testing nhung lam mat muc dich xu ly async.

H: Lop caching tuong tac voi ket qua tac vu Celery nhu the nao?

Ket qua tac vu luu trong Celery result backend co the duoc cache bang framework cache cua Django. View kiem tra cache truoc; neu khong co, no gui tac vu va luu ID AsyncResult. Cac request tiep theo poll result backend thong qua ID da cache cho den khi tac vu hoan thanh, sau do cache output cuoi cung.

Danh sach Kiem tra Deployment San xuat

Deploy Celery cung voi Django doi hoi su chu y den mot so van de van hanh ma phong van cung thuong kiem tra.

python
# myproject/settings.py — Production configuration
CELERY_TASK_ALWAYS_EAGER = False  # Never True in production
CELERY_TASK_ACKS_LATE = True  # Redelivery on worker crash
CELERY_WORKER_PREFETCH_MULTIPLIER = 1  # Fair scheduling
CELERY_TASK_REJECT_ON_WORKER_LOST = True  # Reject on unexpected exit
CELERY_TASK_TIME_LIMIT = 300  # Hard kill after 5 minutes
CELERY_TASK_SOFT_TIME_LIMIT = 240  # SoftTimeLimitExceeded after 4 min
CELERY_WORKER_MAX_TASKS_PER_CHILD = 1000  # Prevent memory leaks
CELERY_BROKER_CONNECTION_RETRY_ON_STARTUP = True

Thiet lap WORKER_MAX_TASKS_PER_CHILD khoi dong lai tien trinh worker sau 1000 tac vu, ngan chan ro ri bo nho — van de ma Celery 5.6 da xu ly rong hon voi cac ban va ro ri bo nho.

Quan ly tien trinh voi systemd hoac supervisor dam bao worker khoi dong lai khi gap loi. Don vi systemd toi gian:

ini
# /etc/systemd/system/celery-worker.service
[Unit]
Description=Celery Worker
After=network.target redis.service

[Service]
Type=forking
User=django
Group=django
WorkingDirectory=/opt/myproject
ExecStart=/opt/myproject/venv/bin/celery -A myproject worker \
    --loglevel=info --concurrency=4 --pidfile=/var/run/celery/worker.pid
ExecStop=/bin/kill -s TERM $MAINPID
Restart=always

[Install]
WantedBy=multi-user.target

Bắt đầu luyện tập!

Kiểm tra kiến thức với mô phỏng phỏng vấn và bài kiểm tra kỹ thuật.

Ket luan

  • Celery 5.6.3 (thang 3 nam 2026) mang den cac ban va ro ri bo nho, structured logging, ho tro quorum queue, va tuong thich psycopg3 — nang cap tu cac phien ban cu de huong loi ngay lap tuc.
  • Luon truyen primary key cho tac vu, khong bao gio truyen doi tuong ORM. Dieu nay ngan chan loi serialization va du lieu cu.
  • Dinh tuyen tac vu den cac hang doi chuyen dung dua tren dac diem khoi luong cong viec. Thong bao nhanh va bao cao cham khong nen canh tranh cho cung worker.
  • Su dung acks_late=True va thiet ke tac vu idempotent cho khoi luong cong viec quan trong phai ton tai qua crash worker.
  • Dat ca time_limit va soft_time_limit tren moi tac vu de ngan worker bi treo khoi tieu thu tai nguyen vo thoi han.
  • Framework tac vu tich hop Django 6.0 xu ly cong viec nen don gian; Celery van can thiet cho xu ly phan tan, lap lich, va canvas workflows.
  • Giam sat do sau hang doi va ty le retry voi Flower hoac tong hop log co cau truc de phat hien van de cong suat truoc khi anh huong den nguoi dung.

Thẻ

#django
#celery
#python
#async
#interview

Chia sẻ

Bài viết liên quan