Top Banner
Application Caching: The Hidden Microservice Scott Mansfield (@sgmansfield) Senior Software Engineer EVCache
68

Application Caching: The Hidden Microservice (SAConf)

Jan 12, 2017

Download

Technology

Scott Mansfield
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Application Caching: The Hidden Microservice (SAConf)

Application Caching:The Hidden Microservice

Scott Mansfield (@sgmansfield)Senior Software EngineerEVCache

Page 2: Application Caching: The Hidden Microservice (SAConf)
Page 3: Application Caching: The Hidden Microservice (SAConf)
Page 4: Application Caching: The Hidden Microservice (SAConf)
Page 5: Application Caching: The Hidden Microservice (SAConf)
Page 6: Application Caching: The Hidden Microservice (SAConf)
Page 7: Application Caching: The Hidden Microservice (SAConf)
Page 8: Application Caching: The Hidden Microservice (SAConf)
Page 9: Application Caching: The Hidden Microservice (SAConf)
Page 10: Application Caching: The Hidden Microservice (SAConf)
Page 11: Application Caching: The Hidden Microservice (SAConf)
Page 12: Application Caching: The Hidden Microservice (SAConf)
Page 13: Application Caching: The Hidden Microservice (SAConf)
Page 14: Application Caching: The Hidden Microservice (SAConf)
Page 15: Application Caching: The Hidden Microservice (SAConf)
Page 16: Application Caching: The Hidden Microservice (SAConf)
Page 17: Application Caching: The Hidden Microservice (SAConf)
Page 18: Application Caching: The Hidden Microservice (SAConf)
Page 19: Application Caching: The Hidden Microservice (SAConf)

90 seconds

Page 20: Application Caching: The Hidden Microservice (SAConf)

What do caches touch?

Signing up*Logging inChoosing a profilePicking liked videosPersonalization*Loading home page*Scrolling home page*A/B testsVideo image selection

Searching*Viewing title detailsPlaying a title*Subtitle / language prefsRating a titleMy ListVideo history*UI stringsVideo production*

* multiple caches involved

Page 21: Application Caching: The Hidden Microservice (SAConf)

Home PageRequest

Page 22: Application Caching: The Hidden Microservice (SAConf)
Page 23: Application Caching: The Hidden Microservice (SAConf)

Key-Value store optimized for AWS and tuned for Netflix use cases

Ephemeral Volatile Cache

Page 24: Application Caching: The Hidden Microservice (SAConf)

What is EVCache?

Distributed, sharded, replicated key-value storeTunable in-region and global replicationBased on MemcachedResilient to failureTopology awareLinearly scalableSeamless deployments

Page 25: Application Caching: The Hidden Microservice (SAConf)

Why Optimize for AWS

Instances disappearZones failRegions become unstableNetwork is lossyCustomer requests bounce between regions

Failures happen and we test all the time

Page 26: Application Caching: The Hidden Microservice (SAConf)

EVCache Use @ Netflix Hundreds of terabytes of dataTrillions of ops / dayTens of billions of items storedTens of millions of ops / secMillions of replications / secThousands of serversHundreds of instances per clusterHundreds of microservice clientsTens of distinct clusters3 regions4 engineers

Page 27: Application Caching: The Hidden Microservice (SAConf)

Architecture

Server

Memcached

Prana (Sidecar)

Application

Client Library

Client

Eureka(Service Discovery)

Page 28: Application Caching: The Hidden Microservice (SAConf)

Architecture

us-west-2a us-west-2cus-west-2b

ClientClient Client

Page 29: Application Caching: The Hidden Microservice (SAConf)

Reading (get)

us-west-2a us-west-2cus-west-2b

Client

Primary Secondary

Page 30: Application Caching: The Hidden Microservice (SAConf)

Writing (set, delete, add, etc.)

us-west-2a us-west-2cus-west-2b

ClientClient Client

Page 31: Application Caching: The Hidden Microservice (SAConf)

Code

EVCache evCache = new EVCache.Builder().setAppName("EVCACHE_TEST").setCachePrefix("prefix").setDefaultTTL(900) // 15 minutes.build();

evCache.set("key", "value");

evCache.get("key");

evCache.delete("key");

Page 32: Application Caching: The Hidden Microservice (SAConf)

Use Case: Lookaside Cache

Application

Client Library

Client Ribbon Client

S S S S

C C C CData Flow

Page 33: Application Caching: The Hidden Microservice (SAConf)

