<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[딜리버스 - Medium]]></title>
        <description><![CDATA[우주에서도 가능한 택배를 제공하는 혁신적인 물류 인프라를 구축하고자하는 딜리버스 팀의 블로그입니다. - Medium]]></description>
        <link>https://medium.com/delivus?source=rss----4eecc410ebae---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>딜리버스 - Medium</title>
            <link>https://medium.com/delivus?source=rss----4eecc410ebae---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 23:45:45 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/delivus" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[React Native Camera Kit 바코드 프레임 기능 개선]]></title>
            <link>https://medium.com/delivus/react-native-camera-kit-%EB%B0%94%EC%BD%94%EB%93%9C-%ED%94%84%EB%A0%88%EC%9E%84-%EA%B8%B0%EB%8A%A5-%EA%B0%9C%EC%84%A0-cbea21f50cbc?source=rss----4eecc410ebae---4</link>
            <guid isPermaLink="false">https://medium.com/p/cbea21f50cbc</guid>
            <category><![CDATA[qr-code]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[react-native-camera]]></category>
            <dc:creator><![CDATA[Tayo]]></dc:creator>
            <pubDate>Thu, 24 Jul 2025 08:21:02 GMT</pubDate>
            <atom:updated>2025-07-24T08:21:02.630Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YjGcBZOfFFbNB9Jh1CFNCw.jpeg" /></figure><h3>📋 개요</h3><p>React Native Camera Kit 라이브러리의 바코드 스캔 기능을 사용하면서 발견한 프레임 영역 제한 문제를 해결한 과정을 정리했습니다.</p><p>문제 발견부터 플랫폼 별 네이티브 코드 분석, 수정 작업, 그리고 최종 검증까지의 여정을 담았습니다.</p><p>Android와 iOS 플랫폼 간의 카메라 API 동작 차이를 이해하고 통일된 사용자 경험을 제공하는 것이 주요 목표였습니다.</p><h3>🔍 문제 발견 및 배경</h3><h4><strong>발견된 이슈</strong></h4><p>프로젝트 초기부터 React Native Camera Kit을 카메라 관련 모든 기능(권한 처리, 프리뷰, 촬영)의 메인 라이브러리로 사용해왔습니다. 이 라이브러리는 바코드/QR 코드 스캔 시 시각적 가이드 프레임을 제공하는데, 실제 테스트 과정(풀 스크린이 아닌 특정 사이즈로 기능 사용 시)에서 다음과 같은 문제들이 발견되었습니다.</p><blockquote>첨부된 이미지는 스캔 성공/실패 유무가 아닌 스캔 인식 확인을 위한 이미지입니다.</blockquote><p><strong>Android 플랫폼:</strong></p><ul><li>프레임 외부에 있는 바코드/QR 코드도 빈번하게 인식됨</li><li>가이드 프레임과 실제 인식 영역 사이의 불일치</li></ul><figure><img alt="프레임 내에서 인식되지 않는 현상" src="https://cdn-images-1.medium.com/max/484/1*8YLBRRpI9Jce3zEYdAGUcA.gif" /><figcaption>프레임 안에서 인식되지 않는 현상</figcaption></figure><p><strong>iOS 플랫폼:</strong></p><ul><li>특정 렌더링 타이밍에서 프레임 제어 로직이 아예 무시되는 현상</li><li>카메라 세션 초기화 시점과 프레임 설정 시점의 싱크 문제</li></ul><figure><img alt="페이지 재진입 시 프레임 안에서 인식되지 않는 현상" src="https://cdn-images-1.medium.com/max/484/1*aQftivRmaKwjf2YCaOEO_w.gif" /><figcaption>페이지 재진입 시 프레임 안에서 인식되지 않는 현상</figcaption></figure><p>이런 문제들로 인해 사용자가 의도하지 않은 코드가 스캔되거나, 프레임 안에 정확히 맞춘 코드가 인식되지 않는 상황이 발생했습니다.</p><h4><strong>기술적 선택 고려사항</strong></h4><p>이 문제를 해결하기 위해 2가지 접근 방식을 검토했습니다.</p><p><strong>1. 다른 라이브러리로 교체</strong></p><ul><li>전용 바코드 스캐너 라이브러리 또는 `react-native-vision-camera` 같은 통합 카메라 모듈</li><li>새로운 의존성 관리 및 빌드 설정 복잡성 증가</li><li>팀 내 학습 곡선과 유지보수 비용</li><li>앱 번들 사이즈 증가</li></ul><p><strong>2. 기존 라이브러리 보완 (선택한 방식)</strong></p><ul><li>현재 안정적으로 동작하는 기능들은 그대로 유지</li><li>문제가 되는 부분만 선택적으로 패치</li><li>리스크를 최소화하면서 문제 해결</li></ul><p>결과적으로 기존 스택의 안정성과 팀의 생산성을 고려하여 두 번째 접근 방식을 선택했습니다.</p><h3>🏗️ 전체 해결 과정</h3><figure><img alt="해결 과정 플로우차트" src="https://cdn-images-1.medium.com/max/278/1*zQNkwZ33CYZm_Hih3VKdOw.png" /><figcaption>해결 과정 플로우차트</figcaption></figure><h3>🤖 Android 측 문제 분석 및 해결</h3><h4>문제 원인 심층 분석</h4><p>Android 코드를 분석한 결과, 바코드 스캔 로직의 구조적 문제를 발견했습니다.</p><ol><li><strong>CameraX + ML Kit 조합</strong>에서 이미지 전체를 분석 대상으로 설정</li><li><strong>가이드 프레임</strong>은 단순히 UI 레이어에서 시각적 표시만 담당</li><li><strong>실제 스캔 영역 제한 로직</strong>이 구현되지 않음</li></ol><p><strong>AS-IS:</strong></p><figure><img alt="카메라 프레임 동작 순서" src="https://cdn-images-1.medium.com/max/832/1*vKh6_FzmzjJDCI0H4pSHKw.png" /><figcaption>카메라 프레임 동작 순서</figcaption></figure><h4>해결 방안 설계</h4><p>문제를 해결하기 위해 바운딩 박스 기반 필터링 레이어를 추가했습니다.</p><p>ML Kit에서 감지된 모든 바코드의 바운딩 박스 좌표를 가져와서, 프레임 영역과 교차 검사를 통해 필터링하는 것입니다.</p><p><strong>TO-BE:</strong></p><figure><img alt="카메라 프레임 개선 방안" src="https://cdn-images-1.medium.com/max/1024/1*diSFPQV-6ait8GuJ4od00Q.png" /><figcaption>카메라 프레임 개선 방안</figcaption></figure><h4>구현 상세</h4><p><strong>1. QRCodeAnalyzer 수정</strong></p><p>기존 분석기에 이미지 크기 정보를 추가로 전달하도록 수정했습니다.</p><pre>class QRCodeAnalyzer (<br>    private val onQRCodesDetected: (qrCodes: List&lt;Barcode&gt;, imageSize: Size) -&gt; Unit<br>) : ImageAnalysis.Analyzer {<br>    <br>    @SuppressLint(&quot;UnsafeExperimentalUsageError&quot;)<br>    @ExperimentalGetImage<br>    override fun analyze(image: ImageProxy) {<br>        val mediaImage = image.image ?: return<br>        <br>        val inputImage = InputImage.fromMediaImage(mediaImage, image.imageInfo.rotationDegrees)<br>        <br>        val scanner = BarcodeScanning.getClient()<br>        scanner.process(inputImage)<br>            .addOnSuccessListener { barcodes -&gt;<br>                val strBarcodes = mutableListOf&lt;Barcode&gt;()<br>                barcodes.forEach { barcode -&gt;<br>                    strBarcodes.add(barcode ?: return@forEach)<br>                }<br>                // 이미지 크기 정보와 함께 전달<br>                onQRCodesDetected(strBarcodes, Size(image.width, image.height))<br>            }<br>            .addOnCompleteListener {<br>                image.close()<br>            }<br>    }<br>}</pre><p><strong>2. 프레임 영역 계산 로직</strong></p><p>화면 크기에 따라 동적으로 프레임 크기를 계산하고, 정확한 좌표를 유지하도록 구현했습니다.</p><pre>override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {<br>    super.onMeasure(widthMeasureSpec, heightMeasureSpec)<br>    <br>    val margin = 40<br>    val availableHeight = measuredHeight - margin * 2<br>    val desiredFrameWidth = availableHeight * 2  // 2:1 비율<br>    val availableWidth = measuredWidth - margin * 2<br>    <br>    // 화면 크기에 맞는 프레임 크기 계산<br>    val (frameWidth, frameHeight) = if (desiredFrameWidth &gt; availableWidth) {<br>        val adjustedWidth = availableWidth<br>        val adjustedHeight = adjustedWidth / 2<br>        Pair(adjustedWidth, adjustedHeight)<br>    } else {<br>        Pair(desiredFrameWidth, availableHeight)<br>    }<br>    <br>    // 중앙 정렬<br>    frameRect.left = (measuredWidth - frameWidth) / 2<br>    frameRect.top = (measuredHeight - frameHeight) / 2<br>    frameRect.right = frameRect.left + frameWidth<br>    frameRect.bottom = frameRect.top + frameHeight<br>}</pre><p><strong>3. 핵심 필터링 로직</strong></p><p>가장 복잡한 부분은 카메라 이미지 좌표계를 화면 좌표계로 변환하는 작업이었습니다.</p><pre>val analyzer = QRCodeAnalyzer { barcodes, imageSize -&gt;<br>    if (barcodes.isEmpty()) return@QRCodeAnalyzer<br>    <br>    val barcodeFrameView = barcodeFrame ?: run {<br>        onBarcodeRead(barcodes)<br>        return@QRCodeAnalyzer<br>    }<br>    <br>    // 스케일 팩터 계산<br>    val scaleFactorWidth = viewFinder.width.toFloat() / imageSize.height<br>    val scaleFactorHeight = viewFinder.height.toFloat() / imageSize.width<br>    val scaleFactor = maxOf(scaleFactorWidth, scaleFactorHeight)<br>    <br>    // 오프셋 계산<br>    var offsetX = 0f<br>    var offsetY = 0f<br>    <br>    if (scaleFactorWidth &lt; scaleFactorHeight) {<br>        val scaledWidth = imageSize.height * scaleFactor<br>        offsetX = (scaledWidth - viewFinder.width) / 2<br>    } else {<br>        val scaledHeight = imageSize.width * scaleFactor<br>        offsetY = (scaledHeight - viewFinder.height) / 2<br>    }<br>    <br>    // 바운딩 박스 필터링<br>    val filteredBarcodes = barcodes.filter { barcode -&gt;<br>        val barcodeBoundingBox = barcode.boundingBox ?: return@filter false<br>        <br>        // 좌표 변환<br>        val left = barcodeBoundingBox.left * scaleFactor - offsetX<br>        val top = barcodeBoundingBox.top * scaleFactor - offsetY<br>        val right = barcodeBoundingBox.right * scaleFactor - offsetX<br>        val bottom = barcodeBoundingBox.bottom * scaleFactor - offsetY<br>        <br>        val scaledBarcodeBoundingBox = Rect(left.toInt(), top.toInt(), right.toInt(), bottom.toInt())<br>        <br>        // 프레임 영역 내 완전 포함 여부 확인<br>        barcodeFrameView.frameRect.contains(scaledBarcodeBoundingBox)<br>    }<br>    <br>    if (filteredBarcodes.isNotEmpty()) {<br>        onBarcodeRead(filteredBarcodes)<br>    }<br>}</pre><p><strong>좌표 변환의 복잡성</strong></p><p>Android에서 까다로웠던 부분은 서로 다른 좌표계 간의 변환이었습니다.</p><ol><li><strong>카메라 이미지 좌표계</strong>: ML Kit에서 반환하는 바운딩 박스 좌표</li><li><strong>화면 좌표계</strong>: 실제 뷰파인더와 프레임 영역의 좌표</li><li><strong>회전 및 스케일링</strong>: 디바이스 방향과 카메라 해상도에 따른 변환</li></ol><h3>🍎 iOS 측 문제 분석 및 해결</h3><h4>발견된 타이밍 문제</h4><ol><li><strong>카메라 세션 초기화 시점</strong>과 <strong>프레임 설정 시점</strong>의 불일치</li><li><strong>앱 생명주기 변화</strong>(백그라운드 → 포그라운드)에서 설정 초기화</li><li><strong>뷰 계층 변화</strong>에 따른 프레임 크기 재계산 필요</li></ol><h4>해결 방안: 생명주기 기반 동기화</h4><p>iOS 문제를 해결하기 위해 앱의 생명주기 이벤트를 활용한 동기화 메커니즘을 구현했습니다.</p><pre>override public func viewDidLoad() {<br>    super.viewDidLoad()<br>    <br>    // 앱 재진입 시 스캐너 프레임 업데이트를 위한 옵저버 추가<br>    NotificationCenter.default.addObserver(self,<br>         selector: #selector(handleDidBecomeActive),<br>         name: UIApplication.didBecomeActiveNotification,<br>         object: nil)<br>}<br><br>deinit {<br>    NotificationCenter.default.removeObserver(self)<br>}<br><br>// 화면이 윈도우에 다시 추가될 때 스캐너 프레임 갱신<br>override public func didMoveToWindow() {<br>    super.didMoveToWindow()<br>    if window != nil {<br>        DispatchQueue.main.async {<br>            self.camera.update(scannerFrameSize: self.showFrame ? self.scannerInterfaceView.frameSize : nil)<br>        }<br>    }<br>}<br><br>// 앱이 active 상태로 돌아왔을 때 스캐너의 프레임 영역을 다시 업데이트<br>@objc private func handleDidBecomeActive() {<br>    DispatchQueue.main.async {<br>        self.camera.update(scannerFrameSize: self.showFrame ? self.scannerInterfaceView.frameSize : nil)<br>    }<br>}</pre><h4>iOS 해결 과정 시퀀스</h4><figure><img alt="해결 과정 시퀀스다이어그램" src="https://cdn-images-1.medium.com/max/984/1*3qPYAkxCZ6rgXvT2txJJ2Q.png" /><figcaption>해결 과정 시퀀스다이어그램</figcaption></figure><h4>업데이트 전략</h4><p>iOS에서는 다음과 같은 여러 시점에서 프레임 정보를 업데이트하도록 구현했습니다.</p><ol><li><strong>viewDidLoad</strong>: 초기 옵저버 설정</li><li><strong>didMoveToWindow</strong>: 뷰 계층 변화 시 대응(화면 전환)</li><li><strong>didBecomeActive</strong>: 앱 재진입 시 동기화</li></ol><p>이렇게 업데이트하는 이유는 iOS의 카메라 세션 생명주기와 뷰 생명주기가 완전히 일치하지 않기 때문입니다.</p><h3>🎯 개선 결과 및 검증</h3><h4>기능적 개선사항</h4><p><strong>Android</strong></p><figure><img alt="프레임 안에서만 바코드 인식" src="https://cdn-images-1.medium.com/max/488/1*4yDuueNslRotOVbfnWIDaw.gif" /><figcaption>프레임 안에서만 바코드 인식</figcaption></figure><p><strong>iOS</strong></p><figure><img alt="재진입 시 프레임 안에서만 바코드 인식" src="https://cdn-images-1.medium.com/max/484/1*msQvtyR_6HMuOhZuipqzDA.gif" /><figcaption>재진입 시 프레임 안에서만 바코드 인식</figcaption></figure><p><strong>스캔 정확도 향상</strong></p><ul><li>프레임 영역 완전 포함 여부 기준으로 정확한 바코드 판별</li><li>프레임 외부 또는 경계에 걸친 코드 인식 완전 차단</li></ul><p><strong>UI/UX 통일성</strong></p><ul><li>시각적 가이드와 실제 인식 영역의 일치</li><li>Android와 iOS 간의 동작 차이 해소</li></ul><h4>사용자 경험 개선</h4><ul><li><strong>예측 가능한 동작</strong>: 프레임 안에 코드를 맞추면 반드시 인식</li><li><strong>오탐 제거</strong>: 의도치 않은 코드 스캔으로 인한 혼란 해소</li><li><strong>일관된 경험</strong>: 플랫폼에 상관없이 동일한 사용자 경험</li></ul><h3>💡 프로젝트에서 얻은 인사이트</h3><h4>1. 플랫폼별 카메라 API 특성 이해</h4><p><strong>Android (CameraX + ML Kit)</strong></p><ul><li>후처리 방식의 바운딩 박스 필터링 적합</li><li>세밀한 제어가 가능하지만 구현 복잡도 증가</li></ul><p><strong>iOS (AVFoundation)</strong></p><ul><li>전처리 방식의 관심 영역 설정 방식</li><li>상대적으로 간단하지만 타이밍 이슈 존재</li></ul><h4>2. 오픈소스 라이브러리 활용 시 고려사항</h4><p><strong>문서와 실제 구현의 차이</strong></p><ul><li>공식 문서의 기능 설명이 실제 구현과 다른 경우가 있음</li><li>특히 플랫폼별 구현 차이가 문서에 명시되지 않은 경우 많음</li><li>직접 네이티브 코드 분석이 필요한 상황 발생</li></ul><p><strong>패치 vs 교체 전략</strong></p><ul><li>기존 스택의 안정성과 새로운 기능 도입 사이의 균형</li><li>팀의 학습 곡선과 유지보수 비용 고려</li><li>점진적 개선 vs 전면 교체의 장단점 분석</li></ul><h4>3. 생명주기 관리의 중요성</h4><ul><li><strong>앱 생명주기</strong>와 <strong>컴포넌트 생명주기</strong>의 불일치</li><li><strong>하드웨어 리소스</strong>(카메라)의 초기화 타이밍</li><li><strong>UI 업데이트</strong>와 <strong>기능 로직</strong>의 동기화</li></ul><h3>🔮 향후 개선해볼만한 리스트</h3><h4>1. 사용자 맞춤형 프레임 설정</h4><p>현재는 고정된 2:1 비율 프레임을 사용하고 있지만, 다음과 같은 확장이 가능합니다.</p><ul><li>다양한 프레임 비율 옵션 (1:1, 3:2, 4:3 등)</li><li>프레임 크기 조절 기능</li><li>동적 프레임 위치 조정</li></ul><h4>2. 성능 최적화</h4><p><strong>카메라 포커싱 최적화</strong></p><ul><li>바코드 스캔 시 프레임 영역에 집중된 자동 포커싱</li></ul><h3>🎉 결론</h3><p>이번 React Native Camera Kit 개선 프로젝트는 크로스 플랫폼 개발의 복잡성을 경험할 수 있는 기회였습니다.</p><h4>핵심 성과</h4><ol><li><strong>기술적 성장</strong>: Android와 iOS 네이티브 카메라 API에 대한 실전 경험</li><li><strong>문제 해결 능력</strong>: 좌표 변환과 생명주기 관리 문제 해결</li><li><strong>사용자 경험 개선</strong>: 일관되고 예측 가능한 바코드 스캔 기능 구현</li><li><strong>팀 생산성 향상</strong>: 기존 스택 유지를 통한 안정성과 효율성 확보</li></ol><h4>느낀 점</h4><p><strong>오픈소스 라이브러리 활용</strong></p><ul><li>문서와 실제 구현의 차이를 인지하고 대비해야 함</li><li>네이티브 코드 분석 능력의 중요성</li><li>패치와 교체 전략의 균형잡힌 판단</li></ul><p><strong>크로스 플랫폼 개발의 복잡성</strong></p><ul><li>플랫폼별 API 차이를 이해하고 통일된 경험 제공</li><li>생명주기 관리와 타이밍 이슈의 중요성</li><li>좌표계 변환 등 기본기의 중요성</li></ul><h3>마무리</h3><p>이 글은 당시 버전 기준으로 작성되었으며, <strong>React Native Camera Kit 최신 버전에서는 Android의 프레임 인식 관련 문제에 대한 PR 이 반영되었습니다</strong>. 다만, 프로젝트 요구사항에 따라 여전히 커스터마이징이 필요한 경우에는 본 글의 접근 방식이 참고가 될 수 있습니다. 감사합니다.</p><h4>📚 참고 자료</h4><ul><li><a href="https://github.com/teslamotors/react-native-camera-kit">React Native Camera Kit GitHub</a></li><li><a href="https://developers.google.com/ml-kit/vision/barcode-scanning/android">Android ML Kit Barcode Scanning Guide</a></li><li><a href="https://reactnative.dev/">React Native</a></li><li><a href="https://developer.android.com/training/camerax">CameraX Documentation</a></li></ul><blockquote>※ 일부 문장 표현 정리에 생성형 AI 도구를 활용하였습니다. 기술 내용은 직접 경험을 기반으로 작성되었습니다.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cbea21f50cbc" width="1" height="1" alt=""><hr><p><a href="https://medium.com/delivus/react-native-camera-kit-%EB%B0%94%EC%BD%94%EB%93%9C-%ED%94%84%EB%A0%88%EC%9E%84-%EA%B8%B0%EB%8A%A5-%EA%B0%9C%EC%84%A0-cbea21f50cbc">React Native Camera Kit 바코드 프레임 기능 개선</a> was originally published in <a href="https://medium.com/delivus">딜리버스</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Fluentbit + S3 + Athena + Redash: 실용적인 로그 시각화 구조 전환기]]></title>
            <link>https://medium.com/delivus/fluentbit-s3-athena-redash-%EC%8B%A4%EC%9A%A9%EC%A0%81%EC%9D%B8-%EB%A1%9C%EA%B7%B8-%EC%8B%9C%EA%B0%81%ED%99%94-%EA%B5%AC%EC%A1%B0-%EC%A0%84%ED%99%98%EA%B8%B0-184453202fa8?source=rss----4eecc410ebae---4</link>
            <guid isPermaLink="false">https://medium.com/p/184453202fa8</guid>
            <category><![CDATA[fluentbit]]></category>
            <category><![CDATA[cloudwatch]]></category>
            <category><![CDATA[aws]]></category>
            <dc:creator><![CDATA[Dongkyu Ko]]></dc:creator>
            <pubDate>Wed, 02 Jul 2025 03:20:48 GMT</pubDate>
            <atom:updated>2025-07-02T05:13:28.066Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>딜리버스는 최근 로그 시스템을 기존 CloudWatch 기반 구조에서, Fluentbit을 활용해 S3로 로그를 수집하고 Athena + Redash 기반으로 분석 및 시각화하는 구조로 전환했습니다. 이를 통해 로그 탐색성과 시각화 기능을 대폭 개선하고, 비용 부담 없이 로그 볼륨 증가에도 안정적으로 대응할 수 있는 확장성을 확보했습니다.</blockquote><h3>전환 배경</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/328/1*nrTOMTQkK-QPk2lruwXRCg.png" /></figure><p>기존에는 CloudWatch Logs를 사용해 로그를 조회하고 분석해왔습니다. 하지만 시간이 지나면서 다음과 같은 문제가 명확히 드러났습니다.</p><ul><li>로그 검색 속도가 느림</li><li>불편한 쿼리 UX, 제한된 필터링 기능</li><li>여러 로그 그룹을 통합 조회하는 데 불편함</li><li>시각화 도구 부재, 외부 연동이 어려움</li></ul><p>서비스가 성장하면서 로그 볼륨도 점점 증가했고, CloudWatch만으로는 실시간 이상 징후 탐지, 운영 대시보드 구성, 서비스 품질 모니터링이 제한적이었습니다. ELK 도입도 고려했지만, 운영 복잡성과 비용 이슈로 인해 보다 효율적인 대안을 찾게 되었습니다.</p><h3>전환 목표 및 구조 설계</h3><p>다음과 같은 요건을 충족하는 구조를 목표로 설계했습니다.</p><ul><li><strong>Fluentbit</strong>을 통해 각 ECS 서비스 <strong>로그를 수집</strong></li><li>로그를 <strong>S3에 카테고리별 저장</strong>하여 저비용 장기 보관</li><li><strong>Lambda + EventBridge</strong>로 주기적인 통합 및 전처리</li><li><strong>Athena 테이블</strong>을 통해 쿼리 가능한 구조 구성</li><li><strong>Redash</strong>를 통해 <strong>SQL 기반 시각화</strong></li></ul><p>이로써, CloudWatch의 단점을 극복하면서도 ELK 없이 로그 분석 환경을 구축할 수 있었습니다.</p><h3>Fluentbit: 로그 수집의 핵심</h3><p>서비스 로그가 처음 수집되는 단계에서는 Fluentbit이 중요한 역할을 합니다. 각 ECS Task에는 Fluentbit 사이드카 컨테이너가 함께 배포되며, 다음과 같은 방식으로 작동합니다.</p><ul><li><strong>표준 출력(STDOUT)/표준 에러(STDERR)</strong> 로그를 수집</li><li>로그 유형(tag 또는 container 이름)에 따라 <strong>S3 prefix</strong>를 다르게 지정</li><li><strong>5MB 단위 / 2분 간격</strong>으로 로그를 S3에 업로드하여 버퍼 관리</li></ul><pre>[INPUT]<br>    Name forward<br>    Listen 0.0.0.0<br>    Port 24224<br><br>[FILTER]<br>    Name grep<br>    Match *<br>    Regex log_level ERROR|WARN<br><br>[OUTPUT]<br>    Name s3<br>    Match *<br>    bucket your-log-bucket<br>    total_file_size 5M<br>    upload_timeout 2m<br>    s3_key_format /logs/$TAG/%Y/%m/%d/%H/%M/%S-$UUID.gz</pre><h4>핵심 구성 포인트</h4><ul><li><strong>Lightweight</strong>: Fluentd 대비 리소스 사용량이 적고 컨테이너 환경에 최적화</li><li><strong>다양한 필터링 및 포맷팅</strong>: 필요시 로그 레벨, 패턴 기준 필터링 가능</li><li><strong>정확한 라우팅</strong>: 서비스, 환경별로 S3 경로를 달리해 후처리 효율성 향상</li></ul><h3>Lambda 처리 로직: 통합 및 적재 단계</h3><p>5분 간격으로 실행되는 Lambda는 S3에 수집된 로그를 불러와 correlation_id를 기준으로 묶고, 로그 종류에 따라 event, item, request 데이터를 전처리하여 하나의 통합된 JSON 형태로 변환합니다. 이 과정을 통해 하나의 요청 단위에서 발생한 모든 로그 흐름을 한 번에 추적하고 분석할 수 있습니다.</p><p>correlation_id는 하나의 API 요청 또는 비즈니스 트랜잭션 내에서 발생한 로그들을 연관 지어 추적할 수 있도록 도와주는 식별자입니다.</p><h4>Lambda 입력 예시 ( event, item, request )</h4><pre>{<br>  &quot;@timestamp&quot;: &quot;2025-07-01T16:15:33.396555&quot;,<br>  &quot;event&quot;: {<br>    &quot;action&quot;: &quot;django.eventlogger&quot;,<br>    &quot;category&quot;: &quot;process&quot;,<br>    &quot;message&quot;: &quot;Unable to send notification...&quot;,<br>    &quot;labels&quot;: {<br>      &quot;application&quot;: &quot;daas&quot;,<br>      &quot;env&quot;: &quot;settings.beta&quot;<br>    },<br>    &quot;kind&quot;: &quot;event&quot;<br>  },<br>  &quot;httprequest&quot;: {<br>    &quot;correlation_id&quot;: &quot;024f99eae84c4eaca21c699067940000&quot;<br>  }<br>}ㅓ</pre><h4>Lambda 통합 로그 결과 예시</h4><pre>{<br>  &quot;request&quot;: [<br>    {<br>      &quot;step&quot;: &quot;RequestReceived&quot;,<br>      &quot;method&quot;: &quot;POST&quot;,<br>      &quot;uri&quot;: &quot;/api/orders/&quot;,<br>      &quot;message&quot;: {<br>          &quot;body&quot;: {<br>          &quot;user_id&quot;: 12345,<br>          &quot;items&quot;: [9876, 9877]<br>        }<br>      }<br>    }<br>  ],<br>  &quot;event&quot;: [<br>    {<br>      &quot;step&quot;: &quot;ValidateItems&quot;,<br>      &quot;message&quot;: &quot;Item 9876 is available&quot;<br>    },<br>    {<br>      &quot;step&quot;: &quot;CreateShippingItem&quot;,<br>      &quot;message&quot;: &quot;Shipping item 192837 created&quot;<br>    }<br>  ],<br>  &quot;item&quot;: [<br>    {<br>      &quot;event_name&quot;: &quot;ShippingItemCreated&quot;,<br>      &quot;item_id&quot;: &quot;192837&quot;,<br>      &quot;message&quot;: &quot;ICN1&quot;<br>    }<br>  ],<br>  &quot;correlation_id&quot;: &quot;e4e1d7b6c14a4d88bd8dcf9c45e10234&quot;,<br>  &quot;timestamp&quot;: &quot;2025-07-01T16:16:33.396555&quot;<br>}</pre><ul><li>message 내 한글 유니코드가 있을 수 있어 디코딩 필요</li><li>item의 필드는 event_name에 따라 다르게 구조화되므로 동적 파싱이 필요</li></ul><h3>아키텍처</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*arbWA3OAD4IfSGslddQ7Xw.jpeg" /></figure><ol><li><strong>ECS Fargate Task</strong>: 서비스 로그 발생</li><li><strong>Fluentbit 사이드카</strong>: 로그를 카테고리별 S3로 수집</li><li><strong>EventBridge Scheduler + Lambda</strong>: 5분마다 로그를 통합하고 날짜별 파티션 구조로 Athena 테이블에 반영</li><li><strong>Athena</strong>: 로그 테이블을 날짜 기준으로 파티셔닝</li><li><strong>Redash</strong>: Athena 연동 대시보드로 시각화</li></ol><h3>전환 후 효과</h3><h4>1. 로그 탐색 속도 및 유연성 향상</h4><ul><li>Athena를 통해 수십만 건 이상의 로그도 빠르게 쿼리 가능</li><li>다양한 필터링 및 집계 SQL을 자유롭게 작성 가능</li></ul><h4>2. 가시성 강화</h4><ul><li>서비스 별 에러 발생 비율, 시간대별 로그 수, 사용자 요청 패턴 등 다양한 지표를 Redash로 시각화</li></ul><h4>3. 비용 최적화</h4><ul><li>CloudWatch Logs 대비 저장 비용 절감</li><li>ELK 도입 없이도 충분한 기능 확보</li></ul><h4>4. 확장성 확보</h4><ul><li>로그 볼륨이 증가해도 S3 기반 저장 구조로 유연하게 대응</li><li>Redash 쿼리 성능도 Athena 덕분에 안정적으로 유지</li></ul><h3>사용 예시</h3><h4>Date Picker로 기간 선택해서 로그 조회</h4><p>Redash의 Date Picker에서 선택한 기간을 기반으로, Athena의 날짜 파티션(year, month, day)을 <strong>동적으로 생성</strong>하여 로그를 조회합니다.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/474/1*vdQNp47c38i9pn9KN27vSg.png" /><figcaption>Redash Date Picker</figcaption></figure><p>로그는 S3에 저장될 때 <strong>날짜별 파티션</strong>으로 구성되어 있기 때문에, <strong>파티션 필터링을 통해 불필요한 스캔을 줄이고 쿼리 성능을 높일 수 있습니다.</strong></p><p>아래는 로그를 조회하는 예시입니다.</p><pre>-- 날짜 범위 생성<br>WITH date_range AS (<br>  SELECT <br>    CAST(date_trunc(&#39;day&#39;, d) AS DATE) AS d,<br>    EXTRACT(year FROM d) AS year,<br>    EXTRACT(month FROM d) AS month,<br>    EXTRACT(day FROM d) AS day<br>  FROM UNNEST(SEQUENCE(<br>    TIMESTAMP &#39;{{ start_date }}&#39;,<br>    TIMESTAMP &#39;{{ end_date }}&#39;,<br>    INTERVAL &#39;1&#39; DAY<br>  )) AS t(d)<br>)<br><br>-- 파티션 키 기준으로 logs 테이블 조회<br>SELECT timestamp, service, log_level, message<br>FROM logs<br>JOIN date_range dr<br>  ON logs.year = dr.year<br> AND logs.month = dr.month<br> AND logs.day = dr.day<br>WHERE log_level = &#39;ERROR&#39;<br>  AND timestamp &gt;= now() - interval &#39;1 day&#39;<br>ORDER BY timestamp DESC;</pre><h4>로그 조회의 유연성</h4><p>로그 조회는 다양한 방식으로 아래와 같이 수행할 수 있습니다.</p><ul><li>event.message 또는 request.message의 키워드 기반 검색</li><li>item.id, event.id 등 개별 식별자 기반 조회</li><li>사용자 요청 URI, 상태 코드 등 조건을 조합한 상세 필터링</li></ul><p>이 외에도 SQL 기반 쿼리이기 때문에, 복잡한 조건 조합이나 집계/패턴 분석 등도 자유롭게 수행할 수 있습니다.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zvFXW2-LRmLRk4T-xrfRWg.png" /><figcaption>correlation_id 검색 기반의 대시보드</figcaption></figure><h3>결론 및 향후 계획</h3><p>로그 저장소를 바꾼 것이 아닌 운영 로그의 수집, 처리, 분석 전반을 더 효율적으로 구성한 구조 개선이었습니다.</p><p>과거에는 CloudWatch에서 수동 쿼리와 제한된 필터로만 분석했지만, 이제는 Redash에서 원하는 지표를 SQL로 자유롭게 분석하고 시각화할 수 있게 되었습니다.</p><h4>향후 계획</h4><ol><li><strong>Lambda 처리 방식 고도화</strong>: JSON → Parquet 변환 도입 및 비용 절감</li><li><strong>데이터 보존 전략 수립</strong>: S3 원본 로그에 대한 장기 보관 정책을 수립하고, 일정 기간 이후에는 압축하거나 Glacier로 이전하여 보관 비용을 줄일 예정</li></ol><p>CloudWatch 기반만으로는 로그 분석에 한계가 있었고, ELK 도입은 부담스러웠던 상황에서, 지금의 구조는 현실적인 절충안으로 잘 작동하고 있습니다.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=184453202fa8" width="1" height="1" alt=""><hr><p><a href="https://medium.com/delivus/fluentbit-s3-athena-redash-%EC%8B%A4%EC%9A%A9%EC%A0%81%EC%9D%B8-%EB%A1%9C%EA%B7%B8-%EC%8B%9C%EA%B0%81%ED%99%94-%EA%B5%AC%EC%A1%B0-%EC%A0%84%ED%99%98%EA%B8%B0-184453202fa8">Fluentbit + S3 + Athena + Redash: 실용적인 로그 시각화 구조 전환기</a> was originally published in <a href="https://medium.com/delivus">딜리버스</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Aurora Serverless v2 전환기 — API 성능 향상 및 무중단 스케일링]]></title>
            <link>https://medium.com/delivus/aurora-serverless-v2-%EC%A0%84%ED%99%98%EA%B8%B0-api-%EC%84%B1%EB%8A%A5-%ED%96%A5%EC%83%81-%EB%B0%8F-%EB%AC%B4%EC%A4%91%EB%8B%A8-%EC%8A%A4%EC%BC%80%EC%9D%BC%EB%A7%81-b364849ef1ef?source=rss----4eecc410ebae---4</link>
            <guid isPermaLink="false">https://medium.com/p/b364849ef1ef</guid>
            <category><![CDATA[aurora-serverless]]></category>
            <category><![CDATA[aws]]></category>
            <dc:creator><![CDATA[Dongkyu Ko]]></dc:creator>
            <pubDate>Tue, 29 Apr 2025 04:59:47 GMT</pubDate>
            <atom:updated>2025-04-29T04:19:37.795Z</atom:updated>
            <content:encoded><![CDATA[<h3>Aurora Serverless v2 전환기 — API 성능 향상 및 무중단 스케일업</h3><blockquote>딜리버스는 최근 데이터베이스를 Aurora On-Demand에서 Aurora Serverless v2로 전환했습니다. 이를 통해 API 성능을 높이고, 스케일업 장애를 제거했으며, 향후 물량 증가에 대비한 확장성을 확보했습니다.</blockquote><h3>전환 배경</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/894/1*tMnfwP6oPJzBaTkUl0mOwg.png" /></figure><p>화주사 증가와 함께 배송 지역 확장 및 물량이 급증하면서 기존 인프라의 한계가 드러났습니다.</p><p>Aurora On-Demand를 사용할 당시에는 스케일업이 필요할 때마다 블루/그린 전환을 사용했고 그 과정에서 <strong>장애가 빈번하게 발생</strong>했습니다. 또한, 24시간 365일 운영되는 서비스 특성상, 이러한 스케일업 작업은 평소에는 불가능해 <strong>명절 연휴 기간에만 진행해야 했고</strong>, 만약 긴급 작업이 필요한 경우에는 다수의 화주사에 <strong>사전 공지</strong>를 해야 하는 상황이었습니다. On-Demand 특성상 처리량 한계로 인해, 향후 지속적인 화주사 증가와 배송 물량 확대를 수용하는 데에도 어려움이 예상되었습니다. API 성능 개선을 위해 API 구조 자체의 최적화도 진행했지만, <strong>근본적인 해결</strong>을 위해서는 유연한 인프라 업그레이드가 필수적이었습니다.</p><p>API 성능 개선 관련 참고 글: <a href="https://medium.com/delivus/%EC%84%9C%EB%B2%84%EB%A6%AC%EC%8A%A4%EC%9D%98-%EB%A7%88%EB%B2%95-fastapi%EC%99%80-aws-lambda%EB%A1%9C-django-%EC%95%A0%ED%94%8C%EB%A6%AC%EC%BC%80%EC%9D%B4%EC%85%98%EC%97%90-%EB%82%A0%EA%B0%9C-%EB%8B%AC%EA%B8%B0-346c2e096f3b">서버리스의 마법: FastAPI와 AWS Lambda로 Django 애플리케이션에 날개 달기</a></p><h4>주요 문제 사항</h4><ul><li><strong>스케일업 장애:</strong> Aurora On-Demand 환경에서는 스케일업 시 블루/그린 전환을 해야 했고, 이 과정에서 서비스 장애가 발생했고 스케일업 작업은 평소에는 불가능해 명절 연휴 등 서비스 사용량이 적은 시기에만 진행할 수 있었으며, 긴급 스케일업이 필요한 경우에는 화주사에 사전 공지를 해야 하는 부담이 있었습니다.</li><li><strong>처리량 한계</strong>: On-Demand 환경에서는 분당 약 1200건 정도가 최대 처리량이었으며, 물량이 더 늘어날 경우 성능 저하나 타임아웃이 발생할 위험이 있었습니다.</li><li><strong>비용·운영 부담:</strong> 수직 확장을 통해 인프라 성능을 높이는 것은 비용 효율이 낮았고, 스케일업 실패에 따른 리스크도 지속적으로 존재</li></ul><h3>전환 과정</h3><p>On-Demand 인프라로는 장기적인 확장이 어렵다고 판단해, Aurora Serverless v2를 도입하기로 결정했습니다. 단순한 인스턴스 스펙 업그레이드가 아닌, 인프라 구조 자체를 유연하게 전환하는데 초점을 맞춘 선택이었습니다.</p><h4>주요 특징</h4><ul><li><strong>무중단 스케일링: </strong>필요에 따라 ACU(Aurora Capacity Unit)를 동적으로 조정할 수 있어, 스케일업 시 더 이상 블루/그린 전환이나 서비스 중단이 필요하지 않습니다.</li><li><strong>성능 향상:</strong> 기존 On-Demand(r5.xlarge) 대비, 약 2배 가까운 CPU 리소스 (ACU 16 기준)를 사용할 수 있어 처리량 한계를 넘을 수 있었습니다.. (기존에도 메모리 부하는 없었음)</li><li><strong>확장성 확보:</strong> 트래픽 급증에도 즉각 확장이 가능해져, 앞으로 더 많은 화주사와 배송 물량 증가에 유연하게 대응할 수 있는 기반을 마련했습니다.</li><li><strong>비용 구조 최적화 가능성:</strong> 트래픽에 따라 리소스를 탄력적으로 조정하기 때문에, 트래픽이 적은 시간대에는 비용을 자연스럽게 절감할 수 있는 구조를 갖췄습니다.</li></ul><h4>전환 작업 주요 프로세스</h4><p>전환은 단순한 스펙 변경이 아니라, 실제 운영 환경을 고려한 과정이었습니다. 다음과 같은 단계로 진행했습니다.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_sr6g22QDmi8-1U8KN7JeQ.png" /></figure><p><strong>1.사전 부하 테스트 — 실제 상황을 반영한 검증</strong></p><ul><li>Aurora Serverless v2로 전환하기 전, 기존 On-Demand 환경과 비교하는 사전 부하 테스트를 수행했습니다. 단순 성능 비교가 아니라, <strong>“실제 1분당 인입 주문량을 기반으로 TPS(Transactions Per Second산출”</strong> 을 기준으로 테스트를 설계해, 처리량, 오류율, 응답 속도 등 주요 지표를 구체적으로 측정했습니다. 이를 통해 전환 후 기대할 수 있는 효과를 사전에 수치로 예측할 수 있었습니다.</li></ul><p><strong>2.ACU 설정 최적화 — 효율성과 안정성의 균형</strong></p><ul><li>Serverless v2는 리소스를 유동적(ACU 0.5 ~ 256)으로 조정할 수 있지만, 초기 ACU 설정이 중요했습니다.</li><li>최소 8 ACU, 최대 16 ACU로 설정해, 과도한 리소스 소비 없이 안정성을 확보했습니다. 이 값은 기존 r5.xlarge 인스턴스(4코어, 32GiB 메모리) 기준을 참고해 최적화한 결과였습니다.</li></ul><p><strong>3.무중단 전환 — 다운타임 없이 자연스럽게</strong></p><ul><li>데이터 마이그레이션이나 연결 재설정 없이, Aurora 클러스터 옵션만 변경하는 방법으로 전환을 설계했습니다. 전환 과정에서는 블루/그린 배포 방식을 일부 사용했고, 초기에 약간의 장애가 있었지만, 서버리스 v2로의 최종 전환 과정은 문제 없이 완료되었습니다.</li></ul><p><strong>4.배포 및 모니터링 강화 — 안정성 확보</strong></p><ul><li>API 응답 속도, 오류율, RDS 주요 지표를 24시간 동안 집중 모니터링하며 이상 징후를 조기에 감지할 수 있도록 했습니다. 덕분에 초기 전환 이후에도 안정적으로 서비스가 운영되는 것을 확인할 수 있었습니다.</li></ul><h4>Before vs After</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hJRVn3yExkkoXprzUycafg.png" /><figcaption>전환 전,후 비교</figcaption></figure><h3>전환 후 실제 효과 및 성과</h3><p><strong>1.TPS 50% 향상</strong></p><ul><li>전환 전 약 1,200건 → 전환 후 약 1,800건으로 처리량 증가</li><li>실 서비스 부하 상황에서도 API 타임아웃이나 DB 병목 없이 안정적인 처리</li></ul><p><strong>2.스케일업 장애 제거</strong></p><ul><li>필요 시 자동으로 ACU가 확장되어 별도의 작업 없이 부하 대응 가능</li><li>과거처럼 명절 당일에만 스케일업하거나, 긴급 공지를 돌릴 필요 없음</li></ul><p><strong>3.서비스 안정성 향상</strong></p><ul><li>스케일업/다운 과정에서도 트랜잭션 에러나 DB 연결 오류 없이 정상 운영</li></ul><p><strong>4.확장성 확보</strong></p><ul><li>더 많은 화주사 계약 및 물량 증가가 예상되더라도, 별도 인프라 작업 없이 수용 가능한 구조 완성</li></ul><p><strong>5.비용 최적화</strong></p><ul><li>트래픽이 적은 시간대에는 최소 ACU로 유지되어 불필요한 비용을 줄일 수 있는 구조</li></ul><h3>결론 및 향후 계획</h3><p>Aurora Serverless v2 전환은 단순한 클러스터 옵션 변경이나 성능 향상을 넘어, <strong>서비스 확장성, 안정성, 그리고 운영 자유도</strong>를 획기적으로 끌어올린 중요한 전환이었습니다.</p><ul><li>이전에는, 스케일업을 위해 명절 연휴를 기다리거나, 장애 위험을 감수해야 했습니다.</li><li>이제는, 실시간 부하 감지와 자동 스케일링을 통해 서비스를 끊김 없이 운영할 수 있습니다.</li><li>또한, 화주사 증가와 물량 확대로 인한 예측 불가능한 부하에도 유연하게 대응할 수 있는 기반을 마련했습니다.</li></ul><p>이번 전환은 딜리버스가 미래의 물량 확장까지 안정적으로 대비할 수 있게 해준 터닝포인트였습니다.</p><p>더불어, 24시간 365일 운영되는 서비스의 안정성까지 확보하며, 딜리버스는 지금보다 훨씬 더 높은 수준의 고객 신뢰와 비즈니스 확장성을 갖추게 되었습니다.</p><h4>향후 계획</h4><p><strong>1.Serverless v2 기반 최적화</strong></p><ul><li>부하 패턴을 지속적으로 분석하여, ACU 최소/최대 값을 더욱 정밀하게 조정하고 최적의 비용·성능 밸런스를 맞춰나갈 예정</li></ul><p><strong>2.주문/배송 신청 API 로직 개선</strong></p><ul><li>사전 부하 테스트 과정에서 발견된 API 구조 개선 포인트를 반영해 TPS를 향상시키고, 보다 안정적인 요청 처리를 목표</li></ul><p><strong>3.End-to-End 부하 테스트 고도화</strong></p><ul><li>주문/배송뿐만 아니라, 간선, 반품 등 다양한 물류 워크플로우에 대해 부하 테스트를 확대 적용하여 시스템 전반의 대응력을 높일 예정</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b364849ef1ef" width="1" height="1" alt=""><hr><p><a href="https://medium.com/delivus/aurora-serverless-v2-%EC%A0%84%ED%99%98%EA%B8%B0-api-%EC%84%B1%EB%8A%A5-%ED%96%A5%EC%83%81-%EB%B0%8F-%EB%AC%B4%EC%A4%91%EB%8B%A8-%EC%8A%A4%EC%BC%80%EC%9D%BC%EB%A7%81-b364849ef1ef">Aurora Serverless v2 전환기 — API 성능 향상 및 무중단 스케일링</a> was originally published in <a href="https://medium.com/delivus">딜리버스</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[딜리버스의 MLOps 확장기 — 일일 트레이닝, Lambda 기반 추론]]></title>
            <link>https://medium.com/delivus/%EB%94%9C%EB%A6%AC%EB%B2%84%EC%8A%A4%EC%9D%98-mlops-%ED%99%95%EC%9E%A5%EA%B8%B0-%EC%9D%BC%EC%9D%BC-%ED%8A%B8%EB%A0%88%EC%9D%B4%EB%8B%9D-lambda-%EA%B8%B0%EB%B0%98-%EC%B6%94%EB%A1%A0-ed8cc67575c7?source=rss----4eecc410ebae---4</link>
            <guid isPermaLink="false">https://medium.com/p/ed8cc67575c7</guid>
            <category><![CDATA[mlops]]></category>
            <category><![CDATA[optimization]]></category>
            <category><![CDATA[aws]]></category>
            <dc:creator><![CDATA[Dongkyu Ko]]></dc:creator>
            <pubDate>Tue, 29 Apr 2025 04:59:24 GMT</pubDate>
            <atom:updated>2024-09-30T06:37:09.770Z</atom:updated>
            <content:encoded><![CDATA[<h3>딜리버스의 MLOps 확장기 — 일일 트레이닝, Lambda 기반 추론</h3><blockquote>딜리버스가 MLOps 도입 이후 실제 업무에 적용하기 위해 추가한 기능들에 대한 내용입니다.</blockquote><p>아래 글을 읽기전에 참고하면 좋은 글:</p><p><strong>1. </strong><a href="https://medium.com/delivus/%EB%94%9C%EB%A6%AC%EB%B2%84%EC%8A%A4%EC%9D%98-mlops-%EB%8F%84%EC%9E%85%EA%B8%B0-1%EC%9E%A5-%EB%B0%B0%EA%B2%BD%EA%B3%BC-%EB%AA%A9%ED%91%9C-103dfc1d7c9"><strong>딜리버스의 MLOps 도입기 — 1장 배경과 목표</strong></a></p><p><strong>2. </strong><a href="https://medium.com/delivus/%EB%94%9C%EB%A6%AC%EB%B2%84%EC%8A%A4%EC%9D%98-mlops-%EB%8F%84%EC%9E%85%EA%B8%B0-2%EC%9E%A5-ci-cd-workflow-%EB%B0%B0%ED%8F%AC-d1b2917932f8"><strong>딜리버스의 MLOps 도입기 — 2장 CI/CD, Workflow, 배포</strong></a></p><p><strong>3. </strong><a href="https://medium.com/delivus/%EB%A8%B8%EC%8B%A0%EB%9F%AC%EB%8B%9D-%EA%B8%B0%EB%B0%98%EC%9D%98-%EB%B0%B0%EC%86%A1-%EC%A7%80%EC%97%AD-%EB%B6%84%EB%A5%98-%EC%9E%90%EB%8F%99%ED%99%94-k-means%EB%A1%9C-%ED%81%B4%EB%9F%AC%EC%8A%A4%ED%84%B0%EB%A7%81-%EC%B5%9C%EC%A0%81%ED%99%94%ED%95%98%EA%B8%B0-283b9edfff74"><strong>머신러닝 기반의 배송 지역 분류 자동화: K-Means로 클러스터링 최적화하기</strong></a></p><h3>구조</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7N_mjY96S0ZiFqmdEHDmEA.jpeg" /><figcaption>딜리버스 ML(Machine Learning) 워크플로우</figcaption></figure><p>이전에 작성했던 MLOps 도입기 글과 비교해 많은 부분이 바뀌었습니다. 모델 학습 부분에서는 자체 모델을 개발했기 때문에, 하나의 작업에서 모델 트레이닝과 평가를 한 번에 끝낼 수 있도록 변경했습니다. 또한, 이전 모델 평가 결과와 비교하여 모델 성능이 개선되었을 때만 배포될 수 있도록 기능을 추가했습니다.</p><p>딜리버스의 추론 작업은 하루에 한 번 클러스터링이 생성될 때만 실행되기 때문에, 이를 Lambda 기반으로 변경했습니다. 마지막으로, 일일 트레이닝을 위해 이전에는 AWS MWAA(Managed Workflows for Apache Airflow)를 사용했지만, 일일 트레이닝도 하루에 한 번만 실행되므로 EventBridge Schedule을 사용해 Lambda를 실행하는 방식으로 변경했습니다.</p><p>이제부터 각 작업에 대해 설명하고, 이전과 어떻게 변경되었는지도 추가해서 설명하겠습니다.</p><p><em>모든 리소스는 AWS CDK(</em><a href="https://aws.amazon.com/ko/cdk/"><em>Cloud Development Kit</em></a><em>)를 사용해서 구축했습니다.</em></p><h3>ML(Machine Learning) Training Job</h3><blockquote><a href="https://medium.com/delivus/%EB%94%9C%EB%A6%AC%EB%B2%84%EC%8A%A4%EC%9D%98-mlops-%EB%8F%84%EC%9E%85%EA%B8%B0-2%EC%9E%A5-ci-cd-workflow-%EB%B0%B0%ED%8F%AC-d1b2917932f8">딜리버스의 MLOps 도입기 — 2장 CI/CD, Workflow, 배포</a> 에서는 ML Workflow로 정의했습니다.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*G5qopwK50HQynzKndNlYew.png" /></figure><ol><li><strong>Feature Store</strong>: 모델 학습과 추론에 사용되는 피처들을 저장하고 관리하는 곳입니다. Sagemaker에 제공하는 기능을 사용하고 있습니다.</li><li><strong>Data Preprocessing</strong>: 특성을 추출하고 데이터를 분할하는 작업을 진행합니다. 이상치 제거 및 정규화 등의 작업은 <strong>Update Feature Store Job</strong>에서 진행합니다.</li><li><strong>Model Training:</strong> 딥러닝 기반의 학습을 진행하며, 학습과 동시에 평가를 진행합니다. 평가 지표로는 MAE를 사용하고 있습니다. 딜리버스가 자체적으로 최적화한 <a href="https://github.com/somepago/saint">SAINT</a>(정형 데이터 분석을 위한 딥러닝)기반의 모델입니다.</li><li><strong>Previous Model Comparison:</strong> 이전 모델의 평가 지표와 비교해서 개선되었는지 확인합니다. 평가 지표가 개선이 되었다면 모델을 등록하고 개선이 되지 않았다면 모델을 등록하지 않습니다. 또한, 모델이 개선되지 않았다면 상세 내용을 CloudWatch에서 확인합니다.</li><li><strong>Model Registry:</strong> 모델의 S3 Path 정보를 받아 모델을 등록합니다. 지표가 개선이 되지 않아 추가적으로 모델을 등록하지 않을 때는 실행하지 않습니다.</li></ol><p>MLOps도입 초반보다 Step Functions의 작업이 간소화(학습, 평가, 평가결과 → 학습)되었으며, 이전 모델의 지표와 비교하는 기능을 추가했습니다. 평균 작업 시간은 2시간정도 소요되며, 상세 내용은 CloudWatch에서 확인할 수 있습니다.</p><h3>Create Clustering Job</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BL2E59p07iA3f0lA070tgQ.png" /></figure><ol><li><strong>EventBridge(Rule):</strong> Daas 서버에서 특정 EventBridge 버스로 이벤트를 보내 클러스터링 생성 작업을 시작하기 때문에, 이벤트를 보낼 때 필요한 구성을 정의합니다.</li><li><strong>Fetch &amp; Preprocessing Shipping Data:</strong> Daas RDS에서 당일 배송 데이터를 가져온 후, 데이터 품질 검사를 진행하고, 추론에 사용할 수 있도록 데이터를 전처리합니다.</li><li><strong>Inference:</strong> 전처리가 된 데이터를 사용해 추론을 진행하며, 최종 결과물은 벡터 기반 데이터셋 입니다. 실행 시간은 배송 물량에 따라 달라지지만, 모델의 크기가 작아 1만 건의 데이터 기준으로 20초 이내에 처리됩니다. 추론 작업은 클러스터 생성과 마찬가지로 하루에 한 번만 실행되므로, 실시간 엔드포인트가 필요하지 않아 Lambda를 사용하여 구현했습니다.</li><li><strong>Vector-Based Dataset:</strong> 추론 후 생성된 결과물이며, 지정된 S3 경로에 저장됩니다. 이 데이터셋은 클러스터링의 주요 피처로 사용됩니다.</li><li><strong>Create Clustering:</strong> <a href="https://medium.com/delivus/%EB%A8%B8%EC%8B%A0%EB%9F%AC%EB%8B%9D-%EA%B8%B0%EB%B0%98%EC%9D%98-%EB%B0%B0%EC%86%A1-%EC%A7%80%EC%97%AD-%EB%B6%84%EB%A5%98-%EC%9E%90%EB%8F%99%ED%99%94-k-means%EB%A1%9C-%ED%81%B4%EB%9F%AC%EC%8A%A4%ED%84%B0%EB%A7%81-%EC%B5%9C%EC%A0%81%ED%99%94%ED%95%98%EA%B8%B0-283b9edfff74">머신러닝 기반의 배송 지역 분류 자동화: K-Means로 클러스터링 최적화하기</a>에 나와있는 내용처럼 버니가 배송하는 지역을 생성하기 위한 작업입니다.</li></ol><p>MLOps 도입 초기에는 추론 작업을 위해 SageMaker Processing Job을 사용했지만, 최소 5분 이상 소요되는 문제가 있었습니다. 그 이유는 작업 시작 시 인스턴스를 생성하고 설정하는 데 전체 시간의 90% 이상을 소비했기 때문입니다.</p><p>해당 문제를 해결하기 위해 SageMaker Serverless Endpoint를 고려했지만, 추론 작업이 하루에 한 번만 실행되고 모델 크기도 작아 오버스펙이라고 판단했습니다. 고민을 해본 결과 Lambda를 사용하기로 결정했습니다. 이를 통해 CDK를 활용한 배포가 간편해졌으며, 비용 측면에서도 효율을 높일 수 있었습니다.</p><h3>Update Feature Store Job</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AQlH0GLLoG41eoitLDrRIQ.png" /></figure><ol><li><strong>EventBridge(Schedule):</strong> 해당 작업을 매일 아침 9시에 시작할 수 있도록 필요한 구성을 정의합니다.</li><li><strong>Fetch &amp; Preprocessing Shipping Data:</strong> Daas RDS에서 전일 배송 데이터를 가져온 후, 이상치 데이터를 제거하고 피처 스토어에 업데이트할 수 있도록 전처리합니다.</li><li><strong>Insert Feature Store:</strong> 전처리된 데이터셋을 피처 스토어에 업데이트 할 수 있도록 인증 및 배치 작업 등의 구성을 정의합니다.</li><li><strong>Feature Store</strong>: 모델 학습과 추론에 사용되는 피처들을 저장하고 관리하는 곳입니다. Sagemaker에 제공하는 기능을 사용하고 있습니다.</li><li><strong>Initiate ML Training Job:</strong> 새로 업데이트된 피처들로 재학습을 진행할 수 있도록, ML Training Job의 Step Functions를 호출합니다.</li></ol><p>MLOps 도입 초기에는 해당 파이프라인을 AWS MWAA를 사용했습니다. 하지만 파이프라인이 하나고 복잡하지 않아, 여러 개의 Lambda 함수를 사용해 구성하는 방법으로 변경했습니다. 이를 통해 최소 사양의 AWS MWAA를 사용할 때 발생하는 월 최소 $200 이상의 비용을 절감할 수 있었으며, CDK를 활용한 배포도 더욱 간편해졌습니다.</p><h3>결론</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/502/1*K3kWT4p1roEz426egVzKlg.png" /><figcaption>최종적으로 생성된 배송 지역</figcaption></figure><p>MLOps 도입 초기에는 <strong>‘Automated Machine Learning on AWS’</strong> 책의 내용을 기반으로 구축하는 데 많은 시간을 투자했습니다. 그러나 실제로 구축하고 운영한 이후에는 <strong>실제 업무에 적용하기 위한 최적화</strong>에 더 많은 시간을 쏟아야 했습니다. 오히려 도입 초기보다 더 많은 노력과 시간이 필요했던 것 같습니다. 이 과정에서 주로 <strong>소요 시간 단축</strong>, <strong>업무 자동화</strong>, 그리고 <strong>비용 절감</strong>에 초점을 맞추었습니다.</p><p>앞으로도 MLOps를 운영하면서 필요한 부분이 생기면 지속적으로 최적화를 진행할 예정입니다. 또한, <strong>SageMaker를 로컬 환경에서 사용할 수 있도록 하여</strong> 데이터 사이언티스트들의 생산성을 높이는 방안도 도입할 계획입니다.</p><p>딜리버스는 앞으로 다양한 <strong>ML을 활용해 업무 자동화</strong>를 꾸준히 발전시켜 나갈 것입니다. 긴 글을 읽어주셔서 감사합니다.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ed8cc67575c7" width="1" height="1" alt=""><hr><p><a href="https://medium.com/delivus/%EB%94%9C%EB%A6%AC%EB%B2%84%EC%8A%A4%EC%9D%98-mlops-%ED%99%95%EC%9E%A5%EA%B8%B0-%EC%9D%BC%EC%9D%BC-%ED%8A%B8%EB%A0%88%EC%9D%B4%EB%8B%9D-lambda-%EA%B8%B0%EB%B0%98-%EC%B6%94%EB%A1%A0-ed8cc67575c7">딜리버스의 MLOps 확장기 — 일일 트레이닝, Lambda 기반 추론</a> was originally published in <a href="https://medium.com/delivus">딜리버스</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Part2: Implement authentication in next js v15 using next-auth v5, prisma, postgres db]]></title>
            <link>https://medium.com/delivus/part2-wip-implement-authentication-in-next-js-v15-using-next-auth-v5-prisma-postgres-db-78a950e63dda?source=rss----4eecc410ebae---4</link>
            <guid isPermaLink="false">https://medium.com/p/78a950e63dda</guid>
            <category><![CDATA[nextauth]]></category>
            <category><![CDATA[nextjs]]></category>
            <dc:creator><![CDATA[Chimgee G]]></dc:creator>
            <pubDate>Fri, 04 Apr 2025 06:56:23 GMT</pubDate>
            <atom:updated>2025-04-04T06:56:39.600Z</atom:updated>
            <content:encoded><![CDATA[<h3>Part 2: Setup NextAuth and Implement Sign-in</h3><p>This post covers <strong>Part 2</strong> of the implementation, focusing on setting up Next auth and implementing sign-in logic in a Next.js 15 project. You can see part 1 from <a href="https://medium.com/delivus/how-to-implement-authentication-in-nextjs-15-using-next-auth-prisma-postgres-db-part-1-bc1b90d60d51">here</a>.</p><h3>Topics Covered in This Section:</h3><ol><li>Creating NextAuth Configuration</li><li>Protecting Routes</li><li>Implementing Credential provider</li><li>Implementing Login Form</li></ol><h3>1. Creating NextAuth configuration</h3><p>Step 1: Install next-auth dependency:</p><pre>yarn add next-auth@beta</pre><p>next-auth 5 version is in pre production launch version.</p><p>Step 2: Create an auth.config.ts file in the root directory of the project, which will contain NextAuth configuration options, and export it.</p><pre>import type { NextAuthConfig } from &#39;next-auth&#39;;<br> <br>// Creating the configuration object for NextAuth<br>export const authConfig = {<br>  // Defining signup pages to tailor the authentication experience. Here, we redirect the default sign-in page to &#39;/login&#39;.<br>  pages: {<br>    signIn: &#39;/auth/signin&#39;,<br>  },<br>  providers: []<br>} satisfies NextAuthConfig;</pre><p>Step 3: Generating secret key</p><p>This key is used to encrypt cookies, ensuring the security of user sessions. You can do this by running the following command in your terminal:</p><pre>openssl rand -base64 32</pre><p>Then, in your .env file we created in the Part 1, add your generated key to the AUTH_SECRET variable:</p><pre>AUTH_SECRET=your-secret-key</pre><h3>2. Protecting Routes</h3><p>Now, it is time to protect private routes from public access.</p><p>Step 1: To do that, implement the <strong>authorized callback</strong> in authConfig.</p><p>Let’s assume your public page starts with a “auth” and others are private pages.</p><pre>import type {NextAuthConfig} from &#39;next-auth&#39;;<br><br>export const AUTH_PATH = &quot;/auth&quot;;<br>// Creating the configuration object for NextAuth<br>export const authConfig = {<br>    trustHost: true,<br>    // Defining signup pages to tailor the authentication experience. Here, we redirect the default sign-in page to &#39;/login&#39;.<br>    pages: {<br>        signIn: AUTH_PATH + &#39;/signin&#39;,<br>    },<br>    // Configuring callbacks for handling authorization logic during authentication flow.<br>    callbacks: {<br>        authorized({auth, request: {nextUrl}}) {<br>            // Checking if the user is logged in<br>            const isLoggedIn = !!auth?.user;<br>            // Determining if the user is currently on the dashboard<br>            const isOnAuth = nextUrl.pathname.startsWith(AUTH_PATH);<br>            // Handling authorization logic based on user status and location<br>            if (!isOnAuth) {<br>                // Redirecting unauthenticated users to the login page when attempting to access dashboard-related pages<br>                return isLoggedIn;<br>            } else if (isLoggedIn) {<br>                // Redirecting authenticated users to the dashboard if they attempt to access authentication-related pages like login/signup<br>                return Response.redirect(new URL(&#39;/&#39;, nextUrl));<br>            }<br>            // Allowing access for other scenarios<br>            return true;<br>        },<br>    },<br><br>    // Placeholder array for authentication providers. We initialize it as empty for now, adding providers when required.<br>    providers: [], // We start with an empty array, adding providers as needed<br><br>} satisfies NextAuthConfig;</pre><ul><li>The <strong>authorized callback</strong> is used to verify if the request is authorized to access certain pages</li><li>The providers is list of login options such as username and password, google, facebook login etc. Currently, we provide empty list here.</li></ul><p>Step 2: The <strong>authorized callback</strong> protects private pages via <a href="https://nextjs.org/docs/app/building-your-application/routing/middleware">Next.js Middleware</a>. To do that, add an authConfig into middleware.ts file in root directory.</p><pre>import NextAuth from &#39;next-auth&#39;;<br>import { authConfig } from &#39;./auth.config&#39;;</pre><pre>export default NextAuth(authConfig).auth;</pre><pre>export const config = {<br>    // &lt;https://nextjs.org/docs/app/building-your-application/routing/middleware#matcher&gt;<br>    matcher: [&#39;/auth&#39;, &#39;/((?!api|_next/static|_next/image|.*\\\\.svg).*)&#39;],<br>};</pre><p>Middleware is called before a request is completed and protects private routes before it is even rendered.</p><h3>3. Adding the Credentials provider</h3><p>The Credentials provider allows users to log in with a username and a password.</p><p>Step 1: Install dependency:</p><pre>yarn add zod</pre><p>You can use zod to validate values that user entered before checking if the user exists in the database:</p><p>Step 2: Create auth.ts file in root directory:</p><p>This file will import the next auth configuration and initialize NextAuth. Then add credential provider in providers list and implement authorize logic.</p><pre>import NextAuth from &#39;next-auth&#39;;<br>import { authConfig } from &#39;./auth.config&#39;;<br>import {prisma} from &quot;ospring/app/lib/prisma&quot;;<br>import bcrypt from &#39;bcryptjs&#39;;<br>import Credentials from &#39;next-auth/providers/credentials&#39;;<br>import {findUserByPhone} from &quot;ospring/app/db/user&quot;;<br> <br>export const { auth, signIn, signOut } = NextAuth({<br>  ...authConfig,<br>  providers: [Credentials({<br>            async authorize(credentials) {<br>                // Parsing and validating incoming credentials using Zod<br>                const parsedCredentials = signinSchema<br>                    .safeParse(credentials);<br>                if (parsedCredentials.success) {<br>                    const {phone, password} = parsedCredentials.data;<br>                const user = await findUserByPhone(phone); //fetch user info from database<br>                    console.log(&#39;authorize user:&#39;, user);<br>                    // If user exists, compare hashed passwords<br>                    if (!user) return null;<br>                    console.log(&#39;authorize user:&#39;, user);<br>                    const passwordsMatch = await bcrypt.compare(password, user.password);<br>                    // If passwords match, return the user<br>                    if (passwordsMatch) return user;<br>                // If credentials are invalid, log and return null<br>                console.log(&#39;Invalid credentials&#39;);<br>                return null;<br>            },<br>        })],<br>});</pre><p>Step 3: Create the route.ts file within api/auth/[…nextauth] according to the documentation. route.ts file should export GET, POST method.</p><pre>export {GET, POST} from &#39;@/auth&#39;;</pre><h3>4. Implementing Login Form</h3><p>Step 1: Create authenticate action:</p><p>Generate auth.ts file in app/actions directory.</p><pre>&#39;use server&#39;<br>import {signIn} from &quot;ospring/auth&quot;;<br>import {AuthError} from &quot;next-auth&quot;;<br>interface SigninFormState {<br>    success?: string;<br>    errors?: {<br>        phone?: string[];<br>        password?: string[];<br>        _form?: string[];<br>    };<br>}<br>// Authenticating function for sign-in<br>export async function authenticate(prevState: SigninFormState | undefined, formData: FormData) {<br>    console.log(&quot;authenticate&quot;, formData);<br>    try {<br>        await signIn(&#39;credentials&#39;, formData);<br>    } catch (error) {<br>        console.log(&quot;authenticate error: &quot;, error);<br>        // Handling authentication errors<br>        if (error instanceof AuthError) {<br>            switch (error.type) {<br>                case &#39;CredentialsSignin&#39;:<br>                    return {<br>                        errors: {<br>                            password: [&#39;Invalid credentials.&#39;],<br>                        }<br>                    };<br>                default:<br>                    return {<br>                        errors: {<br>                            password: [&#39;Something went wrong.&#39;],<br>                        }<br>                    };<br>            }<br>        }<br>        throw error;<br>    }<br>};</pre><h3>4. Creating the login route</h3><p>Step 1: Create page.tsx in /app/(pages)/auth/signin directory and paste the following code:</p><p>Its content will be like the following.</p><pre>&quot;use client&quot;<br>import {authenticate} from &quot;ospring/app/actions/auth&quot;;<br>import {useActionState} from &quot;react&quot;;<br>import {Button, Input} from &quot;ospring/app/components/ui&quot;;<br><br>export default function SigninForm() {<br>    console.log(&quot;Login page&quot;);<br>    const [errorMessage, action] = useActionState(authenticate, undefined);<br><br>    return (<br>        &lt;form action={action} id={&quot;login&quot;} className={&quot;h-full flex flex-col pt-10 items-center&quot;}&gt;<br>            &lt;div className={&quot;text-foreground m-10 text-xl&quot;}&gt;Login&lt;/div&gt;<br>            &lt;Input className={&quot;mb-4&quot;} maxLength={8} inputSize={&quot;medium&quot;} name={&quot;phone&quot;} placeholder={&quot;0000 0000&quot;}<br>                   label={&quot;Phone number&quot;}/&gt;<br>            &lt;Input maxLength={36} inputSize={&quot;medium&quot;} name={&quot;password&quot;} type=&quot;password&quot; placeholder={&quot;******&quot;}<br>                   label={&quot;Password&quot;}<br>                   errorMessage={errorMessage?.errors?.password ? errorMessage?.errors?.password[0] : &quot;&quot;}/&gt;<br>            &lt;Button className={&quot;bg-foreground text-background h-10 w-full mt-auto&quot;} type={&quot;submit&quot;}&gt;Login&lt;/Button&gt;<br>        &lt;/form&gt;<br>    )<br>}  </pre><p>You will notice the page imports Button and Input components in this blog, but we skip explaining content of those file. You can check their content from github repository.</p><h3>Try the project</h3><p>Finally, run the project:</p><pre>yarn dev</pre><p>You will see login page rendered in the browser.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/966/1*85OHZI5CaV8YsWsm6t0s1g.png" /></figure><h3>Final Project Structure</h3><p>Your final project structure should look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/742/1*Nq6VaoXAGExeZ0UvWY1zeA.png" /></figure><p>You can find the source code for this example <a href="https://github.com/delivus-daas/next-sample-app/tree/PART2_nextauth">here</a>.</p><h3>References:</h3><p><a href="https://nextjs.org/learn/dashboard-app/adding-authentication">https://nextjs.org/learn/dashboard-app/adding-authentication</a></p><p><a href="https://medium.com/@pawanrijal/building-authentication-in-a-next-js-14-app-using-nextauth-and-prisma-59c9d67a0eca">https://medium.com/@pawanrijal/building-authentication-in-a-next-js-14-app-using-nextauth-and-prisma-59c9d67a0eca</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=78a950e63dda" width="1" height="1" alt=""><hr><p><a href="https://medium.com/delivus/part2-wip-implement-authentication-in-next-js-v15-using-next-auth-v5-prisma-postgres-db-78a950e63dda">Part2: Implement authentication in next js v15 using next-auth v5, prisma, postgres db</a> was originally published in <a href="https://medium.com/delivus">딜리버스</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Finding and Fixing Connection Latency Bottlenecks in Web Services]]></title>
            <link>https://medium.com/delivus/finding-and-fixing-connection-latency-bottlenecks-in-web-services-072aef856cb2?source=rss----4eecc410ebae---4</link>
            <guid isPermaLink="false">https://medium.com/p/072aef856cb2</guid>
            <category><![CDATA[bottleneck]]></category>
            <category><![CDATA[web]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[network]]></category>
            <category><![CDATA[optimization]]></category>
            <dc:creator><![CDATA[Hyunil Kim]]></dc:creator>
            <pubDate>Fri, 17 Jan 2025 02:13:21 GMT</pubDate>
            <atom:updated>2025-01-17T02:13:20.977Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gAWkSLv82gCB45G3R5Upbw.jpeg" /></figure><p>Connection latency can make or break your web service’s user experience. Whether you’re running a high-traffic e-commerce platform or managing internal microservices, understanding and optimizing connection latency is crucial. In this comprehensive guide, we’ll explore methodologies to identify and resolve latency bottlenecks across your web service infrastructure.</p><h3>Understanding Connection Latency</h3><p>Connection latency represents the total time delay between making a request and receiving a response. This encompasses several components:</p><p><strong>Network Travel Time</strong></p><ul><li>Physical distance and network path</li><li>Internet service provider routing</li><li>Content delivery network (CDN) performance</li></ul><p><strong>Server Processing Time</strong></p><ul><li>Application logic execution</li><li>Database queries</li><li>Cache hits/misses</li><li>Resource availability</li></ul><p><strong>Intermediate System Delays</strong></p><ul><li>Load balancer processing</li><li>Proxy server overhead</li><li>DNS resolution time</li><li>SSL/TLS handshake duration</li></ul><h3>Diagnostic Methodologies</h3><h4>1. Systematic Performance Monitoring</h4><p>Implement a multi-layered monitoring approach:</p><p><strong>Infrastructure Level</strong></p><ul><li>CPU utilization</li><li>Memory usage</li><li>Network throughput</li><li>Disk I/O performance</li></ul><p><strong>Application Level</strong></p><ul><li>Request/response times</li><li>Database query performance</li><li>Cache hit rates</li><li>Error rates and types</li></ul><p><strong>User Experience Level</strong></p><ul><li>Page load times</li><li>Time to first byte (TTFB)</li><li>Resource loading sequence</li><li>Client-side rendering time</li></ul><h4>2. Data Collection Strategies</h4><p>Implement comprehensive data collection across your service:</p><p><strong>Logging Practices</strong></p><ul><li>Structured log formats</li><li>Correlation IDs for request tracking</li><li>Timestamp precision</li><li>Context-rich error information</li></ul><p><strong>Metrics Collection</strong></p><ul><li>Request duration histograms</li><li>Error rate tracking</li><li>Resource utilization trends</li><li>Service dependencies performance</li></ul><p><strong>Distributed Tracing</strong></p><ul><li>End-to-end request flow</li><li>Service dependency mapping</li><li>Bottleneck identification</li><li>Performance anomaly detection</li></ul><h3>Common Bottleneck Sources</h3><h4>1. Database Connection Management</h4><p>Key focus areas:</p><p><strong>Connection Pool Configuration</strong></p><ul><li>Pool size optimization</li><li>Connection lifetime management</li><li>Queue length monitoring</li><li>Dead connection handling</li></ul><p><strong>Query Performance</strong></p><ul><li>Execution plan analysis</li><li>Index optimization</li><li>Query caching strategies</li><li>Connection pooling efficiency</li></ul><p><strong>Database Resource Utilization</strong></p><ul><li>Connection count monitoring</li><li>Transaction duration tracking</li><li>Lock contention analysis</li><li>Resource saturation detection</li></ul><h4>2. Network Infrastructure</h4><p>Critical considerations:</p><p><strong>Network Architecture</strong></p><ul><li>Topology optimization</li><li>Route efficiency</li><li>Bandwidth allocation</li><li>Load distribution</li></ul><p><strong>Protocol Optimization</strong></p><ul><li>TCP/IP configuration</li><li>HTTP/2 implementation</li><li>WebSocket efficiency</li><li>Keep-alive settings</li></ul><p><strong>Infrastructure Monitoring</strong></p><ul><li>Network latency tracking</li><li>Packet loss detection</li><li>Bandwidth utilization</li><li>Connection establishment time</li></ul><h4>3. Microservice Communication</h4><p>Essential aspects:</p><p><strong>Service Discovery</strong></p><ul><li>Registry maintenance</li><li>Health check mechanisms</li><li>Cache invalidation strategies</li><li>Failover procedures</li></ul><p><strong>Communication Patterns</strong></p><ul><li>Synchronous vs asynchronous</li><li>Batch processing options</li><li>Message queue implementation</li><li>Event-driven architecture</li></ul><p><strong>Resilience Patterns</strong></p><ul><li>Circuit breaker implementation</li><li>Retry strategies</li><li>Timeout configurations</li><li>Fallback mechanisms</li></ul><h3>Optimization Strategies</h3><h4>1. Caching Implementation</h4><p>Develop a comprehensive caching strategy:</p><p><strong>Cache Layers</strong></p><ul><li>Browser caching</li><li>CDN caching</li><li>Application caching</li><li>Database caching</li></ul><p><strong>Cache Management</strong></p><ul><li>Invalidation strategies</li><li>Update patterns</li><li>Storage optimization</li><li>Hit rate monitoring</li></ul><h4>2. Connection Pooling</h4><p>Optimize connection management:</p><p><strong>Pool Configuration</strong></p><ul><li>Size optimization</li><li>Timeout settings</li><li>Idle connection management</li><li>Maximum lifetime settings</li></ul><p><strong>Monitoring and Maintenance</strong></p><ul><li>Usage patterns analysis</li><li>Performance metrics tracking</li><li>Resource utilization</li><li>Leak detection</li></ul><h4>3. Load Management</h4><p>Implement effective load handling:</p><p><strong>Load Balancing</strong></p><ul><li>Algorithm selection</li><li>Health check configuration</li><li>Session persistence</li><li>Failover planning</li></ul><p><strong>Rate Limiting</strong></p><ul><li>Threshold determination</li><li>Implementation strategy</li><li>Error handling</li><li>Client notification</li></ul><h3>Monitoring and Alerting Best Practices</h3><h4>1. Metric Selection</h4><p>Choose relevant metrics:</p><p><strong>System Metrics</strong></p><ul><li>Resource utilization</li><li>Network performance</li><li>Error rates</li><li>Response times</li></ul><p><strong>Business Metrics</strong></p><ul><li>Transaction success rates</li><li>User experience indicators</li><li>Service level objectives</li><li>Cost efficiency measures</li></ul><h4>2. Alert Configuration</h4><p>Develop effective alerting:</p><p><strong>Threshold Setting</strong></p><ul><li>Baseline determination</li><li>Anomaly detection</li><li>Trend analysis</li><li>Seasonal adjustment</li></ul><p><strong>Alert Management</strong></p><ul><li>Priority levels</li><li>Escalation paths</li><li>Response procedures</li><li>Resolution tracking</li></ul><h3>Long-term Maintenance</h3><h4>1. Capacity Planning</h4><p>Implement forward-looking capacity management:</p><p><strong>Growth Projection</strong></p><ul><li>Traffic patterns analysis</li><li>Resource requirement forecasting</li><li>Scaling threshold determination</li><li>Cost optimization</li></ul><p><strong>Infrastructure Evolution</strong></p><ul><li>Technology stack updates</li><li>Architecture improvements</li><li>Performance optimization</li><li>Security enhancement</li></ul><h3>2. Performance Testing</h3><p>Maintain regular testing procedures:</p><p><strong>Load Testing</strong></p><ul><li>Stress test scenarios</li><li>Performance benchmarks</li><li>Scalability verification</li><li>Failure mode analysis</li></ul><p><strong>Continuous Monitoring</strong></p><ul><li>Trend analysis</li><li>Regression detection</li><li>Performance budgeting</li><li>Impact assessment</li></ul><h3>Conclusion</h3><p>Successfully managing connection latency requires a comprehensive approach combining proper monitoring, systematic analysis, and continuous optimization. By following these methodologies and regularly reviewing your service’s performance, you can maintain optimal latency characteristics and provide an excellent user experience.</p><p>Remember that latency optimization is an ongoing process. Regular monitoring, proactive maintenance, and continuous improvement are key to maintaining optimal service performance over time.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=072aef856cb2" width="1" height="1" alt=""><hr><p><a href="https://medium.com/delivus/finding-and-fixing-connection-latency-bottlenecks-in-web-services-072aef856cb2">Finding and Fixing Connection Latency Bottlenecks in Web Services</a> was originally published in <a href="https://medium.com/delivus">딜리버스</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Breaking Down the Monolith: Expectations vs Reality]]></title>
            <link>https://medium.com/delivus/breaking-down-the-monolith-expectations-vs-reality-49b42a02b9df?source=rss----4eecc410ebae---4</link>
            <guid isPermaLink="false">https://medium.com/p/49b42a02b9df</guid>
            <category><![CDATA[system-architecture]]></category>
            <category><![CDATA[monolith]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[software-architecture]]></category>
            <dc:creator><![CDATA[Hyunil Kim]]></dc:creator>
            <pubDate>Fri, 17 Jan 2025 02:13:09 GMT</pubDate>
            <atom:updated>2025-01-17T02:13:09.245Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ELwe6FMh6emxUSYVWZSUJw.png" /></figure><h3>The Promise</h3><p>Engineering teams often envision a clean transition where they’ll break their monolith into neat, independent services that communicate seamlessly. The anticipated benefits usually include:</p><p>Independent deployments, allowing teams to ship features faster Better scalability for different components Clearer ownership boundaries Enhanced reliability through isolation</p><h3>The Reality Check</h3><h4>Distributed Complexity Isn’t Free</h4><p>Teams often underestimate how much complexity they’re trading for. In a monolithic system, method calls are straightforward. In a distributed system, every service interaction becomes a potential point of failure. Network latency, timeout handling, and retry mechanisms suddenly become everyday concerns.</p><h4>The Database Decomposition Trap</h4><p>Many teams start by splitting their application layer while keeping a shared database. This approach feels safer but creates a worse situation than the original monolith. You end up with distributed services tightly coupled through shared tables and foreign keys.</p><p>Instead, each service should own its data exclusively. This means accepting eventual consistency where appropriate and redesigning transactions that previously spanned multiple domains.</p><h3>What Not To Do</h3><p>Don’t decompose based on technical layers. Breaking your monolith into “frontend-service,” “business-logic-service,” and “data-access-service” just creates a distributed monolith with extra network calls.</p><p>Don’t attempt a “big bang” rewrite. Companies that succeed typically migrate gradually, extracting one bounded context at a time while maintaining the monolith.</p><p>Don’t create microservices for future scalability you don’t need yet. Each service adds operational overhead. Amazon didn’t start with microservices — they evolved to them as their actual scaling needs demanded it.</p><h3>Unexpected Challenges</h3><p>Team structure often doesn’t align with service boundaries. Conway’s Law works both ways — your architecture will reflect your organization, so you may need to reorganize teams first.</p><p>Monitoring and debugging become significantly more complex. A single user request now creates a web of service interactions. Without proper distributed tracing from day one, troubleshooting becomes a nightmare.</p><h3>Leveraging Domain-Driven Design</h3><p>DDD provides essential tools for identifying service boundaries through its concept of bounded contexts and ubiquitous language. When decomposing a monolith:</p><p>Use bounded contexts to identify natural service boundaries based on business capabilities rather than technical concerns. For example, in an e-commerce system, “Shipping” and “Inventory” might use the term “Location” differently — suggesting a natural service split.</p><p>Let domain events guide your integration patterns. Instead of tight coupling through direct calls, use events for cross-service communication. When an Order is placed, emit an “OrderPlaced” event rather than calling the Inventory service directly.</p><p>Start with Event Storming sessions to discover service boundaries collaboratively with domain experts. This helps ensure your architecture reflects real business needs rather than technical assumptions.</p><h3>The Right Approach</h3><p>Start with careful domain analysis. Identify bounded contexts in your monolith before touching any code. These boundaries should align with business capabilities, not technical concerns.</p><p>Build new features as services when they have minimal dependencies on the monolith. This lets you learn microservice patterns with less risk.</p><p>Implement robust monitoring, logging, and distributed tracing before you need them. You want these in place before debugging becomes difficult.</p><p>Consider introducing an anti-corruption layer between your monolith and new services. This helps manage the transition period and prevents new services from inheriting legacy domain models.</p><h3>Final Thoughts</h3><p>The path to microservices is more evolution than revolution. Focus on solving real organizational and technical problems rather than blindly following the hype. Success often correlates with how well teams understand and apply DDD principles to identify proper service boundaries.</p><p>Remember: Start with the domain, not the technology. Let your understanding of the business guide your architectural decisions. The best microservice boundaries are those that reflect real-world business capabilities and constraints.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=49b42a02b9df" width="1" height="1" alt=""><hr><p><a href="https://medium.com/delivus/breaking-down-the-monolith-expectations-vs-reality-49b42a02b9df">Breaking Down the Monolith: Expectations vs Reality</a> was originally published in <a href="https://medium.com/delivus">딜리버스</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to implement authentication in nextjs 15 using next-auth, prisma, postgres db: Part 1]]></title>
            <link>https://medium.com/delivus/how-to-implement-authentication-in-nextjs-15-using-next-auth-prisma-postgres-db-part-1-bc1b90d60d51?source=rss----4eecc410ebae---4</link>
            <guid isPermaLink="false">https://medium.com/p/bc1b90d60d51</guid>
            <category><![CDATA[nextjs]]></category>
            <category><![CDATA[postgres]]></category>
            <category><![CDATA[nextjs-app-router]]></category>
            <category><![CDATA[postgresql]]></category>
            <category><![CDATA[prisma-orm]]></category>
            <dc:creator><![CDATA[Chimgee G]]></dc:creator>
            <pubDate>Fri, 17 Jan 2025 02:12:09 GMT</pubDate>
            <atom:updated>2025-04-04T06:58:14.773Z</atom:updated>
            <content:encoded><![CDATA[<p>In this blog, we’ll create a simple Next.js application with a <strong>Sign In </strong>page, and a dashboard. To accomplish this, we’ll use the following tools:</p><ul><li><strong>Framework:</strong> <a href="https://nextjs.org/">Next.js</a></li><li><strong>Database:</strong> <a href="https://www.postgresql.org/">PostgreSQL</a></li><li><strong>ORM:</strong> <a href="https://www.prisma.io/">Prisma</a> (for database management)</li><li><strong>Authentication:</strong> <a href="https://next-auth.js.org/">NextAuth</a> (<a href="https://www.notion.so/Why-should-we-use-nextauth-17024414f57f8015a909db31b9fa57aa?pvs=21">Why NextAuth?</a>)</li><li><strong>Validation:</strong> <a href="https://zod.dev/">Zod</a></li></ul><p>This post covers <strong>Part 1</strong> of the implementation, focusing on setting up Prisma in a Next.js 15 project.</p><h3>Topics Covered in This Section:</h3><ol><li>Creating a Project</li><li>Setting up Prisma</li><li>Generating Database Tables</li><li>Generating Prisma Client</li><li>Seeding the Database with Dummy Data</li><li>Inserting and fetching Data from Tables</li></ol><h3>1. Create a Next.js Project</h3><p>Use the create-next-app CLI to generate a Next.js application:</p><pre>npx create-next-app@latest [project-name] [options]</pre><p>We created a sample project using the following options.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Z8su5xkhWZl2Q-cpwljGPg.png" /></figure><p>Here, we use “ospring” as a root directory alias, you can use any text instead of that.</p><p>Run the following command, and you will see sample nextjs page rendered in your browser.</p><pre>yarn dev</pre><h3>2. Set Up Prisma</h3><p>Step 1: Install Prisma</p><p>Navigate to the project’s root directory and install Prisma as a development dependency:</p><pre>yarn add -D prisma;</pre><p>Step 2: Initialize Prisma</p><p>Run the following command to initialize Prisma in your project:</p><pre>npx prisma init</pre><p>This creates a prisma/schema.prisma file with the following content:</p><pre>generator client {<br>  provider = &quot;prisma-client-js&quot;<br>}</pre><pre>//connection to DB<br>datasource db {<br>  provider = &quot;postgresql&quot;<br>  url      = env(&quot;DATABASE_URL&quot;)<br>}</pre><p>Step 3: Configure the .env File</p><p>Create .env file in root directory and add your database connection string to the .env file in the project’s root directory:</p><pre>//file name: .env<br>DATABASE_URL=postgresql://your_user_name:your_user_password@localhost:port/your_database_name?sslmode=ospring</pre><p>We assume that you’ve already prepared database server. If not, please read <a href="https://www.devart.com/dbforge/postgresql/how-to-install-postgresql-on-macos/">this tutorial</a>.</p><h3>3. Generate Database Tables</h3><p>Step 1: Define Table Schema</p><p>Add the following model definition to your prisma/schema.prisma file:</p><pre>// This block of code defines a User model to create user table<br>model User {<br>  id              String    @id @default(cuid()) // primary key with CUID<br>  name            String    @db.VarChar(255) // varchar<br>  phone           String    @unique @db.VarChar(255) // phone must be unique<br>  password        String    @db.VarChar(255)<br>  createdAt       DateTime  @default(now())<br>  updatedAt       DateTime    @updatedAt<br>}</pre><p>Here, we created only 1 table for storing user basic info to login.</p><p>Step 2: Push the Schema to the Database</p><p>Run the following command to apply the schema to your database:</p><pre>npx prisma db push</pre><p>If it executed successful, you’ll see the following message:</p><pre>🚀  Your database is now in sync with your Prisma schema. Done in 67ms</pre><pre>✔ Generated Prisma Client (v6.1.0) to ./node_modules/@prisma/client in 47ms</pre><p>After successfully executed the above command, we can check tables we defined were created in database using prisma studio. To do that, run the following command:</p><pre>npx prisma studio</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*g-8ydtHbz9GZr7upsciNXw.png" /></figure><h3>4. Generate the Prisma Client</h3><p>Step 1: Install the Prisma client:</p><pre>yarn add @prisma/client</pre><p>Step 2: Generate the client by running:</p><pre>npx prisma generate</pre><p>We should execute this command every time you make change prisma.schema file.</p><p>Step 3: Create a reusable Prisma client in a new app/lib/prisma.ts file:</p><pre>import {PrismaClient} from &quot;@prisma/client&quot;;<br>// }<br>const globalForPrisma = global as unknown as { prisma: PrismaClient };</pre><pre>// Creating a global instance of PrismaClient or using the existing one<br>export const prisma =<br>    globalForPrisma.prisma ||<br>    new PrismaClient({<br>        log: [&quot;query&quot;],<br>    });<br>// Setting the global Prisma instance if not in production<br>if (process.env.NODE_ENV !== &#39;production&#39;) globalForPrisma.prisma = prisma;</pre><p>This file imports PrismaClient we generated and exports its instance. We can control our database using this instance from anywhere from our project.</p><h3>5. Seed Dummy Data</h3><p>“<strong>Seeding</strong>” refers to adding initial data to the database or populating the database with sample data.</p><p>Step 1: Install bcryptjs</p><p>Install the bcryptjs package to hash passwords:</p><pre>yarn add bcryptjs<br>yarn add -D @types/bcryptjs</pre><p>Step 2: Create seed.ts file in /prisma folder.</p><p>In the prisma folder, create a seed.ts file with the following content:</p><pre>import {PrismaClient} from &#39;@prisma/client&#39;;<br>import bcryptjs from &quot;bcryptjs&quot;;</pre><pre>const prisma = new PrismaClient();<br>// Function to generate a hashed @password<br>const generatePasswordHash = async (password: string) =&gt; {<br>    // generates a random salt. A salt is a random value used in the hashing process to ensure<br>    // that even if two users have the same @password, their hashed passwords will be different.<br>    // The 10 in the function call represents the cost factor, which determines how much<br>    // computational work is needed to compute the hash.<br>    const salt = await bcryptjs.genSalt(10);<br>    return bcryptjs.hash(password, salt);<br>};</pre><pre>const initialUsers = [<br>    {name: &#39;Admin&#39;, phone: &#39;99999999&#39;},<br>    {name: &#39;User&#39;, phone: &#39;88888888&#39;},<br>    {name: &#39;Driver&#39;, phone: &#39;96969696&#39;},<br>];</pre><pre>const seed = async () =&gt; {<br>    // clean up before the seeding (optional)<br>    await prisma.user.deleteMany();</pre><pre>    // you could also use createMany<br>    // but it is not supported for databases<br>    // e.g. SQLite &lt;https://github.com/prisma/prisma/issues/10710&gt;<br>    for (const user of initialUsers) {<br>        const password = await generatePasswordHash(&#39;1q2w3e4&#39;)<br>        await prisma.user.create({<br>            data: {...user, password},<br>        });<br>    }<br>};</pre><pre>seed()<br>    .then(async () =&gt; {<br>        await prisma.$disconnect();<br>    })<br>    .catch(async (e) =&gt; {<br>        console.error(e);<br>        await prisma.$disconnect();<br>        process.exit(1);<br>    });</pre><p>Step 3: Create the Seeder Script</p><p>Add a seed script to package.json:</p><pre>...<br>&quot;scripts&quot;: {<br>    ...<br>    &quot;db-seed&quot;: &quot;prisma db seed&quot; <br>  },<br>  &quot;prisma&quot;: {<br>    &quot;seed&quot;: &quot;ts-node --compiler-options {\\&quot;module\\&quot;:\\&quot;CommonJS\\&quot;} prisma/seed.ts&quot;<br>  },<br>...</pre><p>Finally, run the seed script:</p><pre>yarn db-seed</pre><p>If prisma seed command was successfully executed, we can see the following text.</p><p>After running seed, we can check that we generated initialData in database we defined in seed.ts file.</p><h3>6. Insert and Fetch Data</h3><p>Let’s create user.ts file in /db folder. In a new app/db/user.ts file, define functions to manage user data:</p><pre>// Function to check if a user&#39;s phone is verified<br>import {prisma} from &quot;ospring/app/lib/prisma&quot;;</pre><pre>// Function to find a user by phone in the database<br>export const findUserByPhone = async (phone: string) =&gt; {<br>    return prisma.user.findFirst({<br>        where: {<br>            phone,<br>        },<br>    });<br>};</pre><pre>// Function to find a user by phone in the database<br>export const saveUser = async (phone: string, password: string) =&gt; {<br>    return prisma.user.create({<br>        data: {<br>            name: phone,<br>            phone,<br>            password<br>        },<br>    });<br>};</pre><h3>Final Project Structure</h3><p>You can find the source code for this example <a href="https://github.com/delivus-daas/next-sample-app/tree/PART1_prisma">here</a>.</p><p>In Part2, we’ll cover implementing authentication using <strong>NextAuth</strong>.</p><h3>References:</h3><p><a href="https://nextjs.org/docs/app/api-reference/cli/create-next-app">https://nextjs.org/docs/app/api-reference/cli/create-next-app</a></p><p><a href="https://vercel.com/guides/nextjs-prisma-postgres">https://vercel.com/guides/nextjs-prisma-postgres</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bc1b90d60d51" width="1" height="1" alt=""><hr><p><a href="https://medium.com/delivus/how-to-implement-authentication-in-nextjs-15-using-next-auth-prisma-postgres-db-part-1-bc1b90d60d51">How to implement authentication in nextjs 15 using next-auth, prisma, postgres db: Part 1</a> was originally published in <a href="https://medium.com/delivus">딜리버스</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[PWA Advanced Service Worker]]></title>
            <link>https://medium.com/delivus/pwa-advanced-service-worker-6459d5fce037?source=rss----4eecc410ebae---4</link>
            <guid isPermaLink="false">https://medium.com/p/6459d5fce037</guid>
            <category><![CDATA[pwa]]></category>
            <category><![CDATA[service-worker]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Chimgee G]]></dc:creator>
            <pubDate>Wed, 08 Jan 2025 07:13:01 GMT</pubDate>
            <atom:updated>2025-01-08T07:13:01.248Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Introduction</strong></h3><p>Service worker is a type of the web worker. It is javascript script file that runs separately from browser’s main thread. Service worker’s main features are the followings.</p><ul><li>intercepting network request</li><li>caching or retrieving resources from the cache</li><li>delivering push messages</li></ul><blockquote><em>If the browser being used doesn’t support background sync, there is no point storing it in the IndexedDB as it cannot be synced later, we directly send the request in this case</em></blockquote><p>Service workers are independent of the application, then</p><ul><li>Service worker cannot access local storage</li><li>Service worker cannot access DOM directly. To communicate with it, service workers call postMessage function.</li><li>Service workers intercept network request, then they only run over HTTPS.</li></ul><p>Service workers depend on 2 APIs.</p><ol><li>Fetch</li><li>Cache(independent from browser cache)</li></ol><h3>Service worker lifecycles</h3><p>Service workers have 3 lifecycles.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KNm7s08YqIy0reHuypNJVg.png" /></figure><ol><li>Registration</li></ol><p>Tells your browser where your service worker is located.</p><pre>navigator.serviceWorker.register(&#39;/service-worker.js&#39;, {<br>  scope: &#39;/app/&#39;<br>});</pre><ul><li>navigator.serviceWorker.register(&#39;/service-worker.js&#39;, { scope: &#39;/app/&#39; });</li></ul><p>The scope determines which files the service worker controls,</p><p>2. Installation</p><p>Once the the browser registers a service worker, installation can be attempted. Installation only occurs when service worker is newly registered or updated. A service worker installation triggers an install event. In install event, service worker can perform some tasks such as precaching. So web app can load instantly. The following is how to handle install event.</p><pre>// Listen for install event, set callback <br>self.addEventListener(&#39;install&#39;, function(event) {<br>    // Perform some task<br>});</pre><p>The install event is used to cache assets that are used in the offline mode.</p><pre>self.addEventListener(&#39;install&#39;, (event) =&gt; {<br>  event.waitUntil(<br>    caches.open(&#39;v1&#39;).then((cache) =&gt; {<br>      return cache.addAll([<br>        &#39;./sw-test/&#39;,<br>        &#39;./sw-test/index.html&#39;,<br>        &#39;./sw-test/style.css&#39;,<br>        &#39;./sw-test/app.js&#39;,<br>        &#39;./sw-test/image-list.js&#39;,<br>        &#39;./sw-test/star-wars-logo.jpg&#39;,<br>        &#39;./sw-test/gallery/&#39;,<br>    ...<br>      ]);<br>    })<br>  );<br>});</pre><p>3. Activation</p><p>After service worker’s installation is completed, it activates. But only one service worker is running at any given time. If there is previous service worker, which is running, newly installed service worker will wait until previous one finishes. When the new service worker activates, an activate event is triggered.</p><pre>self.addEventListener(&#39;activate&#39;, function(event) {<br>  // Perform some task<br>});</pre><p>The active event handler is good place to clean up old caches.</p><pre>self.addEventListener(&#39;activate&#39;, (event) =&gt; {<br>  var cacheKeeplist = [&#39;v2&#39;];<br><br>  event.waitUntil(<br>    caches.keys().then((keyList) =&gt; {<br>      return Promise.all(keyList.map((key) =&gt; {<br>        if (cacheKeeplist.indexOf(key) === -1) {<br>          return caches.delete(key);<br>        }<br>      }));<br>    })<br>  );<br>});</pre><p>After a service worker activates, it controls and listens events for all pages that are under its scope.</p><h3>Service worker events</h3><p>install and activate events are triggered when the service worker is installing and activates. Then there are other events that service worker can handle. Service workers won’t receive fetch and push events until it acticates</p><ul><li>Message events</li><li>Fetch events</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9H1t2uiPyOLdEYuK95ILgw.png" /></figure><p>You can handle fetch event of web pages which are within the service workers scope like the following code.</p><pre>self.addEventListener(&#39;fetch&#39;, (event) =&gt; {<br>  event.respondWith(<br>    // magic goes here<br>  );<br>});</pre><ul><li>self.addEventListener(&#39;fetch&#39;, (event) =&gt; { event.respondWith( // magic goes here ); });</li></ul><p>In the fetch event handler, you can check whether response is cached previously in the install event. If it is cached then return cached resource instead of requesting it from the network.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zOntPAxz0J6XklLRFnIiLg.png" /></figure><pre>self.addEventListener(&#39;fetch&#39;, (event) =&gt; {<br>  event.respondWith(<br>    caches.match(event.request).then((response) =&gt; {<br>      return response || fetch(event.request);<br>    })<br>  );<br>});</pre><ul><li>Push events</li><li>Sync events</li></ul><h3>Service worker usages</h3><p>Service workers provide the following features.</p><ul><li>Notification API</li><li>Push API</li><li>Background data Synchronization</li></ul><p>Service workers <strong>solve the page loading part</strong> by letting you serve content from a cache. But what about when the page needs to send something to the server? What if users want to close tab or lost internet connection? Background sync helps you to solve this issues. If you implement background sync in your PWA, users requests would be like during the offline mode</p><ol><li>Visit the pwa</li><li>Internet connection is lost</li><li>Send a request</li><li>Close browser or go to the another app</li><li>Internet connection goes online</li><li>Request sent is completed in the background</li></ol><p>To do that register sync for the page</p><pre>// Then later, request a one-off sync:<br>navigator.serviceWorker.ready.then(function(swRegistration) {<br>  return swRegistration.sync.register(&#39;myFirstSync&#39;);<br>});</pre><ul><li>// Then later, request a one-off sync: navigator.serviceWorker.ready.then(function(swRegistration) { return swRegistration.sync.register(&#39;myFirstSync&#39;); });</li></ul><p>Then listen for the event. “myFirstSync” is sync tag name, that should be unique if you want to perform separate syncs.</p><pre>self.addEventListener(&#39;sync&#39;, function(event) {<br>  if (event.tag == &#39;myFirstSync&#39;) {<br>    event.waitUntil(doSomeStuff());<br>  }<br>});</pre><p>“doSomeStuff” is a function that return promise indicating the success/failure of whatever it’s trying to do. If it fulfills, sync will complete, if it fails, another sync will be reserved. Background sync is used for</p><ul><li>photo upload</li><li>Channel Messaging API</li><li>Receives centralized updates to expensive-to-calculates data such as geolocation, so multiple pages can use one set of data</li><li>Hooks for background services</li><li>Custom templating based on certain URL patterns</li><li>Performance enhancements, for example pre-fetching resources that the user is likely to need in the near future, such as the next few pictures in a photo album.</li></ul><h3>Communicate with ClientPage</h3><p>Service cannot directly access to React DOM. Then how to update your user with service worker’s task progress.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZwLqcZEGlJcJkCuEusSgyw.png" /></figure><p>There are a several ways to communicate service worker with your client page.</p><ol><li><a href="https://developer.mozilla.org/en-US/docs/Web/API/Broadcast_Channel_API">Broadcast channel API</a> (which is not supported in Safari yet)</li><li><a href="https://developer.mozilla.org/en-US/docs/Web/API/Channel_Messaging_API">Message Channel</a></li><li>Client API</li></ol><p>I will clarify this method in this document.</p><p>In the Service Worker we select the client we want to send the response to with the filter option.</p><p>In the service-worker.ts — somewhere in the service worker</p><pre>self.clients.matchAll(options).then(function (clients) {<br>  if (clients &amp;&amp; clients.length) {<br>    //Respond to last focused tab<br>    clients[0].postMessage({type: &#39;MSG_ID&#39;});<br>  }<br>});in the Service Worker we select the client we want to send the response to with the filter options</pre><ul><li>self.clients.matchAll(options).then(function (clients) { if (clients &amp;&amp; clients.length) { //Respond to last focused tab clients[0].postMessage({type: &#39;MSG_ID&#39;}); } });in the Service Worker we select the client we want to send the response to with the filter options</li></ul><p>On the client-side, we listen to the response of the service worker.</p><p>In the App.tsx — somewhere in the web app</p><pre>if (navigator.serviceWorker.controller) {<br> navigator.serviceWorker.addEventListener(&quot;message&quot;, (event) =&gt; {<br>       console.log(&quot;onMessage&quot;, event.data);<br>       if (event.data &amp;&amp; event.data.type === &quot;MSG_ID&quot;) {<br>         //update ui to inform user<br>         alert(&quot;Upload completed.&quot;);<br>       }<br>  });<br>}</pre><p>Send message from the client to the service worker</p><p>In the App.tsx — somewhere in the web app</p><pre>navigator.serviceWorker.controller.postMessage({<br>  type: &#39;MESSAGE_IDENTIFIER&#39;,<br>});</pre><p>In the service-worker.ts — somewhere in the service worker.</p><pre>self.addEventListener(&#39;message&#39;, (event) =&gt; {<br>  if (event.data &amp;&amp; event.data.type === &#39;MESSAGE_IDENTIFIER&#39;) {<br>    // do something<br>  }<br>});</pre><h3>Whole source — sample github repository</h3><p><a href="https://github.com/Chimaa123/serviceworkertest">https://github.com/Chimaa123/serviceworkertest</a></p><h3>References</h3><p><a href="https://developers.google.com/web/ilt/pwa/introduction-to-service-worker">https://developers.google.com/web/ilt/pwa/introduction-to-service-worker</a></p><p><a href="https://developers.google.com/web/ilt/pwa/tools-for-pwa-developers">https://developers.google.com/web/ilt/pwa/tools-for-pwa-developers</a></p><p><a href="https://dev.to/ruppysuppy/supercharge-your-website-using-pwa-background-sync-1m23">https://dev.to/ruppysuppy/supercharge-your-website-using-pwa-background-sync-1m23</a></p><p><a href="https://developers.google.com/web/updates/2015/12/background-sync">https://developers.google.com/web/updates/2015/12/background-sync</a></p><p><a href="https://web.dev/two-way-communication-guide/">https://web.dev/two-way-communication-guide/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6459d5fce037" width="1" height="1" alt=""><hr><p><a href="https://medium.com/delivus/pwa-advanced-service-worker-6459d5fce037">PWA Advanced Service Worker</a> was originally published in <a href="https://medium.com/delivus">딜리버스</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Optimizing Your Database Connection Pool]]></title>
            <link>https://medium.com/delivus/optimizing-your-database-connection-pool-6d0be89f8a8b?source=rss----4eecc410ebae---4</link>
            <guid isPermaLink="false">https://medium.com/p/6d0be89f8a8b</guid>
            <category><![CDATA[database]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[optimization]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[connection-pool]]></category>
            <dc:creator><![CDATA[Hyunil Kim]]></dc:creator>
            <pubDate>Tue, 24 Dec 2024 00:59:45 GMT</pubDate>
            <atom:updated>2024-12-24T00:59:45.528Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*02lOS6HdJNd543x-ksW7FQ.jpeg" /></figure><p>Database connection pools are crucial for application performance, but determining the optimal pool size isn’t straightforward. In this guide, we’ll explore how to properly size your connection pool and maintain peak database performance.</p><h3>Why Connection Pool Optimization Matters</h3><p>Connection pool sizing has a profound impact on application performance, often more significant than query optimization or hardware upgrades. Here’s why:</p><h4><strong>Connection Establishment Overhead</strong></h4><p>Each new database connection requires a TCP handshake, authentication, and resource allocation, taking anywhere from 10ms to 100ms. For high-throughput applications, this overhead can accumulate to significant latency.</p><h4><strong>Resource Consumption</strong></h4><p>Each database connection consumes approximately 10MB of server memory. A poorly configured pool of 1000 connections could unnecessarily consume 10GB of RAM, potentially leading to OOM issues or excessive swap usage.</p><h4><strong>Hidden Bottlenecks</strong></h4><p>When connection pools are misconfigured, issues often manifest as seemingly unrelated problems:</p><ul><li>Sudden application timeouts during peak loads</li><li>Mysterious performance degradation after code deployments</li><li>Unexplained database CPU spikes</li><li>Intermittent transaction deadlocks</li></ul><h4><strong>Cascading Failures</strong></h4><p>An oversized pool can mask problems until it’s too late. For example, if your database has a limit of 1000 connections and your pool is set to 900, you might not notice issues until a traffic spike causes multiple application instances to exceed the total connection limit, leading to a cascade of failures.</p><h3>Understanding Connection Pools</h3><p>A connection pool maintains a cache of database connections that can be reused when future requests to the database are required. Instead of opening a new connection for every database operation, the application can reuse existing connections from the pool, which:</p><ul><li>Reduces the overhead of establishing new connections</li><li>Manages concurrent database operations efficiently</li><li>Controls the maximum number of connections to the database</li></ul><h3>Factors Affecting Pool Size</h3><p>Several factors influence the optimal connection pool size:</p><h4>1. Available Database Resources</h4><p>Your database server has limits on:</p><ul><li>Maximum concurrent connections</li><li>CPU capacity</li><li>Memory resources</li><li>I/O capacity</li></ul><p>For example, PostgreSQL’s default max_connections is typically 100, while MySQL’s default max_connections is 151. You’ll need to stay well below these limits, considering other applications that might connect to the database.</p><h4>2. Application Characteristics</h4><p>Consider these application-specific factors:</p><ul><li>Number of concurrent users</li><li>Average request duration</li><li>Transaction patterns</li><li>Peak load periods</li><li>Application thread pool size</li></ul><h4>3. Hardware Resources</h4><p>Both application and database servers’ resources matter:</p><ul><li>CPU cores available</li><li>Memory capacity</li><li>Network bandwidth</li><li>Disk I/O capabilities</li></ul><h3>Calculating Optimal Pool Size</h3><p>Here’s a practical approach to calculate your initial pool size:</p><h3>The Core Formula</h3><pre>Optimal Pool Size = Tn × (Cm - 1) + 1<br><br>Where:<br>Tn = Number of threads that need concurrent database access<br>Cm = Number of connections needed per thread</pre><h3>Practical Steps for Sizing</h3><h4><strong>Start with a baseline:</strong></h4><ul><li>Pool size = (CPU cores × 2) + 1</li><li>This provides a reasonable starting point for many applications</li></ul><h4><strong>Monitor and adjust:</strong></h4><ul><li>Track connection wait times</li><li>Monitor database CPU usage</li><li>Observe connection usage patterns</li><li>Measure transaction response times</li></ul><h3>Common Pitfalls to Avoid</h3><h4><strong>Oversizing the Pool</strong></h4><ul><li>Wastes database resources</li><li>Can lead to increased contention</li><li>May cause higher latency due to context switching</li></ul><h4><strong>Undersizing the Pool</strong></h4><ul><li>Creates connection bottlenecks</li><li>Increases request queuing</li><li>Leads to timeouts during peak loads</li></ul><h4><strong>Ignoring Connection Leaks</strong></h4><ul><li>Failed to close connections properly</li><li>No monitoring of connection usage</li><li>Missing connection timeout configurations</li></ul><h3>Conclusion</h3><p>Optimizing your database connection pool size requires careful consideration of your application’s needs and resources. Start with a conservative estimate based on your system’s characteristics and implement proper monitoring. Regular review and adjustment of your pool configuration ensures optimal performance as your application grows.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6d0be89f8a8b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/delivus/optimizing-your-database-connection-pool-6d0be89f8a8b">Optimizing Your Database Connection Pool</a> was originally published in <a href="https://medium.com/delivus">딜리버스</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>