Increase application performance and reduce latency with Lambda@Edge

Published on October 19, 2022

Increase application performance and reduce latency with Lambda@Edge

In a time of rapid technological innovation, keeping up with the world of computing and content delivery has become increasingly resource – and infrastructure – intensive. Moreover, a fast-growing organization with an expanding global user base must be able to deliver high-speed digital services at ultralow latency.

Contentful is a content platform relied upon by its customers and partners to serve thousands of requests per second to users all over the world. The reason why Contentful is so effective is because it uses Amazon CloudFront with Lambda@Edge under the hood, achieving low latency on every region worldwide while also running custom code at the edge that enables security features and high availability.

Read on to learn more about Lambda@Edge and how it can fit into your organization’s approach to software management.

Create a free Contentful account and install AWS Lambda right now.

What is Lambda@Edge?

Lambda@Edge is one of the most widely used computing and content delivery services today, a provisional feature in Amazon CloudFront distribution services that allows you to execute your application and websites at the Amazon CloudFront or edge locations closest to your users. It also offers you the flexibility to customize and target the specific needs of your users, such as addressing peak traffic times based on a time zone or region.

CloudFront is a globally distributed network service which offers a robust and secure content delivery infrastructure capable of distributing content at high speed and low latency around the globe. This mechanism ensures that your content is swiftly delivered to your users regardless of location. 

At the core of Lambda@Edge is AWS Lambda. The Lambda service allows you to run serverless functions on demand according to predefined events that act as triggers. This enables you to deploy and make your Lambda functions available across multiple locations. Lambda also supports many programming languages, including Node.js and Python, and offers latency monitoring, error detection, and analytics tools. 

With your content widely distributed across various Amazon CloudFront locations, Lambda@Edge eliminates the need to build and manage physical infrastructures like data centers and server farms worldwide. This cost-effective provision allows you to deliver your content directly to users at a fast speed while improving the overall performance of your application.

How does Lambda@Edge work?

The operation of Lambda@Edge relies on three fundamental activities: 

  • Once you’ve built your application, AWS Lambda allows you to deploy the application. 

  • With the application running, you’ll create trigger points at Amazon CloudFront that will execute your codes on demand. 

  • Finally, you’ll initialize Lambda@Edge to serve your application at locations closest to your users.

Consider a case where you’re serving an image via a regular web server rather than CloudFront. For example, the URL http://example.com/sunsetphoto.png may be used to serve a picture named sunsetphoto.png. The image will be visible to your users when they visit the URL. However, the user may experience significant delays while waiting for the image to be rendered. 

In contrast, when using the robust delivery mechanism of Amazon CloudFront, user requests can be processed and fetched from an edge location closest to the user with the fastest delivery time and lowest latency. With AWS, the efficiency of your application is significantly increased because the number of networks a user’s request must pass through is drastically reduced. 

Four CloudFront Events can trigger a Lambda function:

  • Viewer request: When CloudFront receives a user request, it verifies if the content was already cached and readily available before sending the request to the origin. This runs before checking the Points of Presence (PoP) cache. 

  • Origin request: This is an outgoing request from CloudFront. In this case, CloudFront triggers the Lambda function before forwarding the request to the origin.

  • Origin response: This is an incoming response from the origin. It triggers the Lambda function after CloudFront has received the response.

  • Viewer response: This refers to an outgoing response that triggers the Lambda function before CloudFront forwards the response to the client.

How Lambda@Edge can help your organization

Using Lambda@Edge can eliminate the urgency to build, manage, and scale infrastructures (such as servers and data centers) to keep up with growing user demand. It provides a cost-effective, pay-as-you-go service model that allows you to run your applications at the edge while only paying for usage time, rather than the computing power or when the application is idle.

Lambda@Edge can help improve your applications’ performance and global distribution with little or no server administration required. It can also execute your code in response to Amazon CloudFront content delivery network (CDN) events. Simply deploy your application to AWS Lambda, and it’ll handle all the necessities to run and grow your application with high availability in an AWS region near your end user. The diagram below depicts a typical workflow of the AWS Lambda@Edge Process. 

Lambda@Edge

Use cases for Lambda@Edge

Lambda@Edge is extremely flexible, making it an important service for organizations looking to leverage the AWS Worldwide Network backbone and infrastructure. Lambda@Edge use cases are divided into two main categories: user experience and origin infrastructure.

Improved user experience

Improving the user experience is an essential concern in content delivery. There are many ways Lambda@Edge can help you achieve this. 

1. User prioritization

Lambda@Edge can help you control and prioritize access to your website. Premium customers are prioritized, while non-paying visitors can be temporarily redirected to an alternative page. This ensures that your content is adequately distributed on-demand based on user priority.

2. User authentication and authorization

Implementing user authentication from scratch can be difficult. Lambda@Edge authentication can be handy when validating and authorizing users visiting your website. User authentication also plays a vital role in securing user information and privacy, making it very unlikely for unauthorized requests or malicious users to reach your origin infrastructure.

3. A/B testing

Lambda@Edge enables you to test and offer different versions of your applications to users without redirecting them from the base URL, ensuring you can release updates to your website seamlessly while maintaining uninterrupted content delivery to your users. Testing also helps with checking and validating your application’s quality, compliance, and efficiency.

