AWS Learning
Database

Amazon Neptune

Graph Database, Gremlin, SPARQL, Social Networks, Fraud Detection

Tổng quan

Amazon Neptunefully managed graph database service của AWS, được tối ưu cho việc lưu trữ và truy vấn các mối quan hệ phức tạp giữa dữ liệu.

┌─────────────────────────────────────────────────────────────────────────────┐
│                           AMAZON NEPTUNE                                    │
├─────────────────────────────────────────────────────────────────────────────┤
│                                                                             │
│   Định nghĩa ngắn gọn:                                                      │
│   ════════════════════                                                      │
│   • Fully managed GRAPH DATABASE                                            │
│   • Optimized cho highly connected data                                     │
│   • Hỗ trợ cả Property Graph và RDF                                         │
│   • Query bằng Gremlin, SPARQL, hoặc openCypher                             │
│                                                                             │
│   Khi nào dùng?                                                             │
│   ════════════════════                                                      │
│   • Social networks (ai là bạn của ai?)                                     │
│   • Recommendation engines (bạn có thể thích gì?)                           │
│   • Fraud detection (giao dịch nào đáng ngờ?)                               │
│   • Knowledge graphs (entities liên quan thế nào?)                          │
│                                                                             │
└─────────────────────────────────────────────────────────────────────────────┘

Graph Database là gì

Relational vs Graph Database

┌─────────────────────────────────────────────────────────────────────────────┐
│                  RELATIONAL vs GRAPH DATABASE                               │
├─────────────────────────────────────────────────────────────────────────────┤
│                                                                             │
│  RELATIONAL DATABASE (RDS, Aurora):                                         │
│  ══════════════════════════════════                                         │
│                                                                             │
│  Users Table          Friends Table                                         │
│  ┌────┬───────┐      ┌─────────┬─────────┐                                  │
│  │ ID │ Name  │      │ User_ID │Friend_ID│                                  │
│  ├────┼───────┤      ├─────────┼─────────┤                                  │
│  │ 1  │ Alice │      │    1    │    2    │                                  │
│  │ 2  │ Bob   │      │    1    │    3    │                                  │
│  │ 3  │ Carol │      │    2    │    3    │                                  │
│  └────┴───────┘      └─────────┴─────────┘                                  │
│                                                                             │
│  → Cần JOIN nhiều bảng để tìm relationships                                 │
│  → Slow khi relationships phức tạp                                          │
│                                                                             │
├─────────────────────────────────────────────────────────────────────────────┤
│                                                                             │
│  GRAPH DATABASE (Neptune):                                                  │
│  ══════════════════════════                                                 │
│                                                                             │
│           ┌───────┐                                                         │
│           │ Alice │                                                         │
│           └───┬───┘                                                         │
│        FRIEND │ FRIEND                                                      │
│          ┌────┴────┐                                                        │
│          ▼         ▼                                                        │
│      ┌───────┐ ┌───────┐                                                    │
│      │  Bob  │─│ Carol │                                                    │
│      └───────┘ └───────┘                                                    │
│           FRIEND                                                            │
│                                                                             │
│  → Relationships là first-class citizens                                    │
│  → Traversal nhanh, không cần JOIN                                          │
│                                                                             │
└─────────────────────────────────────────────────────────────────────────────┘

Thuật ngữ cơ bản

TermMô tảVí dụ
Node/VertexEntity trong graphUser, Product, Movie
EdgeRelationship giữa nodesFRIEND_OF, PURCHASED, RATED
PropertyAttribute của node hoặc edgename, age, since
LabelCategory của nodePerson, Product

Các loại Graph Models

Neptune hỗ trợ 2 loại graph models:

┌─────────────────────────────────────────────────────────────────────────────┐
│                      GRAPH MODELS IN NEPTUNE                                │
├─────────────────────────────────────────────────────────────────────────────┤
│                                                                             │
│  1. PROPERTY GRAPH                                                          │
│  ═══════════════════                                                        │
│                                                                             │
│  • Query languages: Gremlin, openCypher                                     │
│  • Nodes và edges có properties (key-value pairs)                           │
│  • Phổ biến cho: Social networks, fraud detection                           │
│                                                                             │
│     ┌──────────────┐    PURCHASED     ┌──────────────┐                      │
│     │ User: Alice  │ ───────────────► │ Product: X   │                      │
│     │ age: 30      │    price: $100   │ category: Y  │                      │
│     └──────────────┘                  └──────────────┘                      │
│                                                                             │
├─────────────────────────────────────────────────────────────────────────────┤
│                                                                             │
│  2. RDF (Resource Description Framework)                                    │
│  ═══════════════════════════════════════                                    │
│                                                                             │
│  • Query language: SPARQL                                                   │
│  • Data stored as triples: Subject - Predicate - Object                     │
│  • Phổ biến cho: Knowledge graphs, semantic web                             │
│                                                                             │
│     Subject          Predicate         Object                               │
│     ────────         ─────────         ──────                               │
│     :Alice           :knows            :Bob                                 │
│     :Alice           :age              30                                   │
│     :Alice           :worksAt          :AWS                                 │
│                                                                             │
└─────────────────────────────────────────────────────────────────────────────┘

