DEV Community

loading...
Cover image for Stateful Serverless on Lambda using Upstash Redis

Stateful Serverless on Lambda using Upstash Redis

Mathis Van Eetvelde
Result driven, passionate Site Reliability / Cloud Engineer with an exceptional technical background and a keen eye for details. Experienced with Golang, ML, DevOps, Kubernetes, Docker, AWS.
・1 min read

In this video I go in depth on building a stateful serverless API using on AWS Lambda using Upstash redis. Upstash is a Redis compatible database.

Because of its very low latency it's perfect for building something like a link shortening service. Which is exactly what we'll be doing in this video!

Alt Text
Click here or on the thumbnail above to watch the video!

Here is a small code snippet from the video:

func Handler(ctx context.Context, request events.APIGatewayProxyRequest) (Response, error) {
    // extract hash from API Gateway headers
    hash := request.Headers["hash"]

    // get from Redis compatible Upstash database
    val, err := client.Get(ctx, hash).Result()

    if err == redis.Nil {
        // hash not found
        return Response{
            StatusCode: 404,
            Body:       notFoundResponse,
            Headers:    headers,
        }, nil
    }

    // did find hash
    return Response{
        StatusCode: 200,
        Body:       fmt.Sprintf(redirectResponse, val),
        Headers:    headers,
    }, nil
}
Enter fullscreen mode Exit fullscreen mode

Discussion (0)