4. User tracking and analytics

Real-time tracking and analyzing users distributed globally can be time-intensive, but Lambda@Edge gives you clear visibility of your company’s security and growth posture. It also offers cutting-edge analytics tools to manage real-time user activity and data across different locations effectively.

5. Real-time image transformation

With users accessing your content from different devices, you need to serve different versions of your images according to device size. Image resizing and compression can be efficiently managed with Lambda@Edge. In real-time, you can serve mobile, tablet, and desktop versions of your images on demand. 

Simplify and reduce origin infrastructure

Lambda@Edge offers several capabilities to improve or replace your existing origin or simplify your origin infrastructure.

1. Search engine optimization (SEO)

SEO improves the visibility of your website. It promotes your web content and increases the chances of your website being featured by search engines. Lambda@Edge helps improve your website’s SEO by providing a fast, pre-rendered landing page hosted on AWS. 

For an in-depth walkthrough on optimizing SEO with Contentful, please refer to this guide.

2. Enhanced website security

Lambda@Edge takes website security to the next level. With strict delivery protocols, it offers concrete security assurances by adding extra security headers to every user request sent to your origin infrastructure. These security guarantees help preserve the integrity of your website and application.

3. Bot mitigation at the edge

Bots have a pattern of consuming huge valuable computing resources. Lambda@Edge plays an active role in fighting — or altogether eliminating — bot activities and malicious traffic on your website, helping insulate your origin infrastructure from damage caused by bot activities.

Bot mitigation can also significantly improve your website and application’s performance and resource management for real users.

4. Dynamic web application at the edge

Lambda@Edge allows you to dynamically build, run, and modify your application to adapt to specific attributes or conditions. An important feature of a dynamic website is its ability to respond with various views and animations depending on the user’s attributes. Dynamic websites can also offer automatic scaling with minimal administrative effort.

5. Intelligent routing across origins and data centers

Intelligent routing encompasses a variety of functions, including load balancing. Lambda@Edge allows you to balance the traffic to your website by offering dynamic request routing based on certain viewer attributes. 

For example, you can route requests to origins inside a home region based on a viewer’s location. This dynamic routing further improves the user experience of your website or application.

Benefits of Lambda@Edge

Although building and deploying your application to AWS may seem daunting, there are many benefits to using the computing services provided by AWS Lambda@Edge for your organization. We’ll discuss a few of these below.

Customized content delivery

Since your content is widely distributed across various CloudFront locations, you have the flexibility to target and customize your content according to your users’ specific needs. These customizations may include offering content in language localized to your users. 

Highly responsive applications

Lambda@Edge offers a fast response time, ultra-low latency, and highly efficient distribution mechanism. This ensures that your application maintains a fast and real-time response to user requests.

Additionally, Lambda@Edge goes beyond the offerings of regular web servers in hosting and running highly responsive applications.

Automatic scaling

Computing resources can be effectively managed with automatic scaling. Lambda@Edge automatically scales the infrastructure in response to user growth. Upward scaling accommodates an increase in traffic coming to your website or application, while downward scaling helps manage resources when user activity drops. Without Lambda@Edge, scaling can be difficult, especially when dealing with physical infrastructure.

Easy and optimized deployment

Lambda@Edge has proven efficacy in deploying latency-sensitive applications. Managing globally distributed users with different time zones during website maintenance and deployment can be challenging. Lambda@Edge optimizations reduce unnecessary system downtime and delays during content deployments. 

Wrapping up

Managing physical computing infrastructures can be extremely expensive and difficult. But with Lambda@Edge providing highly efficient tools for building, scaling, managing, and delivering digital content and applications to users, your organization can focus on what it does best — content creation. 

Lambda@Edge also allows you to deploy and serve your applications at locations closest to your users, helping you deliver content with a fast response time and ultra-low latency.

Finally, with the capabilities of Lambda@Edge, you don’t have to worry about setting up and managing physical computing assets all over the world to keep up with user demand.

Contentful is an AWS Competency Partner with an AWS-qualified software offering that empowers digital builders to leverage content structuring, orchestration, and reusability to manage the full lifecycle of content across the organization.

Subscribe for updates

Build better digital experiences with Contentful updates direct to your inbox.

Leonardo Freitas dos Santos

Leonardo Freitas

Infrastructure Engineer, Contentful

Leonardo is a Infrastructure Engineer at Contentful

Related articles

If you're feeling the limitations of traditional CMSes like WordPress and Drupal, it's time to look at the headless CMS and the composable content platform.
Guides

What are the benefits of a headless CMS versus a traditional CMS?

June 22, 2023

What is content governance? It's the big-picture management of content across an organization to ensure that all assets are on-message and high quality.
Guides

What is content governance?

June 9, 2022

Astro is a terrific open-source web framework for content-heavy websites like landing pages, blogs, and more. Let's take a tour of Astro and its features.
Guides

What is Astro? A JS framework tour

June 14, 2023

Contentful Logo 2.5 Dark

Ready to start building?

Put everything you learned into action. Create and publish your content with Contentful — no credit card required.

Get started