Thursday, September 28, 2017

Serverless Event Driven Blockchain Applications With BigchainDB And Azure

Serverless Computing    is  the  new  design  pattern  that  is emerging  when  creating  business applications.  It  does  not  mean  that ,  there  are  no  Servers  in executing  the functionality. It is more of ,  the complexities  about  managing  the  servers, compute capacities are hidden from the application itself  and  the  application  only  concentrate  about  its  functionality.

Serverless  architectures  are  more closely aligned  with  Event  driven  Computing,  where  the applications  are  viewed  as  a  set of  small  functions  which  execute  based  on  the  triggering events.  This model is different  from  traditional  procedural  and  monolithic  application  architectures. 

There  are  lot  to  be  read  about  Event  Driven  and  Serverless  architectures in the context  of agile applications  which  follow  Microservices  design  pattern.

Azure Functions :  Azure Functions is an event-driven compute experience which allows you to execute your code, written in the programming language of your choice, without worrying about servers. Benefit from scale on demand and never pay for idle capacity.

BigchainDB  :    In my  earlier  articles  we  have  seen  the introduction  of BigchainDB as  one  of  the  Scalable  Blockchain  databases.  With Bigchain DB  we  can  build  De-centralized  applications whose  transactions  are  immutable.With its  current  schema  model,  BigchainDB is a  good choice for  Asset Registrations, Ownership and  transfers. Typically  BigchainDB  will work in  tandem with Smart Contract  blockchain  platforms  like Ethereum.

One  of  the  important  aspects  of  BigchainDB  is  its  support  for Web Sockets. BigchainDB provides real-time event streams over the WebSocket protocol with the Event Stream API. Connecting to an event stream from your application enables a BigchainDB node to notify you as events occur. 

With BigchainDB  ability  to  store  assets  and  considering  the fact  that  transactions  can be notified  to  client applications  using  Web Socket  we could  think  of  many  real  life applications which are event driven and  use the serverless architectures.

The  below  the  architecture  of   Serverless  Event  Driven application  using  BigchainDB web socket interface.


The  following  are  the   brief  description  of  components  involved in this  architecture.

BigchainDB  Web Socket Event Stream API :   BigchainDB  provides a stream for all validated transactions. Each stream is meant as a unidirectional communication channel, where the BigchainDB node is the only party sending messages. The  following  API will show the  end  points for connecting  the  Bigchain DB Event Streams.

{
"assets": "/assets/",
"docs": "https://docs.bigchaindb.com/projects/server/en/v1.0.1/http-client-server-api.html",
"outputs": "/outputs/",
"statuses": "/statuses/",
"streams": "ws://localhost:9985/api/v1/streams/valid_transactions",
"transactions": "/transactions/"
}

Azure Web Jobs :   WebJobs is a feature of Azure App Service that enables you to run a program or script in the same context as a web app, API app, or mobile app.  Web Jobs are typically used for  creating and running back ground  jobs. Webjobs  can  be  continuous  or  triggered  jobs. Web  Jobs in continuous  mode  can  play the role  of  Web Socket  client  so that  it  received  events  from  BigchainDB  for  process  them.  The following  is  a  simple  code  that  listens  on  a  Event Stream and  logs  the  output.


            using (var ws = new WebSocket("ws://****.westus.cloudapp.azure.com:9985/api/v1/streams/valid_transactions"))
            {
             
                ws.OnMessage += (sender, e) =>
             
                    Console.WriteLine("Event Data: " + e.Data);
                };
                 
                ws.Connect();
             
             
            }

Azure Functions  :     As  mentioned  earlier,  Azure functions  is the  event  driven  and  serverless component  of the  architecture. Use Azure Functions to run a script or piece of code in response to a variety of events.  Azure Functions  has  multiple  kind of triggers  which makes  the underlying process  to  run.

Generic webhook  is  one of the triggers which  Process webhook HTTP requests from any service that supports webhooks

Now  if  we  look into  above  architecture  from  the  following  scenario.

  • Bigchain DB  stores  the  Car  Leasing   data  for  a  major  car leasing  company
  • The  initial  Car  Asset  is  created with the ownership of  car  leasing  company
  • Every  time  the  car  is leased  out  a  new   TRANSFER  transaction is created in BigchainDB
  • BigchainDB  event   stream  web socket  trigger notified  the Web Jobs 
  • The  Web Jobs  which  is  created  and  owned  by  an insurer  will be notified  of  car leasing ownership changes
  • The  Web Jobs  triggers a  serverless API  call  using  Azure  functions
  • The  data  gets  stored  at its  destination.
The  insurance  company  on  getting notified  of  changes  in   Driver,  can  adjust the  insurance  premium  according  driver's  past history.

This is a  simplistic  example  of  a architecture  that  uses  Event  driven  and  Serverless  architecture from the perspective of  BigchainDB  and  Azure.

Currently I  face  a  few  errors  with Web Socket  implementation  but  they  will  get solved.  Write to me  if you  are  interested  in knowing  how  Event  Driven  Architectures  will be of  use in a Blockchain  application.

Also  current  Azure  functions  does   not  support  Websocket  directly and  hence  we  need to introduce  Web Jobs    as a  intermediate  component,  however this can be avoided  if Azure  functions support  Web Sockets  as one of the trigger.

10 comments:

  1. It was really a nice post and i was really impressed by reading this
    Azure Online Course


    ReplyDelete
  2. Thank you for sharing this blog !!!
    Required Safe driver Dubai? Call +971 52 894 8931 to hire a driver in Dubai.

    Are you not in a position to drive your car? Hiring a safe driver Dubai is the best thing to do if you are in Dubai and Awesome Drive is the best safe Driver Company. Awesome Drive welcomes you to a chauffeur-driven service. It is a driver company operating in Dubai.

    ReplyDelete
  3. Thank you for sharing this blog !!!
    Are you for in a situation where too exhausted to drive back to home like returning home from a party, long work schedule? Safe driver Dubai is ready to help you…


    ReplyDelete
  4. Do you Need a
    safe driver dubai
    ? enjoy back seats and lets book the safedriveruae as best safe driver service in dubai and enjoy your ride with sober and profession drivers

    ReplyDelete
  5. great experience with safe driver dubai.https://safedriverdubai.net/service/personal-safe-driver-service/

    ReplyDelete