Intro
The purpose of this article is to provide information on how to properly implement AWS Lambda with Neo4j aura.
Challenges
The reason using AWS lambda can be challenging, is that if implemented incorrectly, the driver object and the neo4j package installation will not be reused. This creates some unnecessary additional overheads which can affect the performance of your workload.
Solution
The solution to this is to declare the driver object outside of the lambda event handler, the code outside of the event handler is executed before the event handler.
Here is an example of a lambda function to implement this:
from neo4j import GraphDatabase
URI = "neo4j+s://1709ed87.databases.neo4j.io"
AUTH = ("neo4j", "xxxxxxxxxxx")
driver = GraphDatabase.driver(URI, auth=(AUTH))
def create_person(tx, name):
tx.run("CREATE (a:Person {name: $name})", name=name)
def lambda_handler(event, context):
with driver.session() as session:
session.execute_write(create_person, "James")
The event handler is the lambda_handler function, everything outside of this is considered the init code. Once the lambda_handler completes, the execution environment stays around for a non-deterministic period of time, if other requests for the same function come in while it still exists, then the execution environment can be reused, meaning the package installation and driver object can potentially be used many times.
To further improve this, you can configure Provisioned Concurrency. The way this works is that Lambda will prepare and keep warm a set number of execution environments that are ready to respond to events. For example, configuring a Provisioned Concurrency of 10, means that 10 execution environments are created and their init code is executed, in this case meaning the neo4j package and driver object are created, before any requests have been sent to the lambda function.
It is highly recommended to use Provisioned Concurrency for production workloads, as they offer more consistent. For more information on how to configure this please see the link to the official AWS documentation in the Further Reading section below.
Further Reading
The following article goes into why this works in a little more detail: https://aws.amazon.com/de/blogs/compute/operating-lambda-performance-optimization-part-1/
The following article is the official documentation for Lambda Provisioned Concurrency: https://docs.aws.amazon.com/lambda/latest/dg/provisioned-concurrency.html
Comments
0 comments
Please sign in to leave a comment.