Axum state when using clone and Arc

I am seeking advice on the proper approach to determine when to use Clone versus Arc when defining AppState.

From my understanding, Clone is suitable for inexpensive clone operations, whereas Arc is more appropriate for cases involving more complex data.

If the AppState contains an instance of a struct from another crate, such as the openai-rust crate (only for example purpose) for calling a third-party API service, should I use Arc in this case, considering the complexity of data structure from other crates?

1 Like

Performance really isn't that important here as the state is cloned only during construction of the router, not while your server is running.[1] Arc has the benefit that it is a smart pointer, i.e. cloning an Arc results in two pointers pointing to the same location in memory. If you need to implement some shared mutable state you should use Arc for consistent results when reading or writing to the state. And as you already noticed, for really large state objects Arc can also be useful to avoid multiple copies.

Note that most of the time such third party structs already use Arc under the hood, so cloning them is inexpensive and has the desired effect of sharing resources between instances (i.e. a connection pool).


  1. See answer below. ↩ī¸Ž

Thanks for the information

I see, I thought it's cloned during each request before.

Sorry for the misinformation, but you are right, state is actually cloned during extraction. I confused axum's State with actix-web's Data extractor, which actually puts your app data into an Arc, making it unnecessary to do it manually. So expensive cloning of the state can have a performance impact while the server is running.

2 Likes

If you're worried about the overhead of Arc, you can always Box::leak the state. It'll live as long as your server lives anyways.

1 Like

just know about this . Thank you!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.