Query Languages

ModelQuery LanguageSyntax Style
Property GraphGremlinImperative (step-by-step)
Property GraphopenCypherDeclarative (like SQL)
RDFSPARQLDeclarative

Kiến trúc

┌─────────────────────────────────────────────────────────────────────────────┐
│                       NEPTUNE ARCHITECTURE                                  │
├─────────────────────────────────────────────────────────────────────────────┤
│                                                                             │
│                          ┌─────────────────┐                                │
│                          │   Application   │                                │
│                          └────────┬────────┘                                │
│                                  │                                          │
│                    Gremlin / SPARQL / openCypher                            │
│                                  │                                          │
│                                   ▼                                         │
│   ┌─────────────────────────────────────────────────────────────────────┐   │
│   │                     NEPTUNE CLUSTER                                 │   │
│   │  ┌───────────────────────────────────────────────────────────────┐  │   │
│   │  │                    PRIMARY INSTANCE                            │ │   │
│   │  │                    (Read/Write)                                │ │   │
│   │  └───────────────────────────────────────────────────────────────┘  │   │
│   │                              │                                      │   │
│   │         ┌────────────────────┼────────────────────┐                 │   │
│   │         ▼                    ▼                    ▼                 │   │
│   │  ┌────────────┐       ┌────────────┐       ┌────────────┐           │   │
│   │  │  Replica   │       │  Replica   │       │  Replica   │           │   │
│   │  │ (Read only)│       │ (Read only)│       │ (Read only)│           │   │
│   │  └────────────┘       └────────────┘       └────────────┘           │   │
│   │         AZ-a                AZ-b                AZ-c                │   │
│   └─────────────────────────────────────────────────────────────────────┘   │
│                                  │                                          │
│                                   ▼                                         │
│   ┌─────────────────────────────────────────────────────────────────────┐   │
│   │                 SHARED STORAGE (6 copies across 3 AZs)              │   │
│   │                    Auto-scaling up to 128 TB                        │   │
│   └─────────────────────────────────────────────────────────────────────┘   │
│                                                                             │
└─────────────────────────────────────────────────────────────────────────────┘

Key Features

FeatureMô tả
High AvailabilityMulti-AZ, automatic failover
Durability6 copies across 3 AZs
ScalabilityUp to 15 read replicas
StorageAuto-scaling up to 128 TB
BackupContinuous backup to S3
EncryptionAt rest (KMS) và in transit (TLS)

Use Cases

┌─────────────────────────────────────────────────────────────────────────────┐
│                        NEPTUNE USE CASES                                    │
├─────────────────────────────────────────────────────────────────────────────┤
│                                                                             │
│  1. SOCIAL NETWORKING                                                       │
│  ═════════════════════                                                      │
│                                                                             │
│     "Tìm bạn của bạn của Alice"                                             │
│     "Ai có nhiều connections nhất?"                                         │
│     "Gợi ý kết bạn dựa trên mutual friends"                                 │
│                                                                             │
│  2. RECOMMENDATION ENGINES                                                  │
│  ═══════════════════════════                                                │
│                                                                             │
│     "Người dùng đã mua X cũng mua Y"                                        │
│     "Movies tương tự mà bạn có thể thích"                                   │
│     "Sản phẩm phổ biến trong network của bạn"                               │
│                                                                             │
│  3. FRAUD DETECTION                                                         │
│  ═══════════════════                                                        │
│                                                                             │
│     "Account này có liên kết với fraud ring không?"                         │
│     "Phát hiện circular transactions"                                       │
│     "Identify suspicious patterns"                                          │
│                                                                             │
│  4. KNOWLEDGE GRAPHS                                                        │
│  ═══════════════════                                                        │
│                                                                             │
│     "Barack Obama là tổng thống của nước nào?"                              │
│     "Các công ty con của Amazon là gì?"                                     │
│     "Semantic search và reasoning"                                          │
│                                                                             │
│  5. NETWORK/IT OPERATIONS                                                   │
│  ═════════════════════════                                                  │
│                                                                             │
│     "Server nào kết nối với server này?"                                    │
│     "Impact analysis khi 1 node down"                                       │
│     "Dependency mapping"                                                    │
│                                                                             │
└─────────────────────────────────────────────────────────────────────────────┘

So sánh với các Database khác

