Benchmarking Django vs Spring Boot : A comparative study.

Shreyash vardhan
CodeX
Published in
6 min readJul 16, 2024

Introduction

In this blog post, i will share the benchmarking test comparing Django and Spring Boot, two of the most popular web framework for backend development. Using Apache Bench, I tested a simple API for each framework to see how they performed under various loads. The comparison aims to provide insights into the strengths and weakness of each framework in handling API requests and database operations.

Setup and Methodology

Framework and Tools

  • Django(5.0): A high level python framework.
  • Spring Boot(3.3.1): A comprehensive java based framework.
  • Apache Bench: A tool for benchmarking HTTP server performance.

Test Environment.

  • API description: A simple API endpoint for both frameworks that writes data to a database.
  • Database: MySQL 8.0
  • Server: Macbook Air ( CPU : M1 chip , RAM: 8GB)

Test Parameters

  • Number of requests: 10,000.
  • Concurrency level: 10

Results

Response time(Time taken to complete 10,000 requests)

  • Spring boot: 6.109 seconds.
  • Django: 8.712 seconds.

Request per second

  • Spring Boot : 1636.81
  • Django : 1147.84

Time per request

  • Spring Boot : 0.611 [ms] (mean, across all concurrent requests)
  • Django : 0.871 [ms] (mean, across all concurrent requests)

Connection times [ms]

Spring Boot

+---------------+-----+-------------+--------+------+
| | min | mean[+/-sd] | median | max |
+---------------+-----+-------------+--------+------+
| Connect : | 0 | 0 [0.1] | 0 | 4 |
| Processing : | 1 | 6 [4.4] | 5 | 108 |
| Waiting : | 1 | 6 [4.4] | 5 | 108 |
| Total : | 1 | 6 [4.4] | 5 | 109 |
+---------------+-----+-------------+--------+------+

Django

+---------------+-----+-------------+--------+------+
| | min | mean[+/-sd] | median | max |
+---------------+-----+-------------+--------+------+
| Connect : | 0 | 0 [0.1] | 0 | 8 |
| Processing : | 3 | 9 [4.5] | 8 | 76 |
| Waiting : | 2 | 7 [4.1] | 7 | 62 |
| Total : | 3 | 9 [4.5] | 8 | 76 |
+---------------+-----+-------------+--------+------+

Percentage of the requests served within a certain time (ms)

Spring Boot

  • 50% : 5 ms
  • 66% : 6 ms
  • 75% : 6 ms
  • 80% : 6 ms
  • 90% : 7 ms
  • 95% : 10 ms
  • 98% : 16 ms
  • 99% : 22 ms
  • 100% : 109 ms (longest request)

Django

  • 50% : 8 ms
  • 66% : 9 ms
  • 75% : 9 ms
  • 80% : 10 ms
  • 90% : 11 ms
  • 95% : 13 ms
  • 98% : 23 ms
  • 99% : 27 ms
  • 100% : 76 ms (longest request)

Performance Analysis

The results show that Spring Boot generally performs better than Django in terms of response time and throughput. Spring Boot handled a higher number of requests per second and had lower average connection and processing times.

Key Points

  • Response time : Spring Boot is approximately 29.85% faster than Django in completing the requests.
  • Request per second : Spring Boot handled approximately 42.56% more requests per second than Django.
  • Processing times : Spring Boot’s processing time is approximately 33.33% lower than Django’s.
  • Waiting time : Spring Boot’s waiting time is approximately 14.29% lower than Django’s
  • Request percentiles : Across various percentiles, Spring Boot consistently served requests faster than Django, with differences ranging from 18.52% to 40%.

How Language difference impact performance.

Interpreted vs Compiled

  • Python (Interpreted): Python is an interpreted language, meaning the code is executed line-by-line, which can lead to slower execution times compared to compiled languages. This impacts the performance of Django applications.
  • Java (Compiled): Java is a compiled language, where the code is compiled into bytecode before execution. This typically results in faster execution times and better performance for Spring Boot applications.

Memory Management

  • Python: Python uses a garbage collector for memory management, which can introduce overhead and potentially affect performance. The garbage collector periodically reclaims memory by removing unused objects, which can cause delays.
  • Java: Java also uses a garbage collector, but it offers more sophisticated garbage collection algorithms and tuning options. This allows for more efficient memory management and can contribute to better performance in Java applications.

Syntax and Execution

  • Python: Python’s dynamic typing and concise syntax make it easy to write and maintain code, but it can also lead to runtime type checking, which slows down execution.
  • Java: Java’s static typing and verbose syntax require more code to be written, but the type checking is done at compile time, reducing the runtime overhead and improving performance.

