프론트엔드와 백엔드의 비동기 처리 비교
비동기 처리 기본 개념
- 작업을 기다리지 않고 다른 작업 수행
- 자원 효율적 활용
- 반응성 향상
- I/O 작업 최적화
프론트엔드 비동기
- 브라우저 단일 스레드 환경
- 사용자 인터페이스 반응성 중심
- Promise, async/await, 이벤트 리스너
- 프레임워크별 비동기 처리(React useEffect, Vue watch)
- 사용자 피드백 중심 에러 처리
- AJAX, fetch API, axios 라이브러리
백엔드 비동기
1. Node.js의 비동기 처리 🟢
- 이벤트 루프 기반 비동기 모델
- 단일 스레드 이벤트 루프 아키텍처
- Non-blocking I/O 작업
- 콜백 함수, Promise, async/await 패턴
- 주요 비동기 API
- fs 모듈: 파일 시스템 비동기 작업
- http/https 모듈: 네트워크 요청
- setTimeout/setInterval: 타이머 함수
- Node.js 비동기 예시
// Promise 기반 비동기 처리
const fs = require('fs').promises;
async function readFile() {
try {
const data = await fs.readFile('file.txt', 'utf8');
console.log(data);
} catch (err) {
console.error('Error:', err);
}
}
// 이벤트 이미터 패턴
const EventEmitter = require('events');
const myEmitter = new EventEmitter();
myEmitter.on('event', (data) => {
console.log('Event triggered:', data);
});
// 비동기적으로 이벤트 발생
setTimeout(() => {
myEmitter.emit('event', 'Hello World');
}, 1000);
- Node.js 비동기 라이브러리
- Express: 비동기 미들웨어 체인
- Mongoose: 비동기 데이터베이스 작업
- Axios: 비동기 HTTP 요청
- Bull, Agenda: 비동기 작업 큐
2. Spring Boot의 비동기 처리 🍃
- 다중 스레드 기반 비동기 모델
- 스레드 풀 관리
- 블로킹 I/O와 논블로킹 I/O 모두 지원
- 스프링 비동기 어노테이션 시스템
- 주요 비동기 기능
- @Async 어노테이션: 메소드 비동기 실행
- CompletableFuture: 비동기 결과 처리
- WebFlux: 리액티브 프로그래밍 지원
- Spring Integration: 비동기 메시징
- Spring Boot 비동기 예시
// @Async 어노테이션 사용
@Service
public class EmailService {
@Async
public CompletableFuture<Boolean> sendEmail(String to, String subject) {
// 이메일 전송 로직
return CompletableFuture.completedFuture(true);
}
}
// WebClient를 사용한 비동기 HTTP 요청
@Service
public class ApiService {
private final WebClient webClient;
public Mono<UserData> getUserData(String userId) {
return webClient.get()
.uri("/users/{id}", userId)
.retrieve()
.bodyToMono(UserData.class);
}
}
@Configuration
@EnableAsync
public class AsyncConfig {
@Bean
public Executor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(5);
executor.setMaxPoolSize(10);
executor.setQueueCapacity(25);
executor.setThreadNamePrefix("Async-");
executor.initialize();
return executor;
}
}
- Spring Boot 비동기 라이브러리
- Spring WebFlux: 리액티브 웹 애플리케이션
- Project Reactor: 리액티브 스트림 구현
- Spring Cloud Stream: 이벤트 기반 마이크로서비스
- Spring AMQP: RabbitMQ 통합
Node.js vs Spring Boot 비동기 처리 비교
아키텍처 차이
- Node.js: 단일 스레드 이벤트 루프, 콜백 기반
- Spring Boot: 다중 스레드 모델, 스레드 풀 관리
성능 특성
- Node.js: I/O 바운드 작업에 최적화, 가벼운 요청 처리 효율적
- Spring Boot: CPU 바운드 작업에도 강점, 복잡한 비즈니스 로직 처리 효율적
개발 패러다임
- Node.js: 함수형 프로그래밍, 이벤트 기반 프로그래밍
- Spring Boot: 객체지향 프로그래밍, 선언적 프로그래밍
비동기 에러 처리
- Node.js: try/catch, Promise 체인의 .catch(), 이벤트 에러 리스너
- Spring Boot: 예외 처리 메커니즘, @ExceptionHandler, 글로벌 에러 핸들러
핵심 차이점 요약
- 실행 환경: 브라우저 vs 서버(Node.js/Spring Boot)
- 스레딩 모델: 단일 스레드(프론트엔드, Node.js) vs 다중 스레드(Spring Boot)
- 최적화 목표: 사용자 경험 vs 시스템 처리량
- 프로그래밍 패러다임: 이벤트 기반(프론트엔드, Node.js) vs 객체지향(Spring Boot)
- 성능 지표: 응답 시간 vs 처리량 vs 자원 활용도