May 12, 2022

Announcing a new Spin release

Radu Matei Radu Matei

spin webassembly wasm

Announcing a new Spin release

A little over a month ago, we introduced Spin, a foundational piece of the Fermyon platform built on top of the WebAssembly component model for building web applications, microservices, and event-driven, function-based applications. Today, we are really excited to announce a new release of Spin, v0.2.0, which is packed with new features and improvements to the developer experience!

Since we first released Spin, it has been executing hundreds of thousands of WebAssembly instances in production powering fermyon.com and spin.fermyon.dev, made its first appearance in a conference talk demo (you have to try out Finicky Whiskers, the world’s most adorable manual load generator), and had its first external contributions from the community and public developer meeting. At Fermyon, we are really excited about the momentum of the Spin project and about all the ways we can make it easier for developers to build and deploy WebAssembly components at scale!

Let’s highlight some of the new features of Spin v0.2, starting with the new experience for creating an application.

Creating and running your first Spin application

One of our main goals at Fermyon is to build the best developer experience for server-side WebAssembly. For this release, we focused on creating and running your first Spin application from a template.

After downloading the latest version of Spin, you can use the new spin templates command to configure the default templates from the Spin GitHub repository:

$ spin templates install --git https://github.com/fermyon/spin
+--------------------------------------------------+
| Name         Description                         |
+==================================================+
| http-go      HTTP request handler using (Tiny)Go |
| http-rust    HTTP request handler using Rust     |
| redis-rust   Redis message handler using Rust    |
| ...                                              |
+--------------------------------------------------+

Now, you can create your first application from one of the templates — let’s start with a Spin HTTP application written in Go:

$ spin new http-go

This command created all the necessary files we need to build and run our first Spin HTTP component written in Go — notice that we are writing a regular Go function that uses the HTTP Request and ResponseWriter objects from the Go standard library:

import (
	"fmt"
	"net/http"

	spinhttp "github.com/fermyon/spin/sdk/go/http"
)

func init() {
	spinhttp.Handle(func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "text/plain")
		fmt.Fprintln(w, "Hello Fermyon!")
	})
}

We can now build our component with the TinyGo toolchain for WebAssembly, then start Spin — all with a single command:

$ spin build --up
Executing the build command for component spin-gopher: tinygo build -wasm-abi=generic -target=wasi -gc=leaking -no-debug -o main.wasm main.go
Successfully ran the build command for the Spin components.
Serving HTTP on address http://127.0.0.1:3000
Available Routes:
  spin-gopher: http://127.0.0.1:3000 (wildcard)

You can add more WebAssembly components to the application, written in any language that compiles to WASI.

In this release we are also previewing how to deploy Spin applications in the cloud with Hippo, the WebAssembly PaaS — you can create and deploy an application using spin new, spin build, and spin deploy. Stay tuned for more details on running Spin applications in the cloud!

Using external services

Spin executes each component invocation in its own short-lived WebAssembly instance, which means that state has to be external. In the new Spin release, we are enabling using Redis from WebAssembly components to store key/value pairs, and to publish and subscribe to messages on Redis channels.

Let’s see an example on how to write a Rust HTTP component that uses Redis:

#[spin_sdk::http_component]
fn publish(_req: Request) -> Result<Response> {
    let address = std::env::var(REDIS_ADDRESS_ENV)?;
    let channel = std::env::var(REDIS_CHANNEL_ENV)?;

    // Get the message to publish from the Redis key "mykey"
    let payload = spin_sdk::redis::get(&address, &"mykey").map_err(|_| anyhow!("Error querying Redis"))?;

    // Set the Redis key "spin-example" to value "Eureka!"
    spin_sdk::redis::set(&address, &"spin-example", &b"Eureka!"[..])
        .map_err(|_| anyhow!("Error executing Redis command"))?;

    // Publish a message to the Redis channel.
    match spin_sdk::redis::publish(&address, &channel, &payload) {
        Ok(()) => Ok(http::Response::builder().status(200).body(None)?),
        Err(_e) => internal_server_error(),
    }
}

This HTTP component demonstrates fetching a value from Redis by key, setting a key with a value, and publishing a message to a Redis channel. This component is triggered by an HTTP request, but components could also be triggered by messages on a configured Redis channel. Currently, Spin implements working with Redis as an external data source, but we are exploring multiple new ways to persist data, such as using a relational database or object storage. The extensible nature of the project allows us to add new such features to Spin, as well as to enable the community to build new integrations.

You can read all the new features and improvements of Spin in the release notes. We are already hard at work planning the new features for the next Spin release, v0.3.0 (stay tuned for features such as cron jobs and support for CloudEvents)!

Take Spin for a spin

We are really excited for developers to try Spin — head over to the Spin documentation website, and give it a spin!

Finally, we want to thank everyone who tried Spin and gave us early feedback on the project, especially all the new contributors to the Spin project! We also want to thank the WebAssembly community, and the people building Wasmtime and the WebAssembly component model.

Big shout-out for their Spin contributions to @Mossaka, @dicej, @carlsverre, @bketelsen, @dio, @0xcodeboi, @phaleth, @toVersus, @GeorgeHahn, @lianghanzhen, @danbugs!

If you are interested in Spin and other Fermyon projects, join the chat in the Fermyon Discord server, follow us on Twitter @fermyontech, and if you plan on attending WasmDay EU 2022, make sure to catch Matt and Radu’s talk about Bartholomew and Spin!


🔥 Recommended Posts


Quickstart Your Serveless Apps with Spin

Get Started