Mais conteúdo relacionado
Semelhante a Amazon DynamoDB 신기능 OnDemand 와 Transaction :: 윤평호 - AWS Community Day 2019 (20)
Mais de AWSKRUG - AWS한국사용자모임 (20)
Amazon DynamoDB 신기능 OnDemand 와 Transaction :: 윤평호 - AWS Community Day 2019
- 1. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Amazon DynamoDB 신기능
On-Demand and Transaction
윤평호 AWS Community Day 2019@20190125
Software Engineer
AWSKRUG
Feel free to contact: learder@gmail.com
D a t a b a s e / A n a l y t i c s
- 2. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
목차
1. 온 디맨드 용량 방식
1. 소개
2. 기존 방식과 비교
3. 언제 사용하지?
2. 트랜젝션 지원
1. 왜 필요하고
2. 어떻게 사용하고 사용 예를 보고
3. 추가 기능 및 비용은?
4. 사용예를 하나 더 살펴봅시다.
- 3. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
다이나모디비(DynamoDB)
규모에 관계없이 완전히 관리되는 비관계형 데이테베이스
Performance at scale
Single-digit millisecond response
Consistent perf at any scale
Virtually unlimited throughput
Secondary indexes
Flexible data model
Enterprise ready
Encryption at rest and transit
Fine-grained access control
99.99% service level agreement
Backup and restore
Fully managed
Security
Durability
Availability
Adaptive capacity
Maintenance free
Global tables
DynamoDB Streams
Time To Live
- 4. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
완전 관리형(Fully Managed)
규모에 관계없이 완전히 관리되는 비관계형 데이테베이스
비지니스 로직에 좀 더 집중할 수 있습니다!
Security
Operating system patching
Database patching
Access control
Audit
Encryption
Compliance
Durability
Sustain server, rack, and datacenter outages
Re-replicate data quickly upon hardware failure
Manage backup and restore
Availability
High availability configuration
Monitoring
Cross-region replication
Performance
Performance tuning
Indexing
In-memory caching
Scalability
Capacity planning
Host provisioning
Host repair and retirement
- 5. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
- 6. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
데이터베이스 용량 산정
실제 사용량
- 7. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
관계형 데이터베이스에서는 일반적으로 Scale-Up
- 8. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
NoSQL 에서는 Scale-Out
- 9. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
다이나모디비(DynamoDB, Managed NoSQL database)
서버리스
관리할 소프트웨어나
서버가 없음
대규모확장성
원하는만큼 많은
처리량을 원할때
고성능
일관된 1자리수
밀리세컨드 응답
- 10. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
온디맨드(용량)?
- 11. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
온디맨드(용량)의 기능과 장점
기능
• 용량 계산, 계획 하거나 예약할 필요없음
• 사용한 읽기, 쓰기 개수만큼 과금
장점
• 용량을 낭비하거나 부족함이 없음.
• 트래픽에 따라 즉각적으로 작업 부하를 자동 조정
- 12. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
기존 용량 산정 방식과 비교
최대 소비량을 관리
- 13. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
온디맨드(용량) 요금은?
서울 리전 (ap-northeast-2)은? 조금…
• $1.3556 쓰기 요청 100만개당
• $0.2711 읽기 요청 100만 개당
• $2.033 복제된 쓰기 요청 100만 개당
(글로벌 테이블)
- 14. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
온디맨드(용량)은 언제 사용하나?
• 예약 용량(Provisioned)
• 싸다. 라이브 운영은 이것으로! Burst capacity 로 유용히
• 오토스케일(Auto-scale)
• 스케일업 하는 지연시간 :<
• 지정한 사용율을 유지하지 못하면 스케일링 안됨
• 상한 하한을 정할 수 있으나…
• 온디맨드(On-Demand)
• 서비스는 해야겠는데, 사용양이 예측 불가일 때에
- 15. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
- 16. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
이 기능이 중요한 이유?
트랜젝션 API 로 실생활의 트랜젝션을 (쉽게) 구현할 수 있습니다.
동시다발적 쓰기(write),
여러 아이템을 갱신(update)
처리하기 전에
여러가지 조건 검사
다양한 테이블 간에
데이터의 일관성 유지
- 17. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
1번의
API
호출
다이나모디비 트랜젝션
• 데드락 X
• 긴 트랜잭션 시간 X
• 열린 트랜젝션 X
• 관리되지 않은 동시성 X
• DBA의 괴로움 X
- 18. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
추가된 트랜젝션 API
TransactWriteItems
작업을 10 개 까지 묶을 수 있음
조건 추가 가능
모든 조건이 맞을 때, 쓰기(write) 작업 수행
TransactGetItems
작업을 10개까지 묶을 수 있음
일관되게, 모든 항목의 격리된 스냅샷을 가지고 옴
- 19. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
다이나모디비에서 ACID 지원은?
• 이전에는 단일 아이템 연산(single-item operations)만 ACID 지원
• 이제는 다중 아이템 연산(multi-item operations)에도 ACID 지원
비 트랜잭션 트랜잭션
신규 API
TransactGetItems
TransactWriteItems
기존 API
BatchGetItem, BatchWriteItem
Query, Scan
GetItem, PutItem, UpdateItem,
DeleteItem
- 20. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
NoSQL 트랜젝션의 성공 경로?
설계 선택
• 계정 내에 모든 항목에 트랜젝션 가능
• 서비스 API 와 클라이언트 API
• 요청/응답 기반
• 작업 개수 제한(10개)
어떤 면에서 도움이 되나?
• 데이터와 관계를 늘릴 수 있다
• 복잡성은 줄이고, 비용과 성능 개선이 쉽다
• 비지니스 로직은 애플리케이션에 두고,
데이터베이스 단에 두지 않음
• 일관된 낮은 대기 시간으로, 데드락 발생 안함
• 일관된 낮은 대기시간으로, 높은 가용성 보장
• 대다수의 트랜젝션 사용 사례를 지원함
- 21. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
// Status of Hammer57
GET {
“TableName”:"Gamers",
“Key” : {
"GamerTag":{“S”:"Hammer57”},
"Type":{“S”: "Status”}
}
}
// Return all Hammer57
Gamers
GamerTag = :a
:a Hammer57
Gamers
Primary Key
Attributes
Gamer Tag Type
Hammer57
Rank
Level Points Tier
87 4050 Elite
Status
Health Progress
90 30
Weapon
Class Damage Range
Taser 87% 50
Assets
Coins
1400
FluffyDuffy
Rank
Level Points Tier
5 1072 Trainee
Status
Health Progress
37 8
Assets
Coins
0
비 트랜젝션은 어떤 모양?
- 22. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
// Atomic Update of Hammer57
// Health & Coins
Gamers
Primary Key
Attributes
Gamer Tag Type
Hammer57
Rank
Level Points Tier
87 4050 Elite
Status
Health Progress
10 30
Weapon
Class Damage Range
Taser 87% 50
Assets
Coins
1400
FluffyDuffy
Rank
Level Points Tier
5 1072 Trainee
Status
Health Progress
37 8
Assets
Coins
0
트랜젝션이 필요한 때?
- 23. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
// Atomic Update of Hammer57
// Health & Coins
{ "TransactItems" : [ {
"Update ": {
"TableName": "Gamers",
”Key" :{"GamerTag" : {"S": "Hammer57"},
"Type" : {"S" : "Status"}},
"UpdateExpression" : "Set health = :nhealth",
"ExpressionAttributeValues":{":nhealth":{“N”:“100”}},
}
},
{
"Update ": {
"TableName": "Gamers",
”Key" :{"GamerTag" : {"S": "Hammer57"},
"Type" : {"S" : "Assets"} },
"ConditionExpression" : "coins > :cost",
"UpdateExpression" : "Set coins = coins - :cost",
"ExpressionAttributeValues" :{":cost":{“N”:“400”}}
}
}]
}
Gamers
Primary Key
Attributes
Gamer Tag Type
Hammer57
Rank
Level Points Tier
87 4050 Elite
Status
Health Progress
100 30
Weapon
Class Damage Range
Taser 87% 50
Assets
Coins
1000
FluffyDuffy
Rank
Level Points Tier
5 1072 Trainee
Status
Health Progress
37 8
Assets
Coins
0
트랜젝션 예
- 24. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
트랜젝션이 실패하는 경우?
• 아이템 단위로 실패하는 경우
• 검색 조건 실패(이미 바뀌었어…)
• 읽기/쓰기 용량 부족
• 트랜젝션 충돌
• 다른 이유
• 트랜잭션 진행 중
• 서비스 에러
• 잘못된 구성한 요청
• 퍼미션
- 25. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
-프로파일관리(동일하지않은테이블),호텔예약(재요청처리)
- 26. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
사용자 프로파일 관리(use case #1)
• 문제 정의
• 사용자는 사용자명(user name)과 선택사항으로 이메일(email) 로 구분
• 식별자(사용자명과 이메일)는 고유한 값입니다.
• 사용자는 이메일을 변경할 수 있습니다.
- 27. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
사용자 프로파일 관리 데이터 모델
• 해결 방안
• Users 테이블은 아래와 같이 종류의 레코드 형식을 갖도록 합니다.
• Profile 레코드는 사용자명과 세부 정보를 갖습니다.
• Alias 레코드는 이메일과 참조하는 Profile 레코드의 참조인자를 갖습니다.
Id UserNameRef Email Phone #
“John123” “john@example.com” “444-555-0110”
“john@example.com” “Joe123”
“jane456” “444-555-0199”
- 28. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
새 사용자 프로파일 추가
data = await dynamoDb.transactWriteItems({ TransactItems: [
{ Put: {
TableName: 'Users',
Item: { Id: { S: 'John123' }, Email: {S: 'john123@example.com'}, ...},
ConditionExpression: 'attribute_not_exists(Id)‘ }},
{ Put: {
TableName: 'Users',
Item: { Id: { S: 'john123@example.com' }, UserNameRef: {S: 'John123'}},
ConditionExpression: 'attribute_not_exists(Id)',}}
] }).promise();
- 29. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
이메일 갱신
data = await dynamoDb.transactWriteItems({ TransactItems: [
{ Put: { TableName: 'Users',
Item: { Id: { S: 'John123' }, Email: {S: 'john123@example.org'}, ...},
ConditionExpression: 'attribute_exists(Id) and Email = :old_email',
ExpressionAttributeValues: {':old_email' :{'S': 'john123@example.com'}}}},
{ Put: { TableName: 'Users',
Item: { Id: { S: 'john123@example.org' }, UserNameRef: {S: 'John123'}},
ConditionExpression: 'attribute_not_exists(Id)', }},
{ Delete: { TableName: 'Users',
Item: { Id: { S: 'john123@example.com' }}}).promise();
- 30. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
호텔 예약 관리 (use case #2)
• 문제정의
• 손님은 객실을 예약 할 수 있습니다.
• 손님은 객실을 체크인하여 예약을 활성화합니다.
• 손님은 체크인한 객실을 체크 아웃하고 예약을 종료합니다.
- 31. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
호텔 예약 관리 데이터 모델
Guests
Id (primary key)
Name, Email, etc.
Reservations (set)
OccupiesRooms (set)
Reservations
Id (primary key)
GuestId
FulfilledByRoom
State
Rooms
Id (primary key)
RentedToReservation
State
- 32. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
호텔 예약 관리 데이터 모델
레코드 타입 Id (PK) Attributes
Guest “John”
Reservations : { “500”, “501” }
OccupiesRooms : { “20014” }
Reservation “500”
GuestId: “John”
State: “PENDING”
Reservation “501”
GuestId: “John”
State: “ACTIVATED”
FulfilledByRoom: “20014”
Room “20014”
State: “OCCUPIED”
RentedToReservation : “501”
- 33. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
호텔 예약 관리: 예약하기
• 재시도해도 고유하게 예약되도록 하려면?
• 해결 방안
• 클라이언트에서 고유한 예약 ID 생성
• 클라이언트에서 TransactWriteItems 아래 작업을 묶어 요청
• 새 예약을 위한 조건적인 PUT
• 손님 예약 테이블 셋을 갱신 아니면 새 예약 추가
• 클라이언트 트랜잭션은 더블 부킹되는 일은 없다.
• 색이 바뀌는 필드를 살펴보십시요
- 34. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
호텔 예약 관리: 체크인
레코드 타입 Id (PK) Attributes
Customer “John123”
Reservations : { “567” }
OccupiesRooms : {}
Reservation “567”
CustomerId: “John123”
State: “PENDING”
FulfilledByRoom: null
Room “20014”
State: “FREE”
RentedToReservation : null
Before
- 35. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
호텔 예약 관리: 체크인
레코드 타입 Id (PK) Attributes
Customer “John123”
Reservations : { “567” }
OccupiesRooms : { “20014” }
Reservation “567”
CustomerId: “John123”
State: “ACTIVATED”
FulfilledByRoom: “20014”
Room “20014”
State: “OCCUPIED”
RentedToReservation : “567”
After
- 36. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
호텔 예약 관리: 체크아웃
레코드 타입 Id (PK) Attributes
Customer “John123”
Reservations : { “567” }
OccupiesRooms : { “20014” }
Reservation “567”
CustomerId: “John123”
State: “ACTIVATED”
FulfilledByRoom: “20014”
Room “20014”
State: “OCCUPIED”
RentedToReservation : “567”
Before
- 37. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
호텔 예약 관리: 체크아웃
레코드 타입 Id (PK) Attributes
Customer “John123”
Reservations : { “567” }
OccupiesRooms : { “20014” }
Reservation “567”
CustomerId: “John123”
State: “CLOSED”
FulfilledByRoom: “20014”
Room “20014”
State: “FREE”
RentedToReservation : “567”
After
- 38. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
- 39. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
트랜젝션 비용?
트랜잭션은 비트랜젝션에 비해 2배의 일을 한다.
트랜젝션은 작업 당 2배 단위로 읽기/쓰기 용량을 사용한다.
- 40. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
- 첨부파일관리(레퍼런스카운팅)
- 41. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
첨부파일 관리 (use case #3)
• 문제정의
• SNS에서 게시물에 첨부 파일을 허용한다.
• 게시물은 첨부 파일을 효과적으로 공유할 수 있어야 한다.
• 게시물은 여러개의 첨부 파일을 첨부 가능하다.
• 참조 되지 않는 첨부 파일은 제거되어야 한다.
- 42. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
첨부파일 관리: 데이터 모델
첨부파일 테이블
S3Ref (PK) – S3 레퍼런스
RefCount – 참조 회수
TTL – 사용되지 않을 경우 삭제되어야 할 적절한 시간
게시물 테이블
Id (PK) – 게시물 ID
Attachments (set) – 첨부파일 ID 집합
- 43. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
첨부파일 관리 불변 조건
1. RefCount 는 첨부파일을 참조하는 게시물 개수
2. RefCount > 0 이면 S3Ref 는 정상 S3 오브젝트이다.
3. TTL 이 설정되어 있으면, RefCount = 0 이다. 반대 논리도 성립