Concurrency and Multithreading

  • Python: Python’s Global Interpreter Lock (GIL) can be a bottleneck for CPU-bound tasks, as it prevents multiple native threads from executing simultaneously in a single process. This can limit the performance of Django applications in multi-threaded scenarios.
  • Java: Java supports true multithreading at the language level, allowing multiple threads to run concurrently. This makes Java and Spring Boot well-suited for applications that require high levels of concurrency.

Just-In-Time (JIT) Compilation

  • Python: Standard Python (CPython) does not use JIT compilation. Each line of code is interpreted and executed by the Python interpreter, which can result in slower performance. However, alternatives like PyPy implement JIT to improve performance, though they are not as widely used as CPython.
  • Java: Java employs JIT compilation as part of the JVM. The JVM compiles bytecode into native machine code at runtime, optimizing frequently executed paths. This dynamic compilation can lead to significant performance improvements over time.

Virtual Machine Overhead

  • Python: Python runs on the Python Virtual Machine (PVM), which has a relatively higher overhead due to its simplicity and lack of aggressive optimizations. This can slow down Python applications, including those built with Django.
  • Java: Java runs on the JVM, which is a highly optimized virtual machine with extensive performance tuning options. The JVM’s advanced garbage collection, JIT compilation, and bytecode optimization contribute to better performance for Spring Boot applications.

Conclusion

In conclusion, this benchmarking study highlights the performance differences between Django and Spring Boot, two widely used frameworks for backend development. The results clearly show that Spring Boot outperforms Django in various aspects such as response time, requests per second, and processing times. These differences can be attributed to the underlying languages — Java for Spring Boot and Python for Django — and their respective characteristics, such as being compiled versus interpreted, memory management, and multithreading capabilities.

While Spring Boot generally offers better performance, Django’s simplicity and ease of use make it a strong contender for rapid development and prototyping. Choosing between these frameworks should depend on the specific needs and constraints of your project.

Here’s a detailed analysis to help you decide which framework to choose.

1. Performance

  • Spring Boot: Demonstrates superior performance with faster response times and higher throughput, making it suitable for high-traffic applications that require robust and efficient handling of numerous requests.
  • Django: While slightly slower, Django’s performance is still acceptable for many use cases, especially where peak performance is not the primary concern.

2. Development speed

  • Django: Python’s simplicity and Django’s extensive built-in features allow for rapid development and prototyping. This makes Django a great choice for startups and projects with tight deadlines.
  • Spring Boot: Java’s verbose syntax and the comprehensive nature of Spring Boot might slow down initial development, but it provides a strong structure and flexibility for complex applications.

3. Ease of use

  • Django: Known for its ease of use, Django’s ORM, admin interface, and out-of-the-box solutions simplify the development process, particularly for developers new to web development.
  • Spring Boot: Offers more control and customization options but comes with a steeper learning curve. It is well-suited for developers who need fine-grained control over application components.

4. Scalability and Concurrency

  • Spring Boot: Excels in scalability and handling concurrent processes due to Java’s multithreading capabilities and JVM optimizations. Ideal for applications that anticipate significant growth or require high concurrency.
  • Django: While capable of handling moderate scalability, it may encounter limitations due to Python’s Global Interpreter Lock (GIL). For highly concurrent applications, additional tools and optimizations might be necessary.

5. Community and Ecosystem

  • Django: Boasts a strong, supportive community and a plethora of third-party packages that enhance functionality, making it easier to find resources and solutions.
  • Spring Boot: Backed by a large enterprise community, Spring Boot benefits from extensive documentation, commercial support, and integration with other enterprise tools and frameworks.

Choosing between Django and Spring Boot ultimately depends on your specific project needs:

  • Choose Django if: You prioritize rapid development, ease of use, and a rich ecosystem of packages. It is especially well-suited for startups, MVPs, and projects with tight timelines.
  • Choose Spring Boot if: You need high performance, scalability, and robustness for complex applications. It is ideal for enterprise-level projects that require fine-grained control and optimization.

About the Author

Shreyash Vardhan is the Lead Backend Engineer at Janitri Innovations, specializing in backend development and system design. With extensive experience in both Django and Spring Boot, Shreyash has a deep understanding of performance benchmarking and optimization. Shreyash’s contributions have significantly improved the company’s technology stack, ensuring high performance and scalability for various applications. Prior to joining Janitri Innovations, Shreyash worked at Amazon, gaining valuable experience in large-scale system design and backend development. You can follow Shreyash on LinkedIn for more insights into backend development and technology trends.

--

--

Shreyash vardhan
CodeX
Writer for

Leading Backend engineering at Janitri innovations.