⚙️

FastAPI vs Golang Performance Comparison

Mar 17, 2025

Comparing FastAPI with Golang Standard Library

Overview

  • FastAPI: A fast Python web framework.
  • Golang: Using its standard library.
  • Tests conducted on AWS with Kubernetes.

Test Setup

Baseline Test

  • Aim: Test framework performance using hardcoded objects returned in JSON.
  • Metrics: Latency, throughput, CPU usage, memory usage, error rate, CPU throttling.

Second Test

  • POST request handling: Parses body, generates UUID & timestamp, inserts record into PostgreSQL.
  • Cache: Memcached used for caching.
  • Metrics: Includes Prometheus metrics for database, cache operations, CPU usage, connection pool size.
  • Infrastructure: Memcached on m7a.xlarge, PostgreSQL on specialized 2xlarge, EKS cluster with two node groups.

Infrastructure Details

  • Node Groups: General-purpose instances for apps, compute-optimized for Prometheus, Grafana, etc.
  • Cost Efficiency: Compute-optimized instances are cheaper due to less memory, used for non-memory-intensive operations.

Baseline Test Results

FastAPI

  • Latency: Higher than Go application.
  • Throughput: Reached 13,000 requests per second.
  • CPU Usage: High, leading to throttling and performance degradation.
  • Availability: Dropped to zero when overloaded.
  • Memory Usage: Notable spike before failure.

Golang

  • Latency: Lower than FastAPI.
  • Throughput: Reached around 62,000 requests per second.
  • Consistency: Maintained performance across tests.
  • Notes: Limit Go user threads to avoid Kubernetes throttling.

Second Test Results

FastAPI

  • Workers: Increased from 2 to 4 to improve CPU usage.
  • Performance: Reached 800 requests per second.
  • Limit Reached: Within 10-15 minutes.

Golang

  • Performance: Better than FastAPI; lower database and cache latency.
  • Throughput: Started failing at 35,000 requests per second due to file descriptor limits.

Additional Observations

  • PostgreSQL & Memcached CPU Usage: Tracked during tests.
  • Connection Pool: Limited to 500, increased database connections over 1,000.
  • Go Limit: Could be higher with proper file descriptor settings.

Conclusion

  • FastAPI performance is underwhelming compared to Golang.
  • Source code for tests and configurations available in the video description.
  • Encouragement to improve FastAPI and contribute back.

Additional Benchmarks

  • Mention of other benchmarks such as Rust vs. Go on the same channel.