• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

REST API Tutorial

  • REST
  • JSON
  • Dark Mode
Home / FAQs / REST API – N+1 Problem

REST API – N+1 Problem

N+1 problem is mostly talked in context of ORMs. In this problem, the system needs to load N children of a parent entity where only the parent entity was requested for. By default, ORMs are configured with lazy-loading enabled, so one query issued for the parent entity causes N more queries, i.e. one each for N child entities.

This N+1 problem is often considered a significant performance bottleneck, and so shall be solved at the design level of application.

N+1 Problem in REST APIs

Though mostly directly associated, yet the N+1 problem is not specific to ORMs only. This problem can be related to the context of web APIs as well, e.g. REST APIs.

In the case of web APIs, the N+1 problem is a situation where client applications are required to call the server N+1 times to fetch one collection resource + N client resources, mostly because of collection resources not had enough information about child resources to build its user interface altogether.

For example, a REST API returning a collection of books as a resource.

<books uri="/books" size="100">
	<book uri="/books/1" id="1">
		<isbn>3434253561</isbn>
	</book>
	<book uri="/books/2" id="2">
		<isbn>3423423534</isbn>
	</book>
	<book uri="/books/3" id="3">
		<isbn>5352342344</isbn>
	</book>
	...
	...
</books>

Here /books resource return list of books with information including only it’s id and isbn. This information is not enough to build a client application UI, which will want to typically show the books name in UI rather than ISBN. It may be that they want to show other information such as author and publication year as well.

In above scenario, client application MUST make N more requests for each individual book resource at /books/{id}. So in the total client will end up invoking REST APIs N+1 times.

The above scenario is only, for example. The idea is that insufficient information in collection resources may lead to the N+1 problem in REST APIs.

How to Solve N+1 Problem

The good thing about the previously discussed problem is that we know what exactly is the issue. And this makes the solution pretty easy. Include more information in individual resources inside collection resource.

You may consult with API consumers, do market research for similar applications and their user interfaces, or simply put yourself in the client’s shoe.

Moreover, you may evolve your APIs over time as your understanding around client requirements improve. This is possible using API versioning.

Was this article helpful?

TwitterFacebookLinkedInReddit
Previous Tutorial:
REST – PUT vs POST
Next Tutorial:
‘q’ Parameter in HTTP ‘Accept’ Header

Reader Interactions

Comments

  1. jehanzeb qayyum says

    December 15, 2019 at 6:25 am

    Graphql to rescue

    Reply
  2. Jacob says

    October 24, 2019 at 8:05 am

    That’s not really the N+1 problem in ORM. See https://www.sitepoint.com/silver-bullet-n1-problem/ for an explanation The issue is that you want N children with your parent and you can efficiently fetch them in one query if you select the parent *with* the children in the same query. The N+1 problem occurs if you fetch the parent first and then each child in a separate query.

    Reply
    • Admin says

      October 24, 2019 at 5:50 pm

      Probably you are referring to the point regarding lazy loading in ORM. Lazy loading also fires separate queries. Not sure which part is confusing to you?

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Search Tutorials

Learn REST

  • What is REST?
  • REST Constraints
  • REST Resource Naming Guide

Guides

  • Caching
  • Compression
  • Content Negotiation
  • HATEOAS
  • Idempotence
  • Security Essentials
  • Versioning
  • Statelessness in REST APIs

Tech – How To

  • REST API Design Tutorial
  • Create REST APIs with JAX-RS

FAQs

  • PUT vs POST
  • N+1 Problem
  • ‘q’ Parameter

Resources

  • What is an API?
  • Comparing SOAP vs REST APIs
  • HTTP Methods
  • Richardson Maturity Model
  • HTTP Response Codes
    • 200 (OK)
    • 201 (Created)
    • 202 (Accepted)
    • 204 (No Content)
    • 301 (Moved Permanently)

Footer

References

  • The dissertation by Roy Thomas Fielding
  • Uniform Resource Identifier (URI, URL, URN) [RFC 3986]
  • Internet MediaTypes
  • Web Application Description Language (WADL)

Meta Links

  • About
  • Contact Us
  • Privacy Policy

Blogs

  • How To Do In Java

Copyright © 2020 · restfulapi.net · All Rights Reserved. | Sitemap