Skip to main content

Multithreading and parallelism

DuckDB supports two concurrency models:

  • Single-process read/write where one process can both read and write to the database.
  • Multi-process read-only (access_mode = 'READ_ONLY') multiple processes can read from the database, but none can write.

This approach provides significant performance benefits for analytics databases. You can find more details on how to handle multiple process writes (or multiple read + write connections) in the DuckDB documentation.

Closing Database Instances

Python snippets showing how to close database instances are shown below:

con = duckdb.connect("md:my_db?cache_buster=123", config={"motherduck_token": my_other_token})

Or you can set the dbinstance_inactivity_ttl setting to zero:

con = duckdb.connect("md:my_db", config={"motherduck_token": token})
con.sql("SET motherduck_dbinstance_inactivity_ttl='0ms'")

Depending on the needs of your data application, you can use multithreading for improved performance. If your queries will benefit from concurrency, you can create connections in multiple threads. For multiple long-lived connections to one or more databases in one or more MotherDuck accounts, you can use connection pooling. Implementation details can be seen in the cards linked below: