Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-sonnet-4-20250514· AnthropicHugging Face LMArena Human Preference 140K

Which the best architecture for module in pure java backend service?

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Claudeclaude-opus-4-20250514-thinking-16k’s answer.
  1. User

    Which the best architecture for module in pure java backend service?

  2. Claude Assistant

    The best architecture for modules in a pure Java backend service depends on your specific requirements, but here are the most effective approaches:

    Layered Architecture with Package-by-Feature This is often the most practical choice for most Java backend services. Organize your modules by business domains rather than technical layers:

    com.yourapp.user
    com.yourapp.order
    com.yourapp.payment
    

    Each module contains its own controllers, services, repositories, and models. This approach promotes high cohesion and makes the codebase easier to understand and maintain.

    Hexagonal Architecture (Ports and Adapters) Excellent for complex business logic and when you need strong separation between your core domain and external dependencies. Structure your modules around:

    • Core domain (business logic, entities, use cases)
    • Ports (interfaces defining contracts)
    • Adapters (implementations for databases, web controllers, external APIs)

    This makes your code highly testable and adaptable to changing requirements.

    Modular Monolith If you're building a larger system, consider using Java modules (JPMS) or Maven/Gradle multi-module projects. Each business capability becomes a separate module with clear dependencies and interfaces between them.

    Key Principles to Follow:

    • Keep modules loosely coupled with well-defined interfaces
    • Use dependency injection (Spring's IoC container works well)
    • Apply SOLID principles, especially Single Responsibility and Dependency Inversion
    • Consider using events for communication between modules to reduce coupling

    For most Java backend services, I'd recommend starting with package-by-feature layered architecture and evolving toward hexagonal architecture as complexity grows. What type of backend service are you building? That would help me give more specific guidance.

  3. User

    Please, give me simple examples

  4. Claude Assistant

    Here are simple examples of different Java backend architectures:

    1. Package-by-Feature (Recommended for most cases)

    // User module
    src/main/java/com/app/user/
    ├── UserController.java
    ├── UserService.java
    ├── UserRepository.java
    └── User.java
    
    // Order module  
    src/main/java/com/app/order/
    ├── OrderController.java
    ├── OrderService.java
    ├── OrderRepository.java
    └── Order.java
    

    UserController.java

    @RestController
    @RequestMapping("/users")
    public class UserController {
        private final UserService userService;
        
        public UserController(UserService userService) {
            this.userService = userService;
        }
        
        @GetMapping("/{id}")
        public User getUser(@PathVariable Long id) {
            return userService.findById(id);
        }
    }
    

    UserService.java

    @Service
    public class UserService {
        private final UserRepository userRepository;
        
        public UserService(UserRepository userRepository) {
            this.userRepository = userRepository;
        }
        
        public User findById(Long id) {
            return userRepository.findById(id)
                .orElseThrow(() -> new UserNotFoundException(id));
        }
    }
    

    2. Hexagonal Architecture

    // Core domain
    src/main/java/com/app/domain/
    ├── User.java
    ├── UserService.java (use case)
    └── ports/
        ├── UserRepository.java (port)
        └── NotificationService.java (port)
    
    // Adapters
    src/main/java/com/app/adapters/
    ├── web/UserController.java
    ├── persistence/JpaUserRepository.java
    └── notification/EmailNotificationService.java
    

    Domain Service (Use Case)

    @Service
    public class UserService {
        private final UserRepository userRepository;
        private final NotificationService notificationService;
        
        public User createUser(String email, String name) {
            User user = new User(email, name);
            User savedUser = userRepository.save(user);
            notificationService.sendWelcomeEmail(savedUser);
            return savedUser;
        }
    }
    

    Port (Interface)

    public interface UserRepository {
        User save(User user);
        Optional<User> findById(Long id);
        Optional<User> findByEmail(String email);
    }
    

    Adapter Implementation

    @Repository
    public class JpaUserRepository implements UserRepository {
        private final JpaRepository<UserEntity, Long> jpaRepo;
        
        @Override
        public User save(User user) {
            UserEntity entity = UserMapper.toEntity(user);
            UserEntity saved = jpaRepo.save(entity);
            return UserMapper.toDomain(saved);
        }
    }
    

    3. Multi-Module Maven Project

    Root pom.xml

    <modules>
        <module>user-service</module>
        <module>order-service</module>
        <module>common</module>
    </modules>
    

    user-service/pom.xml

    <dependencies>
        <dependency>
            <groupId>com.app</groupId>
            <artifactId>common</artifactId>
            <version>1.0.0</version>
        </dependency>
    </dependencies>
    

    Module Structure:

    user-service/
    ├── src/main/java/com/app/user/
    │   ├── UserController.java
    │   ├── UserService.java
    │   └── User.java
    └── pom.xml
    
    order-service/
    ├── src/main/java/com/app/order/
    │   ├── OrderController.java
    │   ├── OrderService.java
    │   └── Order.java
    └── pom.xml
    

    4. Simple Event-Driven Communication

    Event Publisher

    @Service
    public class OrderService {
        private final ApplicationEventPublisher eventPublisher;
        
        public Order createOrder(OrderRequest request) {
            Order order = new Order(request);
            Order savedOrder = orderRepository.save(order);
            
            // Publish event instead of direct coupling
            eventPublisher.publishEvent(new OrderCreatedEvent(savedOrder.getId()));
            return savedOrder;
        }
    }
    

    Event Listener

    @Component
    public class OrderEventListener {
        private final NotificationService notificationService;
        
        @EventListener
        public void handleOrderCreated(OrderCreatedEvent event) {
            notificationService.sendOrderConfirmation(event.getOrderId());
        }
    }
    

    The package-by-feature approach is usually the best starting point. It's simple, intuitive, and scales well for most Java backend services.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.