pinterest arch summit august 2012 - scaling pinterest

46

Upload: drewz-lin

Post on 17-May-2015

1.410 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Pinterest arch summit   august 2012 - scaling pinterest

大会官方网站与资料下载地址:www.archsum m it.com

感谢您参加本次ArchSummit全球架构师峰会!

Page 2: Pinterest arch summit   august 2012 - scaling pinterest

Scaling

Marty WeinerKrypton

Evrhet MilamBatcave

12年8月10⽇日星期五TODO:Title page namesPass on page title consistencyFill out numbersPut "always test in production" pin in screenshot of website

Page 3: Pinterest arch summit   august 2012 - scaling pinterest

Pinterest is . . .An online pinboard to organize

andshare what inspires you.

Scaling Pinterest

12年8月10⽇日星期五

Talking points should be presented with any key phrases in bold, and everything else regular weight. All text should always be centered.

Page 4: Pinterest arch summit   august 2012 - scaling pinterest

12年8月10⽇日星期五

Talking points should be presented with any key phrases in bold, and everything else regular weight. All text should always be centered.

Page 5: Pinterest arch summit   august 2012 - scaling pinterest

12年8月10⽇日星期五

Images should be full-bleed when possible. Captions should be succinct and appear in bold in the bottom right corner. If necessary, you can make this white to make it legible (like this one).

Page 6: Pinterest arch summit   august 2012 - scaling pinterest

12年8月10⽇日星期五

Talking points should be presented with any key phrases in bold, and everything else regular weight. All text should always be centered.

Page 7: Pinterest arch summit   august 2012 - scaling pinterest

Relationships

Scaling Pinterest

Marty WeinerGrayskull, Eternia

Yashh NelapatiGotham City

12年8月10⽇日星期五

If you have to use a list, do this. But remember, the less stuff on a slide, the better

Page 8: Pinterest arch summit   august 2012 - scaling pinterest

Mar 2010 Jan 2011 Jan 2012

Scaling PinterestScaling Pinterest

· RackSpace

· 1 small Web Engine

· 1 small MySQL DB

· 1 Engineer

Page Views / Day

Mar 2010 Jan 2011 Jan 2012 May 2012

12年8月10⽇日星期五

4-5 mts.

Page 9: Pinterest arch summit   august 2012 - scaling pinterest

Scaling Pinterest

Page Views / Day

Mar 2010 Jan 2011 Jan 2012 May 2012

· Amazon EC2 + S3 + CloudFront

· 1 NGinX, 4 Web Engines

· 1 MySQL DB + 1 Read Slave

· 1 Task Queue + 2 Task Processors

· 1 MongoDB

· 2 Engineers

12年8月10⽇日星期五

TODO: Show total somewhere

Page 10: Pinterest arch summit   august 2012 - scaling pinterest

Mar 2010 Jan 2011 Jan 2012

Scaling PinterestScaling Pinterest

Page Views / Day

Mar 2010 Jan 2011 Jan 2012 May 2012

· Amazon EC2 + S3 + CloudFront· 2 NGinX, 16 Web Engines + 2 API Engines· 5 Functionally Sharded MySQL DB + 9 read slaves· 4 Cassandra Nodes· 15 Membase Nodes (3 separate clusters)· 8 Memcache Nodes· 10 Redis Nodes· 3 Task Routers + 4 Task Processors· 4 Elastic Search Nodes· 3 Mongo Clusters· 3 Engineers

12年8月10⽇日星期五

Page 11: Pinterest arch summit   august 2012 - scaling pinterest

Lesson Learned #1It will fail. Keep it simple.

Scaling Pinterest

12年8月10⽇日星期五

Talking points should be presented with any key phrases in bold, and everything else regular weight. All text should always be centered.

Page 12: Pinterest arch summit   august 2012 - scaling pinterest

Page Views / Day

Scaling Pinterest

Mar 2010 Jan 2011 Jan 2012 May 2012

· Amazon EC2 + S3 + Akamai, ELB

· 90 Web Engines + 50 API Engines

· 66 MySQL DBs (m1.xlarge) + 1 slave each

· 59 Redis Instances

· 51 Memcache Instances

· 1 Redis Task Manager + 25 Task Processors

· Sharded Solr

· 6 Engineers

12年8月10⽇日星期五

Page 13: Pinterest arch summit   august 2012 - scaling pinterest

Page Views / Day

Scaling Pinterest

Mar 2010 Jan 2011 Jan 2012 May 2012

· Amazon EC2 + S3 + Edge Cast, ELB

· 135 Web Engines + 75 API Engines

· 80 MySQL DBs (m1.xlarge) + 1 slave each

· 110 Redis Instances

· 60 Memcache Instances

· 2 Redis Task Manager + 60 Task Processors

· Sharded Solr

· 25 Engineers

12年8月10⽇日星期五

Page 14: Pinterest arch summit   august 2012 - scaling pinterest

Why Amazon EC2/S3?· Very good reliability, reporting, and support

· Very good peripherals, such as managed cache, DB, load balancing, DNS, map reduce, and more...

· New instances ready in seconds

Scaling Pinterest

· Con: Limited choice· Pro: Limited choice

12年8月10⽇日星期五

If you have to use a list, do this. But remember, the less stuff on a slide, the better

Page 15: Pinterest arch summit   august 2012 - scaling pinterest

· Extremely mature

· Well known and well liked

· Rarely catastrophic loss of data

· Response time to request rate increases linearly

· Very good software support - XtraBackup, Innotop, Maatkit

· Solid active community

· Very good support from Percona

· Free

Scaling Pinterest

Why MySQL?

12年8月10⽇日星期五

TODO: Animate in a money bag

Page 16: Pinterest arch summit   august 2012 - scaling pinterest

Why Memcache?

· Extremely mature

· Very good performance

· Well known and well liked

· Never crashes, and few failure modes

· Free

Scaling Pinterest

12年8月10⽇日星期五

If you have to use a list, do this. But remember, the less stuff on a slide, the better

Page 17: Pinterest arch summit   august 2012 - scaling pinterest

Why Redis?· Variety of convenient data structures

· Has persistence and replication

· Well known and well liked

· Consistently good performance

· Few failure modes

· Free

Scaling Pinterest

12年8月10⽇日星期五

Data structures -- list, set, sorted set, pubsub, string. All support atomic operations. All support pipelining.

Page 18: Pinterest arch summit   august 2012 - scaling pinterest

Clustering vs

Sharding

Scaling Pinterest

12年8月10⽇日星期五

clustered = automatically managed nodes with autoscaling, sharding = engineer assigns how data is distributed, splits databases, and writes code to redistribute data (if that option is supported)

Page 19: Pinterest arch summit   august 2012 - scaling pinterest

Scaling Pinterest

Clustering

Sharding

· Data distributed automatically

· Data can move

· Rebalances to distribute capacity

· Nodes communicate with each other

12年8月10⽇日星期五

clustered = automatically managed nodes with autoscaling, sharding = engineer assigns how data is distributed, splits databases, and writes code to redistribute data (if that option is supported). Clustered nodes gossip with each other to determine if rebalancing is needed

Page 20: Pinterest arch summit   august 2012 - scaling pinterest

Scaling Pinterest

Clustering

Sharding

· Data distributed manually

· Data does not move

· Split data to distribute load

· Nodes are not aware of each other

12年8月10⽇日星期五

clustered = automatically managed nodes with autoscaling, sharding = engineer assigns how data is distributed, splits databases, and writes code to redistribute data (if that option is supported). Clustered nodes gossip with each other to determine if rebalancing is needed

Page 21: Pinterest arch summit   august 2012 - scaling pinterest

Why Clustering?· Examples: Cassandra, MemBase, HBase, Riak

· Automatically scale your datastore

· Easy to set up

· Spatially distribute and colocate your data

· High availability

· Load balancing

· No single point of failure

Scaling Pinterest

12年8月10⽇日星期五

What could go wrong?

Page 22: Pinterest arch summit   august 2012 - scaling pinterest

Scaling Pinterest

What could possibly go wrong?

source: thereifixedit.com

12年8月10⽇日星期五

If you have to use a list, do this. But remember, the less stuff on a slide, the better

Page 23: Pinterest arch summit   august 2012 - scaling pinterest

Why Not Clustering?· Still fairly young

· Fundamentally complicated

· Less community support

· Fewer engineers with working knowledge

· Difficult and scary upgrade mechanisms

· And, yes, there is a single point of failure. A BIG one.

Scaling Pinterest

12年8月10⽇日星期五I lied, there is major single point of failure. The cluster management system. What if it fails? When will it fail? How will you, a non DB developer, fix it? Clustering is a pipe dream. Maybe someday it will work -- same day when there's a perfect makefile system, version control, bug management

Page 24: Pinterest arch summit   august 2012 - scaling pinterest

Cluster ManagementAlgorithm

Scaling Pinterest

Clustering Single Point of Failure

12年8月10⽇日星期五

joke (without names) about how when the cluster manager fails, load balancing may stop one night and keep you up til 6am, or the cluster manager sprays bad data to all nodes and teh CEO of the cluster company contacts you telling you your data is gone and can he buy you a pizza.

Page 25: Pinterest arch summit   august 2012 - scaling pinterest

Cluster Manager· Same complex code replicated over all nodes

· Failure modes:

· Data rebalance breaks

· Data corruption across all nodes

· Improper balancing that cannot be fixed (easily)

· Data authority failure

Scaling Pinterest

12年8月10⽇日星期五

What could go wrong?

Page 26: Pinterest arch summit   august 2012 - scaling pinterest

Lesson Learned #2Clustering is scary.

Scaling Pinterest

12年8月10⽇日星期五

Swap speakers after this slide

Page 27: Pinterest arch summit   august 2012 - scaling pinterest

Why Sharding?· Can split your databases to add more capacity

· Spatially distribute and colocate your data

· High availability

· Load balancing

· Algorithm for placing data is very simple

· ID generation is simplistic

Scaling Pinterest

12年8月10⽇日星期五

ID management is a single point of failure too, but much simpler and easy to test/debug.

Page 28: Pinterest arch summit   august 2012 - scaling pinterest

When to shard?· Sharding makes schema design harder

· Solidify site design and backend architecture

· Remove all joins and complex queries, add cache

· Functionally shard as much as possible

· Still growing? Shard.

Scaling Pinterest

12年8月10⽇日星期五

Maybe a pictograph here showing sharding early = faster transition, but unnecessary complexity. later = slower transition

Page 29: Pinterest arch summit   august 2012 - scaling pinterest

Our Transition1 DB + Foreign Keys + Joins1 DB + Denormalized + Cache

Several functionally sharded DBs + Read slaves + Cache

1 DB + Read slaves + Cache

ID sharded DBs + Backup slaves + Cache

Scaling Pinterest

12年8月10⽇日星期五

Another possible splitting point

Page 30: Pinterest arch summit   august 2012 - scaling pinterest

Watch out for...

Scaling Pinterest

· Cannot perform most JOINS

· No transaction capabilities

· Extra effort to maintain unique constraints

· Schema changes requires more planning

· Reports require running same query on all shards

12年8月10⽇日星期五

Another possible splitting point

Page 31: Pinterest arch summit   august 2012 - scaling pinterest

How we sharded

Scaling Pinterest

12年8月10⽇日星期五

Page 32: Pinterest arch summit   august 2012 - scaling pinterest

Sharded Server Topology

Initially, 8 physical servers, each with 512 DBs

Scaling Pinterest

db00001db00002

.......db00512

db00513db00514

.......db01024

db03584db03585

.......db04096

db03072db03073

.......db03583

12年8月10⽇日星期五

If you have to use a list, do this. But remember, the less stuff on a slide, the better

Page 33: Pinterest arch summit   august 2012 - scaling pinterest

High Availability

Multi Master replication

Scaling Pinterest

db00001db00002

.......db00512

db00513db00514

.......db01024

db03584db03585

.......db04096

db03072db03073

.......db03583

12年8月10⽇日星期五

If you have to use a list, do this. But remember, the less stuff on a slide, the better

Page 34: Pinterest arch summit   august 2012 - scaling pinterest

Increased load on DB?

To increase capacity, a server is replicated and the new replica becomes responsible for some DBs

Scaling Pinterest

db00001db00002

.......db00512

db00001db00002

.......db00256

db00257db00258

.......db00512

12年8月10⽇日星期五

If you have to use a list, do this. But remember, the less stuff on a slide, the better

Page 35: Pinterest arch summit   august 2012 - scaling pinterest

ID Structure

· A lookup data structure has physical server to shard ID range (cached by each app server process)

· Shard ID denotes which shard

· Type denotes object type (e.g., pins)

· Local ID denotes position in table

Shard ID Local ID

64 bits

Scaling Pinterest

Type

12年8月10⽇日星期五

If you have to use a list, do this. But remember, the less stuff on a slide, the better

Page 36: Pinterest arch summit   august 2012 - scaling pinterest

Lookup Structure

Scaling Pinterest

sharddb003a

{“sharddb001a”: ( 1, 512), “sharddb002b”: ( 513, 1024), “sharddb003a”: (1025, 1536), ... “sharddb008b”: (3585, 4096)}

DB01025 users

users

user_has_boards

boards

1 ser-data

2 ser-data

3 ser-data

12年8月10⽇日星期五

If you have to use a list, do this. But remember, the less stuff on a slide, the better

Page 37: Pinterest arch summit   august 2012 - scaling pinterest

· New users are randomly distributed across shards

· Boards, pins, etc. try to be collocated with user

· Local ID’s are assigned by auto-increment

· Enough ID space for 65536 shards, but only first 4096 opened initially. Can expand horizontally.

Scaling Pinterest

ID Structure

12年8月10⽇日星期五

If you have to use a list, do this. But remember, the less stuff on a slide, the better

Page 38: Pinterest arch summit   august 2012 - scaling pinterest

Objects and Mappings· Object tables (e.g., pin, board, user, comment)

· Local ID MySQL blob (JSON / Serialized thrift)

· Mapping tables (e.g., user has boards, pin has likes)

· Full ID Full ID (+ timestamp)

· Naming schema is noun_verb_noun

· Queries are PK or index lookups (no joins)

· Data DOES NOT MOVE

· All tables exist on all shards

· No schema changes required (index = new table)

Scaling Pinterest

12年8月10⽇日星期五

If you have to use a list, do this. But remember, the less stuff on a slide, the better

Page 39: Pinterest arch summit   august 2012 - scaling pinterest

Loading a Page· Rendering user profile

· Most of these calls will be a cache hit

· Omitting offset/limits and mapping sequence id sort

SELECT body FROM users WHERE id=<local_user_id>SELECT board_id FROM user_has_boards WHERE user_id=<user_id>SELECT body FROM boards WHERE id IN (<board_ids>)SELECT pin_id FROM board_has_pins WHERE board_id=<board_id>SELECT body FROM pins WHERE id IN (pin_ids)

Scaling Pinterest

12年8月10⽇日星期五

Page 40: Pinterest arch summit   august 2012 - scaling pinterest

Scripting· Must get old data into your shiny new shard

· 500M pins, 1.6B follower rows, etc

· Build a scripting farm

· Spawn more workers and complete the task faster

· Pyres - based on Github’s Resque queue

Scaling Pinterest

12年8月10⽇日星期五

If you have to use a list, do this. But remember, the less stuff on a slide, the better

Page 41: Pinterest arch summit   august 2012 - scaling pinterest

Future· Sharded MySQL is here to stay

· Auto-sharding on top of MySQL becoming viable

· Clustering may become hardened in 5 to 10 years

Scaling Pinterest

12年8月10⽇日星期五

If you have to use a list, do this. But remember, the less stuff on a slide, the better

Page 42: Pinterest arch summit   august 2012 - scaling pinterest

In The Works· Service Based Architecture

· Connection limits

· Isolation of functionality

· Isolation of access (security)

· Scaling the Team

Scaling Pinterest

12年8月10⽇日星期五

Connection limits + Isolation of functionality = service oriented architecture

Page 43: Pinterest arch summit   august 2012 - scaling pinterest

Lesson Learned #3Keep it fun.

Scaling Pinterest

12年8月10⽇日星期五

Swap speakers after this slide

Page 44: Pinterest arch summit   august 2012 - scaling pinterest

We are [email protected]

Scaling Pinterest

12年8月10⽇日星期五

Connection limits + Isolation of functionality = service oriented architecture

Page 45: Pinterest arch summit   august 2012 - scaling pinterest

Questions?

Scaling Pinterest

[email protected] [email protected]

[email protected]

12年8月10⽇日星期五

Talking points should be presented with any key phrases in bold, and everything else regular weight. All text should always be centered.

Page 46: Pinterest arch summit   august 2012 - scaling pinterest

杭 州 站 ·2012年 10月 25日 ~27日大会官网:www.qconhangzhou.com