5. Requirements
for
a
Game
API
• RESTful,
probably
JSON
interface
for
stateful
data
• SPEED!!!
Users
are
fickle.
Remember
that
fetching
a
data
item
is
inherently
synchronous.
• Ability
to
Scale
True
social
games
need
state,
and
scale
Scale
means
scale
up
and
scale
down
• Efficiency
Let’s
be
honest,
most
players
are
free
players.
However,
we
need
free
players
to
get
the
scale
we
need
to
get
enough
pay
players.
7. node.js
• REST
is
native
HTTP
is
native!
JSON
is
native!
• Event
driven
programming
model
Leads
your
software
development
to
be
fast
• Scale
out
ready
Because
of
the
loose
coupling,
node
can
scale
out
as
far
as
you
have
machines
However,
this
means
it
needs
Couchbase
• Very
efficient
use
of
system
resources
Single
threaded
but
multi-‐process,
event
driven
model
ensures
good
handoff
from
compute
to
IO
8. Couchbase
• Document
database
Designed
for
JSON
• Sub-‐millisecond
latency
Gives
you
the
consistent
performance
needed
to
build
complex,
interactive
game
play
• Designed
for
Scale
Add
and
remove
nodes
as
needed
• Efficiency
Couchbase
manages
system
resources
such
as
memory
and
CPU
efficiently
12. JSON
&
Couchbase
• Native
Data
format
Special
support
for
JSON
documents
is
provided
Couchbase
recognises
JSON
as
a
Datatype
• Complex
queries
JSON
can
be
handled
by
the
View
engine
Build
indices
via
Map
/
Reduce
20. Keying
•Use
a
Unique
value
for
key
(email,
username,
sku,
isbn,
etc.)
example
u::phil
•Predictable
Keys
can
follow
Key-‐Value
pa[erns
Users
typically
can
be
done
this
way
and
are
the
most
numerous
items
23. Connecting
to
Couchbase
var _db = null;
!
var db = function (cb) {
if (_db) return cb(null, _db);
_db = new couchbase.Connection(dbConfig, function(err) {
if (err) return cb(err);
cb(null, _db);
})
}
29. Dealing
with
concurrency
• Other
users
are
ratings
beers
also,
so
we
use
a
CAS
update
we
don’t
want
to
accidentally
overwrite
another
users
rating
that
is
being
saved
at
the
same
time
as
ours
• Retry
the
operation,
if
appropriate
Also
useful
if
you
have
internal
structure
that
you
want
to
maintain
Actor
1 Actor
2
Couchbase
Server
CAS
mismatch
&
retry
Success
35. Development
vs.
Production
Views
• Development
views
index
a
subset
of
the
data.
• Publishing
a
view
builds
the
index
across
the
entire
cluster.
• Queries
on
production
views
are
scattered
to
all
cluster
members
and
results
are
gathered
and
returned
to
the
client.
37. Storage
to
Index
Couchbase Server
EP EngineRAM Cache
Disk Write Queue
Replication Queue
View Engine
Indexers
Application Server
storage ops
Replica Couchbase Cluster Machine
38. When
to
use
and
when
not
to
use
• views
operate
on
the
persisted
data
• don’t
use
for
“login”
• data
can
be
“stale”
Counts
and
alike
are
ok
to
be
stale