Hyacinth
Hyacinth

Reputation: 87

Very slow Spring Boot application with Mongodb

I have a basic standalone Spring Boot application with MongoDB. However, it is very slow while retrieving data from the database. For example, it takes 2.5 seconds to retrieve five documents (all) from a collection of products, as shown below (entity class), without getters and setters.

@Document(collection = "products")
public class Product {
    private double itemPrice;
    private int quantity;
    @Indexed(unique = true)
    private String name;
    @Id
    private String productId;
    @DBRef
    private Set<ProductTransaction> productTransactions;
}

The repository class is shown below:

@Repository
public interface ProductRepository extends MongoRepository<Product, String> {

}

The service class is shown below:

@Service
public class ProductServiceImpl implements ProductService{
   @Autowired
   ProductRepository productRepository;

   public List<Product> getProducts() {
      startTime = System.currentTimeMillis();
      Iterable<Product> products = productRepository.findAll();
      endTime = System.currentTimeMillis();
      System.out.println("Find all products: " + (endTime - startTime));
      return products;
   }
}

The application.properties file:

spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=productDB

spring.data.mongodb.auto-index-creation=false

The pom.xml is shown below:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.3.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.spring.mongodb.ims</groupId>
    <artifactId>ims-desktop-application</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>ims-desktop-application</name>
    <description>This is an app for managing sales and inventories</description>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.modelmapper</groupId>
            <artifactId>modelmapper</artifactId>
            <version>2.3.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Please note that the same query is very fast when executed in the mongo shell

db.products.find()

I will definitely appreciate any assistance to unravel why the queries from the spring boot app are very slow.

Upvotes: 0

Views: 3261

Answers (1)

Mark Bramnik
Mark Bramnik

Reputation: 42461

Its hard to tell what exactly happens based on this descriptions, so I'll try to provide some general ideas that can lead to the solution

  1. Make sure that spring boot application does not create other queries, and also make sure that the actual query to mongo that spring boot generates indeed the one that you're running from the CLI. According to this thread you should set the log level for org.springframework.data.mongodb to DEBUG

  2. Make sure that the network is not the issue here, maybe you're running the CLI together with the mongo server(on the same host) and its really fast, whereas your spring boot application is hosted on the server that has network issues. So create a minimal reproducable example (without spring boot, just plain "main" that runs a simple query to the mongo db and check how fast it is when run from the server that host the spring boot application). Alternatively you can copy the CLI program or use some ui tool, style Robomongo.

  3. Check the replication/sharding parameters that you send to mongo, for example maybe from the application you run the query to all the shards and mongo will have to wait for the slowest shard to retrieve the information before it can combine the results from all the shards to give you the "result" answer.

  4. Profile - if you see that spring boot application is the reason of slowness - you'll have to profile and see where it gets stuck.

Upvotes: 1

Related Questions