FeatureNeptuneDynamoDBRDS/AuroraDocumentDB
TypeGraphKey-Value/DocumentRelationalDocument
Best forRelationshipsSimple lookupsStructured dataJSON documents
QueryGremlin/SPARQLPartiQLSQLMongoDB API
JoinsNative traversalNot supportedExpensiveLimited
SchemaFlexibleFlexibleFixedFlexible

Khi nào dùng Neptune vs RDS?

ScenarioDùng
Nhiều JOINs phức tạp giữa entitiesNeptune
"Ai biết ai biết ai?" (deep traversal)Neptune
Standard CRUD operationsRDS
Reporting, analytics với SQLRDS
Dữ liệu có relationships đơn giảnRDS

Neptune Features

1. Neptune Streams

Track changes trong graph database:

┌─────────────────────────────────────────────────────────────────────────────┐
│                        NEPTUNE STREAMS                                      │
├─────────────────────────────────────────────────────────────────────────────┤
│                                                                             │
│   Neptune DB ──► Stream ──► Lambda ──► Downstream processing                │
│                                                                             │
│   Use cases:                                                                │
│   • Real-time notifications khi data thay đổi                               │
│   • Sync data to other systems (Elasticsearch, etc.)                        │
│   • Audit trail of all changes                                              │
│                                                                             │
└─────────────────────────────────────────────────────────────────────────────┘

2. Neptune Notebooks

Jupyter notebooks để explore và visualize graph data.

3. Neptune Workbench

Web-based tool để query và visualize graphs.


Neptune Serverless

Neptune Serverless tự động scale capacity dựa trên workload:

┌─────────────────────────────────────────────────────────────────────────────┐
│                       NEPTUNE SERVERLESS                                    │
├─────────────────────────────────────────────────────────────────────────────┤
│                                                                             │
│   Capacity                                                                  │
│       ▲                                                                     │
│       │     ____                                                            │
│       │    /    \          Auto-scales up khi load tăng                     │
│       │   /      \    /\                                                    │
│       │  /        \  /  \                                                   │
│       │ /          \/    \____                                              │
│       └──────────────────────────► Time                                     │
│                                                                             │
│   Benefits:                                                                 │
│   • Pay only for what you use                                               │
│   • No capacity planning needed                                             │
│   • Scales to zero when idle (cost savings)                                 │
│   • Instant scaling                                                         │
│                                                                             │
└─────────────────────────────────────────────────────────────────────────────┘

Neptune ML

Sử dụng machine learning trên graph data:

┌─────────────────────────────────────────────────────────────────────────────┐
│                          NEPTUNE ML                                         │
├─────────────────────────────────────────────────────────────────────────────┤
│                                                                             │
│   Neptune DB ──► SageMaker ──► ML Model ──► Predictions                     │
│                                                                             │
│   Use cases:                                                                │
│   • Node classification ("User này là fraud không?")                        │
│   • Link prediction ("User này có thể kết bạn với ai?")                     │
│   • Node embeddings for ML                                                  │
│                                                                             │
│   Powered by: Graph Neural Networks (GNN)                                   │
│                                                                             │
└─────────────────────────────────────────────────────────────────────────────┘

Exam Tips

Key Points cần nhớ

┌─────────────────────────────────────────────────────────────────────────────┐
│                    EXAM TIPS - AMAZON NEPTUNE                               │
├─────────────────────────────────────────────────────────────────────────────┤
│                                                                             │
│  1. Neptune = GRAPH DATABASE                                                │
│     • Keyword: relationships, connections, graph                            │
│     • KHÔNG phải relational, KHÔNG phải NoSQL document                      │
│                                                                             │
│  2. Query Languages:                                                        │
│     • Gremlin, openCypher (Property Graph)                                  │
│     • SPARQL (RDF)                                                          │
│     • KHÔNG dùng SQL                                                        │
│                                                                             │
│  3. Use Cases:                                                              │
│     • Social networks → "friends of friends"                                │
│     • Fraud detection → "connected to fraud ring?"                          │
│     • Recommendation → "users who bought X also bought Y"                   │
│     • Knowledge graphs → semantic relationships                             │
│                                                                             │
│  4. High Availability:                                                      │
│     • Multi-AZ                                                              │
│     • 6 copies of data across 3 AZs                                         │
│     • Up to 15 read replicas                                                │
│                                                                             │
│  5. Fully Managed:                                                          │
│     • Auto backups                                                          │
│     • Encryption at rest and in transit                                     │
│     • Serverless option available                                           │
│                                                                             │
└─────────────────────────────────────────────────────────────────────────────┘

Sample Exam Questions

Câu hỏiĐáp án
"Database nào tốt nhất cho social network relationships?"Neptune
"Cần query 'friends of friends of friends'?"Neptune
"Fully managed graph database?"Neptune
"Database hỗ trợ Gremlin và SPARQL?"Neptune
"Fraud detection với complex relationships?"Neptune

Tài liệu tham khảo