Use Case: Transient Data Store

Application

Client Library

Client

Application

Client Library

Client

Application

Client Library

Client

Time

Page 34: Application Caching: The Hidden Microservice (SAConf)

Use Case: Primary Store

Offline / Nearline Precomputes for

Recommendations

Online Services

Offline Services

Online Application

Client Library

Client

Data Flow

Page 35: Application Caching: The Hidden Microservice (SAConf)

Online Services

Offline Services

Use Case: Versioned Primary Store

Offline / Nearline Precomputes for

Recommendations

Online Application

Client Library

Client

Data Flow

Archaius(Dynamic Properties)

Control System(Valhalla)

Page 36: Application Caching: The Hidden Microservice (SAConf)

Use Case: High Volume && High Availability

Compute & Publish on schedule

Data Flow

Application

Client Library

In-memory Remote Ribbon Client

Optional

S S S S

C C C C

Page 37: Application Caching: The Hidden Microservice (SAConf)

Pipeline of Personalization

Compute A

Compute B Compute C

Compute D

Online Services

Offline Services

Compute E

Data Flow

Online 1 Online 2

Page 38: Application Caching: The Hidden Microservice (SAConf)

Polyglot Clients

APP

Java Client

APP

Prana

Local Proxy

Memcached

APP

Memcached Proxy

HTTP

APP

HTTP Proxy

Page 39: Application Caching: The Hidden Microservice (SAConf)

Use Case: Polyglot

HTTP

Python App

HTTP Proxy

Input Go App

Prana

Local Proxy

Output

Page 40: Application Caching: The Hidden Microservice (SAConf)

Additional Features

Kafka● Global data replication● Consistency metrics

Key Iteration● Cache warming● Lost instance recovery● Backup (and restore)

Page 41: Application Caching: The Hidden Microservice (SAConf)

Additional Features (Kafka)

Global data replicationConsistency metrics

Page 42: Application Caching: The Hidden Microservice (SAConf)

Region BRegion A

APP APP

Repl Proxy

Repl Relay

1 mutate

2 send metadata

3 poll msg

5 https s

end msg

6 mutate4 get data

for set

Kafka Repl Relay Kafka

Repl Proxy

Cross-Region Replication

7 read

Page 43: Application Caching: The Hidden Microservice (SAConf)

Region A

APP

Consistency Checker

1 mutate

2 send metadata

3 poll msg Kafka

Consistency Metrics

4 pull data

Atlas(Metrics Backend)

5 report

ClientDashboards

Page 44: Application Caching: The Hidden Microservice (SAConf)

Additional Features (Key Iteration)

Cache warmingLost instance recoveryBackup (and restore)

Page 45: Application Caching: The Hidden Microservice (SAConf)

Cache Warming

Cache Warmer (Spark)

Application

Client Library

Client

Control

S3Data Flow

Metadata Flow

Control Flow

Page 46: Application Caching: The Hidden Microservice (SAConf)

Lost Instance Recovery

Cache Warmer (Spark)

Application

Client Library

Client

S3Partial Data Flow

Metadata Flow

Control Flow

Control

Zone A Zone B

Data Flow

Page 47: Application Caching: The Hidden Microservice (SAConf)

Backup (and Restore)

Cache Warmer (Spark)

Application

Client Library

Client

Control

S3Data Flow

Control Flow

Page 48: Application Caching: The Hidden Microservice (SAConf)

What is EVCache bad at?

Perfect consistency

High security

Hot keys

Very large objects

Page 49: Application Caching: The Hidden Microservice (SAConf)

Moneta

Next-generationEVCache server

Page 50: Application Caching: The Hidden Microservice (SAConf)

Moneta

Moneta: The Goddess of MemoryJuno Moneta: The Protectress of Funds for Juno

● Evolution of the EVCache server● Cost optimization● EVCache on SSD● Ongoing lower EVCache cost per stream● Takes advantage of global request patterns

Page 51: Application Caching: The Hidden Microservice (SAConf)

Old Server

● Stock Memcached and Prana (Netflix sidecar)● All data stored in RAM in Memcached● Expensive with global expansion / N+1 architecture

Memcached

Prana

external

Page 52: Application Caching: The Hidden Microservice (SAConf)

Optimization

● Global data means many copies● Access patterns are heavily region-oriented● In one region:

○ Hot data is used often○ Cold data is almost never touched

