Asynchronous by default, synchronous when necessary

Tomasz Nurkiewicz | @tnurkiewicz | nurkiewicz.com

Agenda

tl;dr: distributed systems & event sourcing

THE monolith

Splitting the monolith

1. All teams will henceforth expose their data and functionality through service interfaces.
[...]
4. It doesn’t matter what technology they use. HTTP, Corba, Pubsub, custom protocols — doesn’t matter.
Jeff Bezos, CEO of Amazon, 2002

Amazon architecture circa 2009 Ebola virus

Amazon architecture circa 2009 Ebola virus

Amazon internal services architecture circa 2009

https://apigee.com/about/blog/developer/microservices-amazon

Steve Yegge (2005)


[Amazon's] code base is a disaster, with no engineering standards whatsoever except what individual teams choose to put in place [...]
plus.google.com/+RipRowan/posts/eVeouesvaVX

Steve Yegge (cont.)


- pager escalation gets way harder[...]

Steve Yegge (cont.)


- very serious quotas and throttling [...] in every single service

Steve Yegge (cont.)


- monitoring and QA are the same thing[...]

Steve Yegge (cont.)


- you won't be able to find any [service] without a service-discovery mechanism

Steve Yegge (cont.)


- debugging problems with someone else's code gets a LOT harder

Turning Java interfaces to RESTful services

Cheap and safe


						long triple(long x) {
						  return x * 3;
						}
					

Feign


						@RequestLine("GET /triple/{x}")
						long triple(@Param("x") long x);
					

						> GET /triple/13 HTTP/1.1
						> Host: my-service.us-east-1

						< HTTP/1.1 200 OK

						< 39
					

Fallacies of distributed computing

1994

#AlternativeFacts

1. The network is reliable


2. Latency is zero


tech.finn.no

3. Bandwidth is infinite


Batching, compression

4. The network is secure


SSL, OAuth

6. There is one administrator


Escalation, root cause

7. Transport cost is zero


The Internet is running in debug mode
Rüdiger Möller

Hollywood principle

...on architecture level

Synchronous architecture

Still synchronous


						Flowable
						  .zip(
						    prices.of("IBM"),
						    portfolio.fetch(LocalDate.of(25, JANUARY, 2017)),
						    risk.check("IBM", 12, -0.1, 0)
						        .filter(Result::isSecure),
						    Order::build
						  )
						  .filter(Order::isProfitable)
						  .flatMap(stock::buy)
					

Scott Bellware:

One of the golden rules of service architecture is that you cannot query data from a service.
If you allow queries, you’ve [...] violated the most fundamental tenants of building a service, which is you tell a service what to do [...]
www.hanselminutes.com/578/ruby-and-rails-in-the-real-world-with-scott-bellware (20:30)

Publish changes

Temporal coupling

Event DB

Audit, historic data


						GET /portfolio?at=25-01-2017
					

Synchronous architecture

Audit, historic data


						GET /portfolio?at=25-01-2017
					

Occasionally connected devices

Google Docs, Trello, Pocket, Evernote, Endomondo

Use case: blockchain


en.wikipedia.org/wiki/Blockchain

Use case: banking

"Banking": the hipster way

By sending thousands of simultaneous requests, the attacker was able to "move" coins from one user account to another until the sending account was overdrawn, before balances were updated
hackingdistributed.com/2014/04/06/another-one-bites-the-dust-flexcoin/

What went wrong?

No audit data

Mutability

Race conditions

Academic approach


						BEGIN;
							-- Make sure no overdraft on Alice's account

							UPDATE accounts SET balance = balance - 100.00
							    WHERE name = 'Alice';

							UPDATE accounts SET balance = balance + 100.00
							    WHERE name = 'Bob';
						COMMIT;
					
www.postgresql.org/docs/9.6/static/tutorial-transactions.html

Banking: real approach

SWIFT [...] providing a secure, reliable, and scalable network for the smooth movement of messages
www.investopedia.com/articles/personal-finance/050515/how-swift-system-works.asp

Implementation


					// ...
					MoneyTransferred(
						from = 'Alice',
						to = 'Bob',
						amount = 100.0
					)
					// ...
					

Full stack...

Atwood's Law

any application that can be written in JavaScript, will eventually be written in JavaScript
blog.codinghorror.com/the-principle-of-least-power

twitter.com/jimplush/status/561292521418928128

Use case: Redux

redux.js.org

Redux


						function counter(state = 0, action) {
						  switch (action.type) {
						    case 'INCREMENT':
						      return state + 1
						    case 'DECREMENT':
						      return state - 1
						    default:
						      return state
						    }
						}
					

Event store implementation

geteventstore.com

kafka.apache.org

eventstore.js.org

Caveats

Eventual consistency

Rebuilding costs

Snapshots

Can WWW work this way?

What's next?

Thank you!

nurkiewicz.github.io/talks/2017/async

Tomasz Nurkiewicz | @tnurkiewicz | nurkiewicz.com