SPEAKER 1: Let's take a look at a Cloudstate Kotlin example
that uses the Lightbend Cloudstate service to run
this on the cloud.
And you can find the source code on GitHub,
jamesward, cloudstate-sample-fraud.
OK, first let's take a look at the code.
So with Cloudstate, we use protobufs
to model the messages that we're going to send
and then the RPC operations that will be performed.
So we have a transaction with a time stamp, amount, location,
and description.
And then we have a user transaction
that associates that with a Cloudstate entity key
which allows us to do the partitioning of the messages.
And that also gives us the ability
to scale this thing up and down horizontally based
on what's happening to the messages
as they're being partitioned.
OK, and then we have a way to add a user transaction and Git
transactions.
Let's take a look at the activity entity.
And this is where the logic for handling those messages
happens.
So we've got an EventSourcedEntity.
And then you can see there's a constructor
argument with the user ID which is our partitioning key.
So that's how we're able to partition this data.
In this class, we have our transactions,
which is our mutable list.
And so whenever we get transaction messages coming in,
then we're going to be able to add them to this list
so that we have our accumulated state here in this entity.
Then we have a command handler for addTransaction which
takes that userTransaction.
And then we also get a CommandContext with there.
And then we can have our logic for detecting fraud
on a transaction.
So whenever a new transaction comes in,
we're going to use the past data and the new transaction
to calculate the velocity.
And so there's obviously lots of different ways
that we would compute whether or not the transaction is
potentially fraudulent.
For this simple example, I'm just using velocity.
And if the user's velocity goes over a max velocity,
then we're going to say that there's possible fraud.
And in this case, we're just going
to put to that to the console.
But then, once we have detected the potential fraud,
we are going to emit a message which says, OK, we've done
our handling of this message.
And then that event handler will trigger.
This will go on to an event log, which is persisted in Spanner,
in Google Cloud Spanner.
OK, so that's the bulk of the entity processing.
I've got a little class that just starts up the server.
And then I've got a simulator class
that is going to simulate some transactions.
So let's go over and run this now.
I already have this up and running
on Google Cloud via the Lightbend Cloudstate service.
But let's go to start up the simulator.
And what this will do is start adding a bunch of transactions
to this transaction log.
And what we'll be able to do once this starts emitting
those messages, we'll be able to see the messages that are being
emitted here in the console.
And then we'll go over to Google Cloud Logging
to actually see if we get a potential fraud.
So here we go.
We're now feeding a bunch of transactions into that system.
We've got multiple users.
So we're doing partitioning of the data for that entity.
Now, here in the Logs Viewer in Google Cloud,
now we can see the server messages
from my entity that's running up on Google Cloud and Lightbend
Cloudstate.
So let's go try to jump to now and see.
There we go.
OK.
So it looks like we just got a possible fraud alert.
And we can see the user ID, the merchant,
the amount, when it happened, and then
the Google Map linked to where that transaction was.
And if we let this continue running,
we probably will see some more potential fraud alerts coming
in as that simulator runs.
So you can see we're feeding in tons of messages.
They're partitioned based on user ID.
So you get the ability to auto-scale the service
up and down.
And that's great for something like this,
where the amount of transactions that are coming in
is going to fluctuate throughout the day,
with probably more transactions happening in the morning
and evening, I would guess, and less at night.
And so, great.
We can scale our service automatically based on that.
There is a container image that I built and deployed
which contains my entity code.
OK, so that's the quick example of Cloudstate
running on the Lightbend Cloudstate service.