f# in the cloud
DESCRIPTION
In this talk I introduced some key concepts of cloud computing and an example of how we use F#'s support for actors to implement a stateful server architecture succinctly.TRANSCRIPT
![Page 1: F# in the cloud](https://reader036.vdocuments.mx/reader036/viewer/2022062513/556f649ed8b42a9d338b465e/html5/thumbnails/1.jpg)
Image by Mike Rohde
in the Cloud and at ScaleYan Cui (@theburningmonk)F#
![Page 2: F# in the cloud](https://reader036.vdocuments.mx/reader036/viewer/2022062513/556f649ed8b42a9d338b465e/html5/thumbnails/2.jpg)
Who is Gamesys?
•Founded in 2001
•#1 in the UK
• Handle $5 Billion in turnover annually
•First company to launch real money gaming on Facebook
•Employ 1,000 globally
![Page 3: F# in the cloud](https://reader036.vdocuments.mx/reader036/viewer/2022062513/556f649ed8b42a9d338b465e/html5/thumbnails/3.jpg)
![Page 4: F# in the cloud](https://reader036.vdocuments.mx/reader036/viewer/2022062513/556f649ed8b42a9d338b465e/html5/thumbnails/4.jpg)
![Page 5: F# in the cloud](https://reader036.vdocuments.mx/reader036/viewer/2022062513/556f649ed8b42a9d338b465e/html5/thumbnails/5.jpg)
![Page 6: F# in the cloud](https://reader036.vdocuments.mx/reader036/viewer/2022062513/556f649ed8b42a9d338b465e/html5/thumbnails/6.jpg)
Running in the Cloud, at Scale
• 1 Million DAU
• 250 Million requests/day
• 7 Billion rows of analytics events/month
• 2 TB of analytics data/month
![Page 7: F# in the cloud](https://reader036.vdocuments.mx/reader036/viewer/2022062513/556f649ed8b42a9d338b465e/html5/thumbnails/7.jpg)
Running in the Cloud, at Scale
• 100% cloud hosted
• AWS for Game services
• Google App Engine for back-office services
• NoSQL databases– DynamoDB, CouchBase, Redis, Neo4j, ...
• Google BigQuery for analytics
![Page 8: F# in the cloud](https://reader036.vdocuments.mx/reader036/viewer/2022062513/556f649ed8b42a9d338b465e/html5/thumbnails/8.jpg)
What is the Cloud?
Cloud computing is an expression used to describe a variety of computing concepts that involve a large number of computers connected through a real-time communication network such as the Internet....
Such virtual servers do not physically exist and can therefore be moved around and scaled up (or down) on the fly without affecting the end user - arguably, rather like a cloud.
- Wikipedia
![Page 9: F# in the cloud](https://reader036.vdocuments.mx/reader036/viewer/2022062513/556f649ed8b42a9d338b465e/html5/thumbnails/9.jpg)
What is the Cloud?
IaaSVMs, load balancers, storage, ...
PaaSRuntime,
database, web servers, ...
SaaSVirtual desktop,
games, emails, ...
![Page 10: F# in the cloud](https://reader036.vdocuments.mx/reader036/viewer/2022062513/556f649ed8b42a9d338b465e/html5/thumbnails/10.jpg)
Why the Cloud?
• Trade capital cost with operating cost
• Lower operating cost
• Elastic scaling
• Speed and agility
• Focus on what differentiates your product
• Global infrastructure for your global audience
![Page 11: F# in the cloud](https://reader036.vdocuments.mx/reader036/viewer/2022062513/556f649ed8b42a9d338b465e/html5/thumbnails/11.jpg)
Why the Cloud - Scalability
• Scale up
• Scale out
• Scale everything!– Your architecture is as scalable as its least scalable part
• Scalability != simple choice of language/framework
![Page 12: F# in the cloud](https://reader036.vdocuments.mx/reader036/viewer/2022062513/556f649ed8b42a9d338b465e/html5/thumbnails/12.jpg)
Travel, Collect, Craft!
![Page 13: F# in the cloud](https://reader036.vdocuments.mx/reader036/viewer/2022062513/556f649ed8b42a9d338b465e/html5/thumbnails/13.jpg)
Trap Monsters
![Page 14: F# in the cloud](https://reader036.vdocuments.mx/reader036/viewer/2022062513/556f649ed8b42a9d338b465e/html5/thumbnails/14.jpg)
![Page 15: F# in the cloud](https://reader036.vdocuments.mx/reader036/viewer/2022062513/556f649ed8b42a9d338b465e/html5/thumbnails/15.jpg)
Stateful Server
Elastic Load Balancer
S3
Auto scaling Group
Server A Server B
...
EC2
CloudFront
![Page 16: F# in the cloud](https://reader036.vdocuments.mx/reader036/viewer/2022062513/556f649ed8b42a9d338b465e/html5/thumbnails/16.jpg)
Stateful Server
• Need to ensure Server affinity– All calls need to be routed to the affined server
• Need to balance load– Session lengths vary greatly– Some players are more active than others– Need to avoid hot spots
• Need to avoid players hogging a server– Need to be able to scale down!
![Page 17: F# in the cloud](https://reader036.vdocuments.mx/reader036/viewer/2022062513/556f649ed8b42a9d338b465e/html5/thumbnails/17.jpg)
Stateful Server
• Persist player state after short inactivity
• Move player to another server after persistence
![Page 18: F# in the cloud](https://reader036.vdocuments.mx/reader036/viewer/2022062513/556f649ed8b42a9d338b465e/html5/thumbnails/18.jpg)
Why Stateful Server?
• 500% efficiency increase
• 60% reduction in avg latency
• Fewer game servers
• No CouchBase cluster
• Huge saving on cost
![Page 19: F# in the cloud](https://reader036.vdocuments.mx/reader036/viewer/2022062513/556f649ed8b42a9d338b465e/html5/thumbnails/19.jpg)
The Actor Model
An actor is the fundamental unit of computation which embodies the 3 things
• Processing• Storage• Communication
that are essential to computation.
-Carl Hewitt*
* http://bit.ly/HoNHbG
![Page 20: F# in the cloud](https://reader036.vdocuments.mx/reader036/viewer/2022062513/556f649ed8b42a9d338b465e/html5/thumbnails/20.jpg)
The Actor Model
• Everything is an actor
• An actor has a mailbox
• When an actor receives a message it can:– Create new actors– Send messages to actors it has addresses for– Designate how to handle the next message it receives
![Page 21: F# in the cloud](https://reader036.vdocuments.mx/reader036/viewer/2022062513/556f649ed8b42a9d338b465e/html5/thumbnails/21.jpg)
Stateful Server
• Gatekeeper– Manages the local list of active workers– Spawns new workers
• Worker– Manages the states for a player– Optimistic locking– Persist state after period of inactivity
![Page 22: F# in the cloud](https://reader036.vdocuments.mx/reader036/viewer/2022062513/556f649ed8b42a9d338b465e/html5/thumbnails/22.jpg)
Stateful Server
Game Server
Player A
Player B
S3Worker C
Worker B
GatekeeperRe
ques
t Han
dler
s
Asynchronous
![Page 23: F# in the cloud](https://reader036.vdocuments.mx/reader036/viewer/2022062513/556f649ed8b42a9d338b465e/html5/thumbnails/23.jpg)
Stateful Server
Game Server
Player A
Player B
S3Worker C
Worker B
Gatekeeper
Worker A
Requ
est H
andl
ers
Asynchronous
ok
![Page 24: F# in the cloud](https://reader036.vdocuments.mx/reader036/viewer/2022062513/556f649ed8b42a9d338b465e/html5/thumbnails/24.jpg)
Stateful Server
Game Server
Player A
Player B
S3Worker C
Worker B
Gatekeeper
Worker A
Requ
est H
andl
ers
Asynchronous
![Page 25: F# in the cloud](https://reader036.vdocuments.mx/reader036/viewer/2022062513/556f649ed8b42a9d338b465e/html5/thumbnails/25.jpg)
Stateful Server
Game Server
Player A
Player B
S3Worker C
Gatekeeper
Worker A
Requ
est H
andl
ers
Asynchronous
![Page 26: F# in the cloud](https://reader036.vdocuments.mx/reader036/viewer/2022062513/556f649ed8b42a9d338b465e/html5/thumbnails/26.jpg)
Stateful Server
Game Server
Player A
Player B
S3Worker C
Worker A
Requ
est H
andl
ers
Gatekeeper
Asynchronous
error
![Page 27: F# in the cloud](https://reader036.vdocuments.mx/reader036/viewer/2022062513/556f649ed8b42a9d338b465e/html5/thumbnails/27.jpg)
![Page 28: F# in the cloud](https://reader036.vdocuments.mx/reader036/viewer/2022062513/556f649ed8b42a9d338b465e/html5/thumbnails/28.jpg)
MailboxProcessor<Message>
![Page 29: F# in the cloud](https://reader036.vdocuments.mx/reader036/viewer/2022062513/556f649ed8b42a9d338b465e/html5/thumbnails/29.jpg)
Async<Message option>
![Page 30: F# in the cloud](https://reader036.vdocuments.mx/reader036/viewer/2022062513/556f649ed8b42a9d338b465e/html5/thumbnails/30.jpg)
switch state
![Page 31: F# in the cloud](https://reader036.vdocuments.mx/reader036/viewer/2022062513/556f649ed8b42a9d338b465e/html5/thumbnails/31.jpg)
![Page 32: F# in the cloud](https://reader036.vdocuments.mx/reader036/viewer/2022062513/556f649ed8b42a9d338b465e/html5/thumbnails/32.jpg)
![Page 33: F# in the cloud](https://reader036.vdocuments.mx/reader036/viewer/2022062513/556f649ed8b42a9d338b465e/html5/thumbnails/33.jpg)
Why F#?
• Time to Market
• Efficiency
• Correctness
• Complexity
![Page 34: F# in the cloud](https://reader036.vdocuments.mx/reader036/viewer/2022062513/556f649ed8b42a9d338b465e/html5/thumbnails/34.jpg)
Why F#?
• Agents– No locks– Asynchronous message passing– Each actor is self-contained and easier to reason with
• Pattern matching– Clear and concise way to handle all branch conditions
![Page 35: F# in the cloud](https://reader036.vdocuments.mx/reader036/viewer/2022062513/556f649ed8b42a9d338b465e/html5/thumbnails/35.jpg)
Why F#?
• Async Workflows– Non-blocking IO– Convert synchronous code into asynchronous code with
minimal code changes– Similar to C# 5’s async-await feature, but available in F#
since 2007!
![Page 36: F# in the cloud](https://reader036.vdocuments.mx/reader036/viewer/2022062513/556f649ed8b42a9d338b465e/html5/thumbnails/36.jpg)
Thank You!
![Page 37: F# in the cloud](https://reader036.vdocuments.mx/reader036/viewer/2022062513/556f649ed8b42a9d338b465e/html5/thumbnails/37.jpg)
JackpotJoy Slotshttp://apps.facebook.com/jackpotjoyslots
Bingo Lanehttp://apps.facebook.com/bingolane
Here Be Monstershttp://apps.facebook.com/herebemonsters
Building a MMORPGhttp://bit.ly/1hjqoL8http://slidesha.re/18MD4XY
Google I/O 2013 – Here Be BigQueryhttp://bit.ly/1fHjbce