온보딩 기간동안 API response time을 줄이기 위한 작업을 진행하였고, 간단하게 성과를 볼 수 있는 코드를 분석하여 정리하였습니다.
📌 bulkWrite 사용 예
const a = ids.map((id) => ({
updateOne: {
filter: {
_id: id,
},
update: {
$set: {
deleted: new Date(),
},
},
},
})) as any[];
const result = await test.bulkWrite(a, { session: this.session });
bulkWrite를 사용하여 대량의 데이터를 soft delete하고 있었습니다📌 updateMany 사용 예
const result = await Items.updateMany(
{ _id: ids },
{ deleted: timestamp },
{ new: true, session: this.session },
);
bulkWrite로 처리되던 soft delete를 updateMany를 통해 구현하였습니다 await this.itemDao.updateAll(...);
await this.itemDao.trashAll(...);
const updatedGroup = await this.groupDao.updateTrash(...);
const [updatedGroup] = await Promise.all([
this.groupDao.updateTrash(...),
this.itemDao.trashUpdateAll(...),
this.itemDao.trashAll(...),
]);
Promise.all을 사용하여 병렬적으로 수행하도록 수정하였습니다.1600개 restore Items API(상단 기존 API, 하단 refactor API)


1600개 deleteForever Items API(상단 기존 API, 하단 refactor API)






5. 800개 delete items(상단 기존 API, 하단 refactor API)


⭐️ 결론
bulkwrite는 하나의 컬렉션에서 한번에 여러개의 crud작업을 처리하고자 할때 적합한 선택지이다. 따라서 대량의 데이터는 insertmany, updatemany, deletemany로 처리하고 한번에 crud작업을 진행하고자 할 때 혹은 업데이트 내용이 각각 다를 때, bulkwrite로 처리하는게 좋다고 생각합니다.
순서가 연관되지 않은 비동기 작업들은 병렬적으로 처리할 수 있다면 병렬적으로 처리합시다.
1, 2의 방법만으로도 대략 50%의 response time을 개선할 수 있었습니다.