How to synchronously load a csv file into memory before handling HTTP requests
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Many backend applications need reference data — lookup tables, configuration, pricing rules — loaded from CSV files before the server can handle requests. If you start accepting HTTP requests before the data is ready, you risk returning incomplete or empty responses. The solution is to synchronously load the CSV into memory during application startup, before binding the HTTP server to a port.
Node.js: Synchronous CSV Loading
Using fs.readFileSync with Manual Parsing
Using a CSV Library (csv-parse)
For complex CSVs with quoted fields, newlines in values, or custom delimiters:
Python: Loading CSV Before Flask/FastAPI
Flask Example
FastAPI with Startup Event
Pandas for Large CSVs
Go: Loading CSV at Init
Handling Large Files
For CSVs that are too large for memory:
- Data Chunking: Divide the CSV into manageable chunks and only load necessary chunks on-demand.
- Data Compression: Compress the CSV file to reduce memory usage.
- Database Integration: If feasible, load the CSV data into a database and query it directly.
- Memory-Mapped Files: Use
mmapto map the file into virtual memory without loading it all at once.
Common Pitfalls
- Async loading without waiting: In Node.js, using
fs.readFile(async) without awaiting beforeserver.listen()means requests may arrive before data is ready. UsereadFileSyncorawaitthe async version before binding the port. - File path issues: Relative paths resolve from the working directory, not the script location. Use
path.join(__dirname, 'data.csv')in Node.js orpathlib.Path(__file__).parent / 'data.csv'in Python. - Encoding errors: CSV files may use UTF-8 with BOM, Latin-1, or other encodings. Specify encoding explicitly:
open('file.csv', encoding='utf-8-sig'). - Memory limits: A 1 GB CSV loaded into objects can consume 3-5 GB of RAM due to object overhead. Consider SQLite or a database for files over 100 MB.
- Hot reloading: Development servers that auto-reload (Flask debug, nodemon) re-read the CSV on every restart. For large files, this adds significant startup time.
Summary
- Load CSV data synchronously during application startup, before the HTTP server starts listening
- In Node.js, use
fs.readFileSyncor await async reads beforeserver.listen() - In Python, load at module level or in a startup event handler
- In Go, use
init()functions to load beforemain()runs - For large files, consider chunked loading, databases, or memory-mapped files
Related reading
- How to terminate a thread blocking on socket IO operation instantly?
- How to train and evaluate simultaneously in Object Detection API ?
- How to trigger message send of Fastapi websocket outside of Fastapi app
- How to turn off or handle camelCasing in JSON response ASP.NET Core?
- How to turn on front flash light programmatically in Android?
- How to unit test asynchronous APIs?
- How to update a Map or a List on AWS DynamoDB document API?
- How to upload a file and JSON data in Postman?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.