Serving
Restate services can run in two ways: as an HTTP endpoint or as AWS Lambda functions.
Creating an HTTP endpoint
- Create a
RestateHttpEndpointBuilder
- Bind one or multiple services to it
- Listen on the specified port (default
9080
) for connections and requests.
- Java
- Kotlin
import dev.restate.sdk.endpoint.Endpoint;import dev.restate.sdk.http.vertx.RestateHttpServer;class MyApp {public static void main(String[] args) {RestateHttpServer.listen(Endpoint.builder().bind(new MyService()).bind(new MyVirtualObject()).bind(new MyWorkflow()).build());}}
fun main() {RestateHttpServer.listen(endpoint {bind(MyService())bind(MyVirtualObject())bind(MyWorkflow())})}
Creating a Lambda handler
- Add the dependency
dev.restate:sdk-lambda:1.2.0
. - Extend the class
BaseRestateLambdaHandler
- Override the register method
- Bind one or multiple services to the builder
- Java
- Kotlin
import dev.restate.sdk.endpoint.Endpoint;import dev.restate.sdk.lambda.BaseRestateLambdaHandler;class MyLambdaHandler extends BaseRestateLambdaHandler {@Overridepublic void register(Endpoint.Builder builder) {builder.bind(new MyService()).bind(new MyVirtualObject());}}
import dev.restate.sdk.endpoint.Endpointimport dev.restate.sdk.lambda.BaseRestateLambdaHandlerclass MyLambdaHandler : BaseRestateLambdaHandler() {override fun register(builder: Endpoint.Builder) {builder.bind(MyService()).bind(MyVirtualObject())}}
Have a look at the deployment section for guidance on how to deploy your services on AWS Lambda.
Run on Lambda without handler changes
The implementation of your services and handlers remains the same for both deployment options.
Using Java 21 Virtual Threads
If you use a JVM >= 21, you can use virtual threads to run your services:
- Java
- Kotlin
builder.bind(new Greeter(),HandlerRunner.Options.withExecutor(Executors.newVirtualThreadPerTaskExecutor()));
builder.bind(Greeter(),HandlerRunner.Options(coroutineContext = Executors.newVirtualThreadPerTaskExecutor().asCoroutineDispatcher(),),)
Validating request identity
SDKs can validate that incoming requests come from a particular Restate instance. You can find out more about request identity in the Security docs. You will need to use the request identity dependency, for example in Gradle:
implementation("dev.restate:sdk-request-identity:1.2.0")
- Java
- Kotlin
import dev.restate.sdk.auth.signing.RestateRequestIdentityVerifier;import dev.restate.sdk.endpoint.Endpoint;import dev.restate.sdk.http.vertx.RestateHttpServer;class MySecureApp {public static void main(String[] args) {var endpoint =Endpoint.builder().bind(new MyService()).withRequestIdentityVerifier(RestateRequestIdentityVerifier.fromKeys("publickeyv1_w7YHemBctH5Ck2nQRQ47iBBqhNHy4FV7t2Usbye2A6f")).build();RestateHttpServer.listen(endpoint);}}
import dev.restate.sdk.auth.signing.RestateRequestIdentityVerifierimport dev.restate.sdk.http.vertx.RestateHttpServerimport dev.restate.sdk.kotlin.endpoint.endpointfun main() {RestateHttpServer.listen(endpoint {bind(MyService())requestIdentityVerifier =RestateRequestIdentityVerifier.fromKeys("publickeyv1_w7YHemBctH5Ck2nQRQ47iBBqhNHy4FV7t2Usbye2A6f",)})}