How-to deal with variable-latency sync functions in an async context?

⚓ Rust    📅 2025-09-22    👤 surdeus    👁️ 6      

surdeus

Warning

This post was published 39 days ago. The information described in this article may have changed.

Hi there!

I'm currently building a web server with axum, in a good'ol async fashion.

For data persistence, I want to use the embedded database redb for storage, which only has synchronous methods.

Calling a method to read data could range from a microsecond to several milliseconds depending on whether the data is in memory or on the disk. Here is the problem I have:

  • If I call redb's methods synchronously from an async context, it may block the thread entirely, which is not viable ;
  • If I use e.g. tokio's spawn_blocking function to move the call on a dedicated worker thread, it will have some latency in order to move the task there, especially if it needs to create a new thread for the task.

So I'm basically stuck with losing a lot of performance when redb takes a long time (case 1) or a little bit of performance, many times when it takes a short time (case 2).

What solution would there be to have minimum latency when redb returns data quickly without blocking the thread when it takes longer, knowing that it's not possible to know ahead of time how long the method will take?

Thanks in advance for your help!

1 post - 1 participant

Read full topic

🏷️ Rust_feed