DEV Community

Cover image for Stateful Serverless on Lambda using Upstash Redis
Mathis Van Eetvelde
Mathis Van Eetvelde

Posted on

Stateful Serverless on Lambda using Upstash Redis

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)