English edition updated

When should a Next.js app run at the edge? A look at IGA Pages

  • #Next.js
  • #Edge
  • #IGA Pages
  • #Architecture

In addition to being deployed on function services, Next.js applications can also be run on edge nodes. IGA Pages is a product of Volcano Engine, and it belongs to the same category of edge acceleration products as Alibaba Cloud ESA: they both combine site acceleration capabilities with some edge running capabilities, allowing applications to process requests on nodes closer to the user.

However, edge deployment is not the best choice for all Next.js applications. The real questions to consider are: how much computation is being performed on the server side, how many database accesses are required, and whether these data services are located close to edge nodes.

1. What are the differences between IGA Pages and Function Services?

IGA Pages and Alibaba Cloud ESA can be understood as products in the same category, but they are not the same product. Their runtime support, deployment methods, and configuration limitations cannot be directly compared. In actual use, you should still refer to their respective official documentation.

Function services typically run applications in a central region, and user requests are accelerated to this region via DCDN. Its advantage is that application code and databases can be located in close proximity, resulting in shorter paths for server-side database access.

IGA Pages operates more like an edge computing environment: application code or server-side logic can be deployed to edge nodes, giving user requests the opportunity to be processed on nodes closer to them. It inherently relies on edge acceleration products for access acceleration, rather than being a regular origin server requiring an additional layer of DCDN.

The core difference between the two methods can be simply summarized as follows:

  • Function service + DCDN: Applications run centrally, and DCDN is responsible for accelerating user requests and static resources to the application's local area.
  • IGA Pages: A portion of the application's runtime capabilities are offloaded to edge nodes, allowing user requests to be processed closer to the user.

"Closer to the user" here does not mean "closer to the database." The benefits brought by edge nodes must ultimately be evaluated together with the cost of server-side data access.

Second, first distinguish between SSR and server-side API.

These two words often appear together, but they do not describe the same thing.

**SSR (Server-Side Rendering)** describes a page generation method: when a user requests a page, the server generates the HTML and then returns it to the browser. During the SSR process, the server can read from the database, call external services, or it can completely avoid accessing the database.

A server-side API describes the interface structure: when a browser or other service requests an interface, the server typically returns JSON or other data. A common implementation in Next.js is the Route Handler. It's primarily used for data reading and writing, authentication, and calling third-party services, but it's not equivalent to page rendering.

A Next.js application can have both an SSR page and a server-side API, but their request paths and responsibilities should be understood separately:

  • SSR: Server-side generated HTML page.
  • Server-side API: The server responds with data or performs operations.
  • Server-side components directly read from the database: This is server-side data access and is not automatically equivalent to an external API.

Therefore, not all server-side logic should be called SSR, nor should all code running on the Next.js server be called API.

III. Why is database location important?

If the page is purely static, the closer the edge node is to the user, the greater its advantage. However, if the page uses SSR or the server-side API needs to access the database, the situation becomes more complex: the edge node still needs to access the database and other backend services when processing requests.

Assuming the database is still located in mainland China, but a user's request is assigned to an overseas edge node, an SSR request might follow this path:

User → Overseas Edge Node → Mainland Database → Overseas Edge Node → User

At this point, while the code is closer to the user, the server-side database access becomes more remote. Each page rendering requires cross-regional requests, and latency may offset the benefits of edge deployment. Database connectivity, network stability, and cross-regional traffic costs also need to be considered.

However, it's inaccurate to assume that "anything using a database is unsuitable for edge nodes." The key is the ratio of computation to data access:

  • Computationally intensive with few database queries: This type of server-side logic is typically best placed on edge nodes. Requests can perform most of the computation closer to the user, with only a small amount of data reading or validation.
  • Database queries are very intensive: This type of logic is generally not suitable for direct placement on edge nodes, especially when there are serial dependencies between queries, frequent read/write operations, or when the database is concentrated in a single region. A large number of round trips between edge nodes and the database can make network latency a major bottleneck.

This involves considering more than just the "number of queries," including whether each query must wait for the previous result, the data volume, the read/write ratio, and whether the database is located near an edge node. Edge nodes address the distance from the user to the compute node, but they cannot automatically address the distance from the compute node to the database.

Therefore, adding another layer of acceleration to database services (such as Supabase) is usually not a priority. Database requests involve authentication, read/write, connectivity, and consistency issues; it's not as simple as putting a static file on a CDN. A more relevant question is: should databases be deployed overseas in the first place? Should domestic and overseas applications, servers, and databases be completely separated into two separate systems?

If users, computing power, and data are primarily located overseas, deploying the database directly overseas is generally more reasonable than having domestic servers or edge nodes access the database across regions. For applications that serve both domestic and overseas markets, it's also worth considering deploying servers and databases separately domestically and overseas, allowing requests from both sides to remain locally as much as possible, reducing cross-border calls and data flow. This geographically split architecture usually makes data isolation and compliance management easier, but the specific solution still needs to be evaluated individually based on business type, data classification, and applicable regulations.

