SAFE-template icon indicating copy to clipboard operation
SAFE-template copied to clipboard

Support for serverless backend instead of dedicated Saturn server, while still having shared types and behavior?

Open thewoolleyman opened this issue 3 years ago • 8 comments

Hello!

New to SAFE Stack (and the dotnet ecosystem), and excited to use it for some apps!

The one thing that seems to be missing first-class support is the ability to have a serverless backend (via Azure Functions, perhaps leveraging Azure Static Web Apps) instead of a Saturn server backend. Serverless is a much cheaper architecture for many types of apps than dedicated, always-running server instance(s).

I also want to ensure that I can still take advantage of the awesome ability to share types and behavior across the client and server(less) backend.

My first inclination was to try to modify or repurpose Fable.Remoting. But, I'm still new to this whole ecosystem, and don't know know where to start, or if that's the right approach.

I've done some research, and found the following articles which seem relevant:

QUESTIONS:

  • Is first-class Serverless support on the roadmap for the SAFE Stack?
  • Any specific advice on pointers on how I could start hacking on a proof of concept for this approach?
  • Anyone want to collaborate on it? 😜

Thanks! -- Chad

thewoolleyman avatar Mar 07 '21 03:03 thewoolleyman

The one thing that seems to be missing first-class support is the ability to have a serverless backend

This is not missing, it is just not part of the default template that SAFE offers. It is important to understand that SAFE is just a bunch of libraries that can be switched around.

In case of a serverless application, you need to change the entry point of your application from default implementation that comes with the template to exposing a serverless function definition like in this application for which you will also need two libraries:

  • Microsoft.NET.Sdk.Functions
  • Microsoft.Azure.Functions.Extensions

See the Yobo repository by @Dzoukr for reference because there some azure function specifics that you also need to take care of such as host.json and making sure the runtime used is compatible etc.

My first inclination was to try to modify or repurpose Fable.Remoting. But, I'm still new to this whole ecosystem, and don't know know where to start, or if that's the right approach.

That's not how you go about it since Fable.Remoting can be integrated as either a part of the Giraffe/Saturn application (exposing a so-called HttpHandler) or as a standard AspNet core middleware, both of which can be used out of the box in a serverless application (like in the Yobo application)

Another simpler example of a serverless SAFE application is Fantomas-UI which follows a similar approach to Yobo

It is true that the documentation is missing on how to go about turning the default template into a serverless one so there is definitely room for improvement. Another option to make a dedicated serverless SAFE template

Zaid-Ajaj avatar Mar 07 '21 14:03 Zaid-Ajaj

Just to add to this - you can actually hook Giraffe (and probably Saturn) into e.g. Azure Functions, and AWS Lambda definitely has support for Giraffe as there's even a template and some docs on the AWS site about it.

We should add a receipe (or two) for this as (despite Azure Functions shortcomings) it's an increasing pattern that we're seeing.

isaacabraham avatar Mar 08 '21 09:03 isaacabraham

Thank you for the advice and links, @Zaid-Ajaj ! I will look into them.

@isaacabraham , yes, having this as a standard template recipe would be great.

thewoolleyman avatar Mar 08 '21 10:03 thewoolleyman

@isaacabraham regarding this:

despite Azure Functions shortcomings

I am aware of the general risks and concerns with using serverless vs dedicated server architectures.

But are you referring to something specific about Azure Functions? If so, what?

thewoolleyman avatar Mar 08 '21 10:03 thewoolleyman

Lack of .NET 5 support (preview - YES! preview! 4 months after .NET 5 release! - announced on Thursday) Low performance of consumption-based hosts 12 seconds startup times Endless fights with dependencies

Pricing is good(-ish), but just be ready for "no free lunch"

Dzoukr avatar Mar 08 '21 10:03 Dzoukr

@Dzoukr thank you. expressed much more accurately and succinctly than I could have done.

isaacabraham avatar Mar 08 '21 10:03 isaacabraham

Pricing is good(-ish), but just be ready for "no free lunch"

@Dzoukr Thanks for the inside info.

I'm planning to take a JAMStack-style approach, and have as much of the logic as possible live in the clients or mashed up from external APIs.

I hope to have the serverless component be mostly a lightweight implementation for backend persistence/API proxying, event sourcing, and scheduled jobs/events.

I haven't tried this on a real app before, so we'll see how it goes. ;)

thewoolleyman avatar Mar 08 '21 14:03 thewoolleyman

Don't get me wrong. I love serverless and I am actually a big fan of Azure Functions. Unfortunately, current state of funcs is way behind its potential and seems more like a hurry duck-type solution over WebJobs SDK to react somehow on AWS Lambda hype. But I am an optimist by nature and one day it will be a great service! 🤞

Dzoukr avatar Mar 08 '21 15:03 Dzoukr

My thoughts are nowadays on Azure to focus more on Container Apps going forwards personally. None the less let's leave this on as a "to-do" item for the backlog of recipes.

@thewoolleyman I know it's been two years since you opened this (!) - if it's something you still would like, could you open this onto the docs repo (https://github.com/SAFE-Stack/docs) and close this? Thanks

isaacabraham avatar Mar 03 '23 15:03 isaacabraham

Thanks for the follow-up. I'm now working on this anymore, so I'll just close the issue. Thanks!

thewoolleyman avatar Mar 10 '23 01:03 thewoolleyman