Skip to the page

Classic problems · senior

Notification service, task scheduler and pub-sub

These problems are small systems with queues and worker threads. Each has a clear flow, retries with backoff, a dead-letter path, and idempotent consumers. Never block the caller on a slow vendor.

The notes

What you must know

  • Notification service: NotificationRequest (user, template, params, priority, idempotency key, send-at), Template, UserPreferences, a Channel interface (Email, SMS, Push), a ChannelFactory and NotificationService.send.
  • Flow: dedupe by idempotency key, check preferences and limits, render, queue by priority (OTP on a fast path), send, record status, retry with backoff and jitter, dead-letter after N tries. Vendors sit behind adapters with a fallback order and a circuit breaker.
  • Limits and quiet hours are a chain of policies. Do not retry OTPs for minutes, because they expire. Never call the vendor synchronously inside the caller request.
  • Task scheduler: Task (run-at, period, priority, status), TaskHandle, a Scheduler interface, a WorkerPool. The queue is a priority queue by run-at, or a DelayQueue. Workers wait on a condition until the head is due, not in a busy loop.
  • Workers catch every exception so one bad task does not kill the thread. Retry with backoff. Fixed-rate and fixed-delay repeats differ. Cancel is a compare-and-set from SCHEDULED to CANCELLED checked before running. Dependencies use in-degree counts. Ageing avoids starvation.
  • Pub-sub: Topic (append-only list of Message), Subscriber, QueueService, and one SubscriberWorker per subscriber with its own offset. Publish appends under a lock and wakes waiters. A slow subscriber delays only itself.
  • Per-subscriber offsets make replay a reset. Offset moves after success gives at-least-once, so consumers must be idempotent. Failures go to a dead-letter topic. Do not deliver on the publisher thread, and do not use one shared queue that pops the message.
  • Shutdown for all three: stop accepting, let running work finish or interrupt it, then join the workers.

The red pen

Common traps: mistake → fix

  • Mistake: A switch on channel inside send is fine

    Fix: use the Channel interface and a factory.

  • Mistake: Busy waiting with while(true) is acceptable

    Fix: wait on a condition or use a DelayQueue.

  • Mistake: One global queue pops the message for all subscribers

    Fix: each subscriber needs its own offset.

  • Mistake: Retry every failure

    Fix: permanent failures and expired OTPs must stop.

Easy to hard

Questions interviewers ask, easy to hard

  1. Design a notification service with email, SMS and push. How do you add a channel?
  2. Design a task scheduler with worker threads. How do workers wait for the next due task?
  3. Design an in-memory pub-sub. How does each subscriber get every message?
  4. How do you stop a retried job from sending twice?
  5. What do you do when a task fails again and again?

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview