architecture patterns and practices

Post on 13-Jan-2015

1.440 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

ADC2013-阿里技术嘉年华上的分享

TRANSCRIPT

trait Speaker { val name:String val aliases:Set[String]}

trait Organization{ val bu = "tmall" val group = "alibaba"}

object WangFuqiang extends Speaker with Organization{ val name = "王福强"

val aliases = Set("千任", "@囚千任") val blog = “http://afoo.me”}

架构模式与实践漫谈

Sunday, July 14, 13

Architecture

Sunday, July 14, 13

Architecture Principles• Abstraction

• Modularity

• Scalability

• Robustness

• Security

• Availability

• Reusability

• name it more...

Sunday, July 14, 13

Exciting?

Sunday, July 14, 13

Don’t Be Silly!

Sunday, July 14, 13

Overload

Sunday, July 14, 13

How About Practices + Patterns

Sunday, July 14, 13

Roadmap First

Sunday, July 14, 13

Consistency

Layering

Isolation

Immutability

Clustering

Buffering

Async

Throttling单结点 多结点

Sunday, July 14, 13

Sunday, July 14, 13

So Far, So Good

Sunday, July 14, 13

Consistency

Layering

Caching

Isolation

Immutability

Clustering

Async

Throttling

Sunday, July 14, 13

What we do?

Sunday, July 14, 13

Sure, 1. Scale Up!

纵向扩展

Sunday, July 14, 13

“I try so hard, I got so far...”

‘cause hardware can’t work well without proper software

Sunday, July 14, 13

Sunday, July 14, 13

Sunday, July 14, 13

Buffering缓冲

Sunday, July 14, 13

Queuesfucking simple idea, right?

Sunday, July 14, 13

Queue Everywhere• cpu task run queue

• thread pool task queue

• actors’ mailbox

• TCP stack send buffer /receive buffer

• MOM

• etc.

Sunday, July 14, 13

Batching分批处理when they slowly move

Sunday, July 14, 13

Sunday, July 14, 13

When to batchWhen to flush

Sunday, July 14, 13

Problem With Nagle Algorithm

Sunday, July 14, 13

Latency VS.

Throughput

It’s your choice!

Sunday, July 14, 13

Smart Batchingfocus on the whole pipeline

Sunday, July 14, 13

DisruptorStart 2 use it today

Sunday, July 14, 13

--------割--------

Sunday, July 14, 13

Sunday, July 14, 13

Throttling限流|flow control

Sunday, July 14, 13

Rate ControlBackpressure

Sunday, July 14, 13

Rate Control

Sunday, July 14, 13

Semaphore

Sunday, July 14, 13

僵硬 灵活

Sunday, July 14, 13

Let’s apply

BackPressureButterfly Effect

Sunday, July 14, 13

Sunday, July 14, 13

Sunday, July 14, 13

Sunday, July 14, 13

表征

位置(反馈)方式

what

where how

BackPressure

load, rt, etc.

ack, nack, etc.hardware, os, app, etc.

Sunday, July 14, 13

--------割--------

Sunday, July 14, 13

2 + 2 = ?

Sunday, July 14, 13

Sunday, July 14, 13

Sunday, July 14, 13

Caching缓存

Sunday, July 14, 13

Why Caching?

Sunday, July 14, 13

Sunday, July 14, 13

Cache Everywhere• CPU Level1-3 cache

• Browser-side cache

• Server-side cache

• Business-layer cache

• DAL-layer cache

• Database-level cache

• Reverse Proxy

• DNS

• Name it more...

Sunday, July 14, 13

Cache Types• Local Cache

• Map

• Ehcache * (BigHeap)

• Redis

• Remote Cache

• Tair *

• Memcached

• In-Memory Data Grid - IMDG

• Coherence

• GemFire

* means Hybrid

Sunday, July 14, 13

Cache Strategy

Dimension Synchronous Asynchronous

READ Read-Through Refresh-Ahead

WRITE Write-Through Write-Behind

Sunday, July 14, 13

Caching Tricks• Dummy Value

• non-exist entities in storage

• Versioning

• long-period-cached files

K1

K2

K3

K1

K2

K3

K4 K4

K5 K5

Cache Storage

file1.v1

file1.v2index.html <=> file1

Sunday, July 14, 13

Case Study - 支付宝双11预充值

我爬...

我也爬...

我跟着爬...

我等?!

Sunday, July 14, 13

--------割--------

Sunday, July 14, 13

Async异步

Sunday, July 14, 13

give me the quote of EUR/USD

here u arebuy 2 lots

transaction result

Story Background...

Sunday, July 14, 13

Future<Double> rateQuoteFuture = executor.submit(new Callable<Double>() { @Override public Double call() throws Exception { connection.getCurrentValue(USD); } });

try { Double rateQuote = rateQuoteFuture.get(5, TimeUnit.SECONDS); // Blocking wait Future<Integer> future = executor.submit(new Callable<Object>() { @Override public Integer call() throws Exception { if(isProfitable(rateQuote)) connection.buy(amount, rateQuote) else throw new Exception("not profitable"); } }); Integer amount = future.get(5, TimeUnit.SECONDS); // Blocking wait System.out.println("Purchased "+ amount + " USD"); } catch (InterruptedException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } catch (ExecutionException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } catch (TimeoutException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. }

Go Async

Sunday, July 14, 13

The Pain In The Neckblocking wait

Sunday, July 14, 13

1. val rateQuote = future {2. connection.getCurrentValue(USD)3. }

5. rateQuote onSuccess { case quote =>6. val purchase = future {7. if (isProfitable(quote)) connection.buy(amount, quote)8. else throw new Exception("not profitable")9. }10. 11. purchase onSuccess {12. case _ => println("Purchased " + amount + " USD")13. }14.}

Go Reactive

Sunday, July 14, 13

Behind The Pretty Face blocking still exists

Sunday, July 14, 13

Task Scheduling Matters

Sunday, July 14, 13

Async & Task Schedule Granularity

•Process•Thread•Actor•Coroutine•Continuation

Sunday, July 14, 13

Go Non-Blocking

Sunday, July 14, 13

And Think In Big Picture

Sunday, July 14, 13

Sir, Yes, Sir.Sunday, July 14, 13

R All Async Good?

Sunday, July 14, 13

“Trade A For A” AsyncAnti-Pattern

Sunday, July 14, 13

A yelled ‘get it done’

B working...

A looking/waiting/smoking...

Sunday, July 14, 13

Future<Double> rateQuoteFuture = executor.submit(new Callable<Double>() { @Override public Double call() throws Exception { connection.getCurrentValue(USD); } });

Double rateQuote = rateQuoteFuture.get(5, TimeUnit.SECONDS);// Nothing more to do, just wait and return

Sunday, July 14, 13

Future<Double> rateQuoteFuture = executor.submit(new Callable<Double>() { @Override public Double call() throws Exception { connection.getCurrentValue(USD); } });

Double rateQuote = rateQuoteFuture.get(5, TimeUnit.SECONDS);// Nothing more to do, just wait and return

Why don’t U do it in current thread?

Sunday, July 14, 13

It’s All About

RESOURCE UTILITY资源利用率是关键

Sunday, July 14, 13

SEDAA grand master of buffering + async

Sunday, July 14, 13

Sunday, July 14, 13

--------割--------

Sunday, July 14, 13

Sunday, July 14, 13

Sunday, July 14, 13

Layering

Isolation

Immutability

Clustering

Buffering

Async

ThrottlingManage Dependencies

Wisely明智地管理依赖

Sunday, July 14, 13

WhereWeStand Service2

Service1

Service3

Sunday, July 14, 13

ExceptionStart Small, But Basic!

Sunday, July 14, 13

What we do after catch?retry?

ignore?throw up?

Sunday, July 14, 13

Sunday, July 14, 13

Sunday, July 14, 13

Timeout Everywhere• Thread.join(long)

• Future.get(long, TimeUnit)

• ExecutorService.awaitTermination(long, TimeUnit)

• CountDownLatch.await(long, TimeUnit)

• RPC Framework, say, HSF or Dubbo

• IO timeout

• you name it...

Sunday, July 14, 13

Circuit Breaker Pattern

I want it to be automatic!

Sunday, July 14, 13

Sunday, July 14, 13

Sunday, July 14, 13

SwitchesI don’t need you here!

开关

Sunday, July 14, 13

Sunday, July 14, 13

trait PartialFunction[-A, +B] extends (A => B) { self =>  import PartialFunction._

  /** Checks if a value is contained in the function's domain. * * @param x the value to test * @return `'''true'''`, iff `x` is in the domain of this function, `'''false'''` otherwise. */  def isDefinedAt(x: A): Boolean ...}

trait Function1[@specialized(..) -T1, @specialized(..) +R] extends AnyRef { self =>  /** Apply the body of this function to the argument. * @return the result of function application. */  def apply(v1: T1): R

http://www.infoq.com/cn/articles/function-switch-realize-better-continuous-implementations

Switch.reduce{

}Sunday, July 14, 13

Hard Enough

Sunday, July 14, 13

2. Scale Out横向扩展

Sunday, July 14, 13

We r gonna Cluster it.

Sunday, July 14, 13

Symmetric

Stateful

State

Asymmetric

Stateless

ComputationSunday, July 14, 13

Replicas 复制品, 副本

Sunday, July 14, 13

Replica means Symmetric

Sunday, July 14, 13

When It Goes Stateless

Sunday, July 14, 13

Computational Replicas

Sunday, July 14, 13

Master-Workers Pattern

Sunday, July 14, 13

Sunday, July 14, 13

Sunday, July 14, 13

Most of the time, Computation and state are so close...

Sunday, July 14, 13

State Replicas

Sunday, July 14, 13

Read ScalesRedundancy

Sunday, July 14, 13

Replication

Sunday, July 14, 13

Replication Types• Consistency Concerning Replication

• Synchronous Replication

• Asynchronous Replication

• Bandwidth Concerning Replication

• Intra-IDC Replication

• Inter-IDC Replication

• Other Views...

Sunday, July 14, 13

Load-Balancing

Sunday, July 14, 13

Sunday, July 14, 13

So short? Just Stay with me

Sunday, July 14, 13

--------割--------

Sunday, July 14, 13

Shards分片,分区aka. Partitioning

Sunday, July 14, 13

Write ScalesStorage Scales

Sunday, July 14, 13

Routing

Sunday, July 14, 13

Lookup Table

Sunday, July 14, 13

Hashing

Sunday, July 14, 13

Consistent Hashing

Sunday, July 14, 13

Sharding Strategy Matters• Capacity Planning

• Capacity Expanding

• State Transferring

• State Access Pattern

• Fault-Tolerance

• etc.

Sunday, July 14, 13

L.B. Vs. Routing

Sunday, July 14, 13

--------割--------

Sunday, July 14, 13

ShitHappens

Sunday, July 14, 13

Sunday, July 14, 13

Isolation隔离性

Sunday, July 14, 13

Bulkhead Pattern

Sunday, July 14, 13

Sunday, July 14, 13

One Tab, One Process

Sunday, July 14, 13

Linux Container

Sunday, July 14, 13

Single Box In A Cluster

Sunday, July 14, 13

A Whole Cluster Goes Bad...

Sunday, July 14, 13

So When you deploy clusters of• Search Service

• HSF/Dubbo Service

• Data Storage Service

• Caching Service(Tair, Redis, memcached...)

• Whatever that’s important or have higher priority, NO Share!

Sunday, July 14, 13

What Do U Think?

Sunday, July 14, 13

--------割--------

Sunday, July 14, 13

Sunday, July 14, 13

Redundancy冗余

Sunday, July 14, 13

Symmetric Cluster?

Sunday, July 14, 13

Symmetric Cluster?

What A Lucky Boy!

Sunday, July 14, 13

What About Asymmetric Cluster?

Sunday, July 14, 13

BinaryStar Pattern

Sunday, July 14, 13

Sunday, July 14, 13

--------割--------

Sunday, July 14, 13

Way&Style

Sunday, July 14, 13

Consistency一致性

Sunday, July 14, 13

Why Consistency?

Sunday, July 14, 13

Sunday, July 14, 13

Sunday, July 14, 13

Consistency Everywhere• Religion

• Bible

• Brotherhood initiation

• Psychology

• Gambling

• Debating

• Industry

大众就一款⻋车高尔夫,拍窄点就是菠萝,加个屁股就是新桑塔纳和新捷达,继续拍一拍就是速腾和朗逸、再捏一把就是新宝来,扩大一圈就是新帕萨特,再拍一拍就是迈腾,再加大一圈就是辉腾,拍成方的就是途安,加多三个后座就是夏朗,加高底盘就是途观,再撑大点就是途锐,拍扁就是尚酷,搓圆了就是甲壳虫。

Sunday, July 14, 13

Show Time...

Sunday, July 14, 13

统一构建原则

Sunday, July 14, 13

In Framework Design like Spring

Sunday, July 14, 13

-- Haskell is a pure functional language and even the I/O system can't break this purity.

Sunday, July 14, 13

Go Consistency When Necessary

Sunday, July 14, 13

Layering分层

Sunday, July 14, 13

Why Layering?

Sunday, July 14, 13

impact

Sunday, July 14, 13

Layer Patterns Everywhere

Can U Name More?

Sunday, July 14, 13

Cake Pattern

Sunday, July 14, 13

Stackable Traits Pattern Decorator Pattern In Scala

Sunday, July 14, 13

--------割--------

Sunday, July 14, 13

Immutability不变性

Sunday, July 14, 13

Sunday, July 14, 13

Sunday, July 14, 13

Sunday, July 14, 13

Persistent Data Structure

Sunday, July 14, 13

Familiar?

Sunday, July 14, 13

Event Sourcing

Sunday, July 14, 13

Capture all changes to an application state as a sequence of events.

Sunday, July 14, 13

Real World ES• Binlog, HLog . . .

• Kafka’s Storage Strategy

• MetaQ - Java Clone of Kafka,

• LMAX Platform

• Many home-brew tools

• Ultra Messaging Stream Edition(UMS) - Informatica

• Available Projects/Solutions

• https://github.com/eligosource/eventsourced

• https://github.com/sbtourist/Journal.IO

• https://github.com/fusesource/hawtjournalSunday, July 14, 13

案例分析•TMS编辑和发布的并发控制与潜在冲突

•模板中变量名变更的追踪无案底可查

Sunday, July 14, 13

Lambda Pattern

Sunday, July 14, 13

Does Lambda’s face inspire U?

Sunday, July 14, 13

Sunday, July 14, 13

Sunday, July 14, 13

http://www.infoq.com/presentations/High-Performance-Network-Applications-in-the-Capital-Markets

Todd-Montgomery

Sunday, July 14, 13

Sunday, July 14, 13

Log VS. JMXYour Opinion?

Sunday, July 14, 13

--------割--------

Sunday, July 14, 13

ComposabilityYes, This is a principle :0)

组合

Sunday, July 14, 13

灵活运用工具的能力比工具质量要重要的多

Sunday, July 14, 13

It’sNot The Ending, It’s The Beginning...

Sunday, July 14, 13

推荐书目

Sunday, July 14, 13

Have Fun & Happy

Sunday, July 14, 13

top related