C.W.K.
Stream
Lesson 06 of 06 · published

실전 Idempotency

~11 min · idempotency, production, patterns

Level 0구경꾼
0 XP0/47 lessons0/11 achievements
0/120 XP to next level120 XP to go0% complete

파이프라인이 가질 수 있는 가장 가치 있는 단일 속성

Idempotent 파이프라인은 한 번이든, 두 번이든, 스무 번이든 같은 결과를 만들어. 한번 이 속성 가지면 디버깅이 더는 무섭지 않아 — "그냥 다시 돌려" 가 희망 섞인 기도 대신 진짜 답이 돼. 없으면 모든 retry 가 주사위 굴리기.

파이프라인을 idempotent 로 만드는 네 가지 실용 패턴

  1. Path 인코딩 immutability raw lake 에. raw/orders/2026/04/30/run_20260430120000.json.gz. Path 자체에 run timestamp 포함. 두 번째 run 이 옆에 새 파일 작성; downstream stage 가 사전 순으로 최신 읽음.
  2. Partition replace 분석 테이블에. warehouse/orders/date=2026-04-30/. 다시 돌리면 atomic 하게 partition 교체 (stage + swap).
  3. Primary key 로 upsert OLTP 모양 destination 에. INSERT ... ON CONFLICT (order_id) DO UPDATE. 같은 key 가 이김; 다시 돌리면 결과 no-op.
  4. Idempotent side effect 데이터 아닌 거 — 이메일, 웹훅, 알림 — 에. dedup key ("2026-04-30 의 Q1 매출 임계 알림") 와 추적 테이블 사용.

Code

Idempotent 알림 sender — 보낸 key 추적해서 dedup·python
import sqlite3

def maybe_send_alert(conn: sqlite3.Connection, key: str, body: str) -> bool:
    '''키당 최대 한 번 알림 보냄. 보냈으면 True.'''
    cur = conn.execute('SELECT 1 FROM sent_alerts WHERE key = ?', (key,))
    if cur.fetchone() is not None:
        return False

    send_email(body)                                          # side effect
    conn.execute('INSERT INTO sent_alerts(key, sent_at) VALUES (?, datetime("now"))', (key,))
    conn.commit()
    return True

# 둘러싼 파이프라인이 다섯 번 돌아도 알림은 한 번만
maybe_send_alert(conn, key='q1-revenue-2026-04-30', body='Q1 매출 12% 하락')

External links

Exercise

본인이 쓴 파이프라인 (또는 스케줄 스크립트) 하나 골라. 각 side effect — 파일 write, DB insert, 이메일 send, API call — 가 idempotent 인지 점수. 가장 나쁜 것에 대해 idempotent 로 만들 변경 (path 인코딩, partition replace, upsert, dedup 테이블) sketch. 오늘 ship 안 해도 됨; 진단 자체가 lesson.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.