Spring kafka setErrorHandler deprecated replacement (boot 2.6.4)
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
If you are upgrading a Spring Boot 2.6.4 application and see that setErrorHandler is deprecated, the replacement is setCommonErrorHandler. In practice, the most common implementation you configure there is DefaultErrorHandler.
The reason for the change is API consolidation. Spring Kafka moved away from separate legacy handler types for record and batch listeners and introduced CommonErrorHandler as the unified container-level contract.
What to Replace
Older code often looked like this:
In the newer API, the equivalent container-level configuration is:
If your old handler logic was retry-oriented, the usual concrete replacement is DefaultErrorHandler.
Typical Replacement with DefaultErrorHandler
Here is a common Spring configuration:
This configuration retries a failed record with a one-second delay and two retry attempts before recovery logic takes over.
Why CommonErrorHandler Replaced setErrorHandler
The old API had several related handler interfaces, which made configuration harder to reason about. CommonErrorHandler gives the container one main error-handling contract for both record listeners and batch listeners.
That means:
- one container-level entry point instead of separate legacy methods
- clearer support for retries, backoff, and recovery
- a more consistent way to configure listener containers across use cases
This change is about container behavior, not listener-specific business logic. If you need per-listener exception mapping, KafkaListenerErrorHandler is still a different feature and serves a different purpose.
When DefaultErrorHandler Is Enough
DefaultErrorHandler is a good default when you want standard retry and recovery behavior. It can classify exceptions, apply backoff, and work with recoverers such as a dead-letter publishing recoverer.
If your application already has custom behavior based on exception type or record content, you may still create your own CommonErrorHandler implementation. But many upgrades only need a straightforward swap from the deprecated setter to setCommonErrorHandler(new DefaultErrorHandler(...)).
That is why migration is usually more about configuration intent than about syntax. If the old application used the container to retry records, seek offsets, or recover failures, those responsibilities still belong at the container level after the upgrade.
For example, many teams pair DefaultErrorHandler with a recoverer that sends failed records to a dead-letter topic after retries are exhausted. That keeps the listener code focused on business logic while the container manages operational retry policy.
That separation is the main reason the new API is easier to maintain. Retry behavior, backoff, recovery, and offset handling stay in one predictable place instead of leaking into listener methods.
Common Pitfalls
- Replacing
setErrorHandlerwith listener-levelerrorHandleron@KafkaListener. They are different layers. - Migrating the method name but not the handler type. The newer setter expects a
CommonErrorHandler. - Forgetting that container-level retry and backoff belong in the factory configuration, not inside the listener method itself.
- Assuming the deprecation is only cosmetic. It reflects a real shift toward the unified
CommonErrorHandlerAPI.
Summary
- In Spring Boot
2.6.4era applications, the replacement forsetErrorHandlerissetCommonErrorHandler. - '
DefaultErrorHandleris the most common concrete replacement.' - The new API unifies container-level handling for both record and batch listeners.
- Listener-specific
KafkaListenerErrorHandleris a separate feature and not a drop-in replacement. - Most migrations are a combination of a setter rename and a handler-type update.
Related reading
- Spring Kafka SSL setup in Spring boot application.yml
- Spring Kafka Test - Not receiving data in @KafkaListener with EmbeddedKafka
- Spring Kafka The class is not in the trusted packages
- spring kafka thorws InstanceAlreadyExistsException exception after setting concurrency > 1
- Spring @KafkaListener and concurrency
- Spring KafkaListener How to know when it's ready
- Spring RabbitMQ - using manual channel acknowledgement on a service with RabbitListener configuration
- Spring RabbitTemplate - How to create queues automatically upon send

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.