How can you implement a priority queue mechanism using Kafka?

Instruction: Discuss how Kafka can be leveraged to design a system that prioritizes certain messages over others during processing.

Context: This question assesses the candidate's creativity and technical ability to utilize Kafka's features for implementing advanced messaging patterns, such as priority queuing.

Updated

Official answer

Kafka partition logs preserve append order; they are not a built-in queue that moves a newly urgent record ahead of older records. Model priorities explicitly if the application needs them.

A practical design uses separate high, normal and low priority topics with independent consumer groups or reserved processing capacity. Producers assign priority from an authorized business rule rather than trusting any caller to mark everything urgent. Separate capacity can protect urgent traffic, but it does not establish a global execution order across topics.

If one application schedules all priorities, use bounded buffers and a documented policy such as weighted service or an age-based promotion rule. Strictly serving high priority until empty can starve other work forever. Work already executing cannot automatically be preempted. For example, a 4:1 service allocation gives low-priority work a share under sustained backlog, but is not an absolute latency guarantee.

A standard KafkaConsumer is not thread-safe. Its owner should continue polling within the required interval, pause/resume assigned partitions when buffers fill, and delegate only through a controlled interface. Disable automatic commits when work completes asynchronously. Commit only the next offset after the completed prefix of delivered records for each partition; one later urgent completion must not skip earlier unfinished work.

Define ordering requirements per entity, maximum queue age and overload behavior. Test starvation, rebalance, crash/replay and priority abuse; measure completion latency by priority, not only aggregate throughput.

Consumer ownership, pause and commits

Related Questions