The following is a relatively complex typical scenario to illustrate this data flow. Instead of treating Supabase as a regular origin server and handing it over to DCDN for acceleration, it first splits the access layer, Next.js server, and database according to the user's region, so that the SSR page rendering and the data requests of the server API are completed in the same region as much as possible.

Complex data flow under edge deployment

IV. Which applications are suitable for deployment on edge nodes?

Pure static application

This is the most suitable scenario. The page can be distributed directly after it is built, without needing to access the database in real time on the edge nodes. Users can obtain static files from nearby nodes.

Computationally intensive server logic with low data access

If the request mainly involves computation rather than repeated database queries, such as request rewriting, lightweight authentication, rule calculation, content processing, or data preprocessing, and only requires a small amount of data reading, then placing this type of logic on an edge node is usually more appropriate.

Applications with separated front-end and back-end

If the front-end only displays the page, and the server-side API is centrally deployed in an area close to the database, then the front-end can be placed on an edge node, and data requests can be handled by the server-side API.

This approach requires separate consideration of cross-domain issues, authentication, API latency, and caching strategies, but the overall architecture has clearer boundaries. It's important to note that deploying the frontend on edge nodes does not mean the server-side API must also be deployed on edge nodes.

Applications that match databases and edge operating regions

If the database itself has a suitable global deployment plan, or if the application's users, computing nodes, and data are concentrated in similar regions, edge SSR or edge server APIs have a better chance of demonstrating their advantages.

V. Which applications are not suitable for direct placement at the edge?

If an application has a large number of dynamic pages, and SSR needs to frequently access a database that is far from the edge node, or if a single request to the server-side API requires multiple serial database queries, then it is not recommended to migrate directly to the edge node for the sake of "global acceleration".

For these types of applications, it's more suitable to first deploy the server-side logic in a function service closer to the database, and then use DCDN to accelerate static resources and user access. While this doesn't mean the entire code runs at the edge, the data access path is more controllable.

Special attention should be paid to the following deployment method:

The server is deployed on an overseas edge node, while the database is deployed in mainland China; each request requires multiple reads and writes to the database.

In this scenario, edge nodes, being close to users but far from the database, may actually result in worse overall response times. For database-intensive applications, priority should be given to ensuring low latency between the server and the database before considering offloading static and cacheable content to DCDN. If the business is primarily located overseas, it's advisable to prioritize deploying the database directly overseas rather than attempting to add an acceleration layer to the existing database connection.

VI. IGA Pages cannot have a DCDN layer superimposed on them.

Edge acceleration products typically rely on DCDN to provide global or regional acceleration capabilities. IGA Pages already belongs to this category of products, so it should not be paired with another layer of DCDN.

If you configure the domain name of an edge acceleration product as an acceleration object or origin server of another DCDN, a loop may form between the two acceleration layers. The platform will perform loop detection, and will usually block access or refuse to take effect after detecting such a configuration.

Therefore, it is necessary to first distinguish between the two architectures:

  • Function service as origin server + DCDN: This is a common architecture of "central area computing, edge accelerated access".
  • IGA Pages: It already includes edge acceleration capabilities, so there's no need to add DCDN on top of it.

If fine-grained control over DCDN caching, routing, and transmission parameters is required, it is generally advisable to choose a function service plus a standalone DCDN, rather than treating IGA Pages as a regular origin server and connecting it to another layer of DCDN.

VII. Configuration Limitations of IGA Pages

The advantage of IGA Pages is that it integrates some deployment and acceleration capabilities, but at the cost of relatively few adjustable parameters.

For example, capabilities like HTTP/2 and Gzip may not be as freely adjustable by the user as a standalone DCDN configuration; some configurations may require activation via a support ticket. If you need fine-grained control over caching, routing, and transport parameters, using a standalone function service combined with DCDN will offer greater flexibility.

Conclusion: First examine the computation and data path, then consider the node distances.

IGA Pages is suitable for purely static websites, server-side logic with computationally intensive operations and low database access, front-end applications with front-end and back-end separation, and applications where computing nodes and data services can be deployed locally.

If your Next.js application uses SSR, or has a server-side API, and the server frequently accesses a database in a fixed region, edge nodes may not be faster. In this case, you should prioritize ensuring low latency between the server and the database before using DCDN to accelerate user access.

To determine whether to use IGA Pages, ask yourself these four questions:

  1. Is this purely static content, or does it require SSR or a server-side API?
  2. Is the server-side logic primarily computational, or mainly database querying?
  3. Is the database located near the edge region where the application will run?
  4. Have edge acceleration products already been used to avoid creating a loop by adding another layer of DCDN?

If the server-side computation is heavy, database access is infrequent, and there are no significant cross-regional data access issues, edge deployment is worth prioritizing. Conversely, if the database queries are intensive, the call chain is long, or the database is far from the edge nodes, edge deployment should be chosen with caution.