Skip to content
This repository has been archived by the owner on Jun 13, 2019. It is now read-only.

Proper way to handle database connections ? #4

Closed
kamek-pf opened this issue Jul 27, 2017 · 3 comments
Closed

Proper way to handle database connections ? #4

kamek-pf opened this issue Jul 27, 2017 · 3 comments

Comments

@kamek-pf
Copy link

kamek-pf commented Jul 27, 2017

Hey there,
So, I realize this might fall outside the scope of this crate, but Rust + Lambda isn't exactly a common thing yet, anyways :

From the AWS Lamba Python doc :

We recommend that pymysql.connect() is executed outside the [lambda] handler, as shown, for better performance.

My question is, is there any way to do this in Rust, and if not, what would you recommend ?

@iliana
Copy link
Owner

iliana commented Jul 28, 2017

Hmm.

I'm pretty sure the current crowbar interface doesn't have a way of doing this at all, to directly answer your question. I suppose it's also very difficult to create a database connection in Python and hand it off to a Rust module, so perhaps that's not worth investigating either.

I'd like to see if there's a way to run code on import of a rust-cpython module. Then I'd need to make the return value of that code accessible to the handler.

@kamek-pf
Copy link
Author

After further investigation, the lazy_static crate solves this problem nicely !

lazy_static! {
    static ref CONN: Mutex<Connection> = Mutex::new(Connection::connect("postgres:/your_connection_string", TlsMode::None).unwrap());
} 

lambda!(|_, _| {
    let conn = CONN.lock().unwrap();
    your_service(&conn)
});

Lambda seems to effectively re-use existing processes and keep connections open.
When opening connections directly in the lambda! closure, I'm seeing 20-30ms execution times, but with this, it drops to 2-3ms.

An equivalent (warm) function in Node is also in the 2-3ms range, so this is very good.
Cold starts are also way more efficient than with Node : I'm getting random cold starts between 200-600ms in Node, but nothing above 100ms in Rust yet.

Great job on this crate @ilianaw :)

@iliana
Copy link
Owner

iliana commented Jul 30, 2017

Nice!

(Yeah, my understanding is that the Python interpreter stays running, with your code imported, until Lambda kills that process for inactivity.)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants