Skip to content

Issue 50 - Photo Update Worker Idempotent Event Assignment (backend) #329

@23abdul23

Description

@23abdul23

Description

Implement a Kafka consumer worker responsible for safely updating Photo documents with their assigned eventId. This worker consumes internal Kafka messages emitted by the Event Clustering Worker (Issue 3) and applies those decisions to the database.

This worker performs only database mutations and must not contain any clustering or decision-making logic.


Scope

  • Consume internal Kafka messages related to event updates
  • Assign eventId to photos in the database
  • Ensure updates are idempotent and retry-safe
  • Isolate photo update logic from clustering logic

Kafka Topic

  • event-updates — internal topic carrying photo-to-event assignment data

Example message:

{
  "eventId": "evt123",
  "photoIds": ["photo1", "photo2"]
}

Folder & Files

workers/
  photoUpdateWorker.js

services/
  photoUpdateService.js

File Responsibilities

workers/photoUpdateWorker.js

  • Initialize Kafka consumer
  • Subscribe to event-updates topic
  • Parse incoming messages
  • Delegate database updates to the service layer
  • Commit Kafka offsets only after successful updates

Main function:

  • startPhotoUpdateWorker()

services/photoUpdateService.js

Contains only database update logic and must be idempotent.

Key functions to implement:

  • assignEventToPhotos(photoIds, eventId)
  • isPhotoAlreadyAssigned(photoId)

Uses:

  • models/Photo.js

Function Responsibilities & Call Flow

startPhotoUpdateWorker()

Entry point for this worker.

Flow:

  1. Consumes a message from the event-updates topic
  2. Extracts eventId and photoIds
  3. Calls assignEventToPhotos(photoIds, eventId)
  4. Commits Kafka offset on success

assignEventToPhotos(photoIds, eventId)

Assigns the given eventId to each photo listed in photoIds.

What this function does:

  • Iterates over each photoId
  • For each photo:
    • Calls isPhotoAlreadyAssigned(photoId)
    • If not assigned, updates photo.eventId = eventId
  • Ensures safe execution if the same message is processed multiple times

Calls:

  • isPhotoAlreadyAssigned(photoId)

isPhotoAlreadyAssigned(photoId)

Checks whether a photo already has an eventId assigned.

What this function does:

  • Reads the Photo document from the database
  • Returns true if eventId already exists
  • Returns false otherwise

Purpose:

  • Prevents duplicate writes
  • Ensures idempotency under retries

Acceptance Criteria

  • Photos receive correct eventId
  • Processing the same message multiple times is safe
  • No duplicate or conflicting updates occur
  • Worker can be restarted without data corruption
  • Kafka offsets are committed only on success

Notes

Ask me things if Confused on Discord, and refer to ARCHITECTURE.md in root folder.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions