Giter Site home page Giter Site logo

coupon-system's Introduction

coupon-system

[Redis/Kafka] 실습으로 배우는 선착순 이벤트 시스템

1. 요구사항 정의

선착순 100명에게 할인쿠폰을 제공하는 이벤트를 진행하고자 한다.

이 이벤트는 아래와 같은 조건을 만족하여야 한다.

  • 선착순 100명에게만 지급되어야한다.
  • 101개 이상이 지급되면 안된다.
  • 순간적으로 몰리는 트래픽을 버틸 수 있어야 합니다.

2. 문제점

race condition

  • 두 개 이상의 쓰레드가 공유 데이터에 엑세스하고 작업하려고 할 때 발생하는 문제

예상 동작

Thread-1 Coupon count Thread-2
select count(*) from coupon 99
create coupon 100
100 select count(*) from coupon
100 failed create coupon

실제 동작

Thread-1 Coupon count Thread-2
select count(*) from coupon 99
99 select count(*) from coupon
create coupon 100
101 create coupon

3. Redis를 활용하여 문제 해결하기

4. Kafka를 활용하여 문제 해결하기

카프카란?

  • 분산 이벤트 스트리밍 플랫폼
  • 이벤트 스트리밍이란 소스에서 목적지까지 이벤트를 실시간으로 스트리밍하는 것
graph LR
    producer --> Topic
    Consumer --> Topic
Loading
  • 토픽 생성
docker exec -it kafka kafka-topics.sh --bootstrap-server localhost:9092 --create --topic testTopic
  • 프로듀서 실행
docker exec -it kafka kafka-console-producer.sh --topic testTopic --broker-list 0.0.0.0:9092
  • 컨슈머 실행
docker exec -it kafka kafka-console-consumer.sh --topic testTopic --bootstrap-server localhost:9092
  • 프로듀서를 활용하여 쿠폰을 생성할 유저의 아이디를 토픽에 넣고 컨슈머를 활용하여 유저 아이디를 가져와서 쿠폰을 생성

  • kafka는 실시간이 아님

Time Test case Producer Consumer
10:00 테스트케이스 시작 데이터 수신 중...
10:01 데이터 전송 완료 데이터 처리...
10:02 테스트케이스 종료 데이터 처리...
10:03 데이터 처리...
10:04 데이터 처리완료

5. 요구사항 변경

6. 시스템 개선하기

coupon-system's People

Contributors

yunyoung1819 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.