● Keep hot data in RAM, cold data on SSD● Size RAM for working set, SSD for overall dataset

Page 53: Application Caching: The Hidden Microservice (SAConf)

New Server

● Adds Rend and Mnemonic● Still looks like Memcached● Unlocks cost-efficient storage & server-side intelligence

Rend

Prana

Memcached (RAM)

Mnemonic (SSD)

external internal

Page 54: Application Caching: The Hidden Microservice (SAConf)

go get github.com/netflix/rend

Rend

Page 55: Application Caching: The Hidden Microservice (SAConf)

Rend

● High-performance Memcached proxy & server● Written in Go

○ Powerful concurrency primitives○ Productive and fast

● Manages the L1/L2 relationship● Tens of thousands of connections

Page 56: Application Caching: The Hidden Microservice (SAConf)

Rend

● Modular to allow future changes / expansion of scope○ Set of libraries and a default main()

● Manages connections, request orchestration, and communication

● Low-overhead metrics library● Multiple orchestrators● Parallel locking for data integrity

Server Loop

Request Orchestration

Backend Handlers

METRICS

Connection Management

Protocol

Page 57: Application Caching: The Hidden Microservice (SAConf)

Mnemonic

Open source soon™

Page 58: Application Caching: The Hidden Microservice (SAConf)

Mnemonic

● Manages data storage on SSD● Reuses Rend server libraries● Maps Memcached ops to RocksDB ops

Rend Server Core Lib (Go)

Mnemonic Op Handler (Go)

Mnemonic Core (C++)

RocksDB (C++)

Page 59: Application Caching: The Hidden Microservice (SAConf)

Why RocksDB?

● Fast at medium to high write load○ Goal was 99% read latency below 20ms

● Log-Structured Merge minimizes random writes to SSD○ Writes are buffered in memory

● Immutable Static Sorted Tables

memtable

Record A Record B

SST

memtable memtable

SST SST . . .

Page 60: Application Caching: The Hidden Microservice (SAConf)

How we use RocksDB

● Records sharded across many RocksDBs per instance○ Reduces number of files checked, decreasing latency

● FIFO "Compaction"○ More suitable for our precompute use cases

● Bloom filters and indices pinned in memory○ Trade L1 space for faster L2

R...

Mnemonic Core

Key: ABCKey: XYZ

R R R

Page 61: Application Caching: The Hidden Microservice (SAConf)

FIFO Limitation

● FIFO compaction not suitable for all use cases○ Very frequently updated records may push out valid records

● Future: custom compaction or level compaction

SST

Record A2

Record B1

Record B2

Record A3

Record A1

Record A2

Record B1

Record B2

Record A3

Record A1

Record B3Record B3

Record C

Record D

Record E

Record F

Record G

Record H

time

SSTSST

Page 62: Application Caching: The Hidden Microservice (SAConf)

Moneta in Production

● Serving all of our personalization data● Rend runs with two ports:

○ One for standard users (read heavy or active data management)○ Another for async and batch users: Replication and Precompute

● Maintains working set in RAM● Optimized for precomputes

○ Smartly replaces data in L1

external internal

Prana

Memcached (RAM)

Mnemonic (SSD)

Std

Batch

Page 63: Application Caching: The Hidden Microservice (SAConf)

Get: 229 μs (139 μs) avgPercentiles:● 50th: 140 μs (113 μs)● 75th: 199 μs (139 μs)● 90th: 318 μs (195 μs)● 95th: 486 μs (231 μs)● 99th: 1.67 ms (508 μs)● 99.9th: 9.68 ms (1.97 ms)

Moneta Performance in Production

Set: 367 μs (200 μs) avgPercentiles:● 50th: 227 μs (172 μs)● 75th: 301 μs (202 μs)● 90th: 411 μs (243 μs)● 95th: 579 μs (295 μs)● 99th: 3.25 ms (502 μs)● 99.9th: 18.5 ms (4.73 ms)

Latencies: peak (trough)

Page 64: Application Caching: The Hidden Microservice (SAConf)

70%Reduction in cost*

70%

Page 66: Application Caching: The Hidden Microservice (SAConf)

Thank You

@[email protected]

Page 67: Application Caching: The Hidden Microservice (SAConf)
Page 68: Application Caching: The Hidden Microservice (SAConf)

Failure Resilience in Client

● Operation Fast Failure● Tunable Retries● Operation Queues● Tunable Latch for Mutations● Async Replication through Kafka