All Projects → MatteoGioioso → Serverless Pg

MatteoGioioso / Serverless Pg

Licence: mit
A package for managing PostgreSQL connections at SERVERLESS scale

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Serverless Pg

Serverless Aws Lambda Node Postgres
Serverless AWS Lambda with Node.js,Postgres Rest API with Sequelize.
Stars: ✭ 18 (-87.32%)
Mutual labels:  aws, serverless, aws-lambda, postgresql
Serverless Docker Image Resize
Simple serverless image resize on-the-fly - Deploy with one command - Built with AWS Lambda and S3
Stars: ✭ 114 (-19.72%)
Mutual labels:  aws, serverless, aws-lambda
Awesome Layers
λ A curated list of awesome AWS Lambda Layers. Sponsored by https://cloudash.dev
Stars: ✭ 1,655 (+1065.49%)
Mutual labels:  aws, serverless, aws-lambda
Node Lambda Log
Basic logging mechanism for Node 6.10+ Lambda Functions
Stars: ✭ 115 (-19.01%)
Mutual labels:  aws, serverless, aws-lambda
Serverless
⚡ Serverless Framework – Build web, mobile and IoT applications with serverless architectures using AWS Lambda, Azure Functions, Google CloudFunctions & more! –
Stars: ✭ 41,584 (+29184.51%)
Mutual labels:  aws, serverless, aws-lambda
Serverless static website with basic auth
Builds a serverless infrastructure in AWS for hosting a static website protected with Basic Authentication and published on a subdomain registered via Route 53
Stars: ✭ 112 (-21.13%)
Mutual labels:  aws, serverless, aws-lambda
Spark On Lambda
Apache Spark on AWS Lambda
Stars: ✭ 137 (-3.52%)
Mutual labels:  aws, serverless, aws-lambda
Lambcycle
🐑🛵 A declarative lambda middleware with life cycle hooks 🐑🛵
Stars: ✭ 88 (-38.03%)
Mutual labels:  aws, serverless, aws-lambda
Serverless Architectures Aws
The code repository for the Serverless Architectures on AWS book
Stars: ✭ 120 (-15.49%)
Mutual labels:  aws, serverless, aws-lambda
Aws Lambda R Runtime
Serverless execution of R code on AWS Lambda
Stars: ✭ 121 (-14.79%)
Mutual labels:  aws, serverless, aws-lambda
Iopipe Js Core
Observe and develop serverless apps with confidence on AWS Lambda with Tracing, Metrics, Profiling, Monitoring, and more.
Stars: ✭ 123 (-13.38%)
Mutual labels:  aws, serverless, aws-lambda
Lambdauth
A sample authentication service implemented with a server-less architecture, using AWS Lambda to host and execute the code and Amazon DynamoDB as persistent storage. This provides a cost-efficient solution that is scalable and highly available and can be used with Amazon Cognito for Developer Authenticated Identities.
Stars: ✭ 1,365 (+861.27%)
Mutual labels:  aws, serverless, aws-lambda
Serverless Chat
A serverless web chat built using AWS Lambda, AWS IoT (for WebSockets) and Amazon DynamoDB
Stars: ✭ 99 (-30.28%)
Mutual labels:  aws, serverless, aws-lambda
Cra Serverless
Serverless pre-rendering (SSR) for React SPA using AWS Lambda, S3, and CloudFront.
Stars: ✭ 137 (-3.52%)
Mutual labels:  aws, serverless, aws-lambda
Kinesis Streams Fan Out Kinesis Analytics
Amazon Kinesis Streams fan-out via Kinesis Analytics (powered by the Serverless Framework)
Stars: ✭ 95 (-33.1%)
Mutual labels:  aws, serverless, aws-lambda
Lambda Toolkit
*DO NOT USE* - This project was done during my initial python and lambda's studies. I would recommend you the `serverless framework`.
Stars: ✭ 114 (-19.72%)
Mutual labels:  aws, serverless, aws-lambda
Aws Lambda List
A list of hopefully useful AWS lambdas and lambda-related resources.
Stars: ✭ 130 (-8.45%)
Mutual labels:  aws, serverless, aws-lambda
Python Lambda
A toolkit for developing and deploying serverless Python code in AWS Lambda.
Stars: ✭ 1,247 (+778.17%)
Mutual labels:  aws, serverless, aws-lambda
Aws Serverless Airline Booking
Airline Booking is a sample web application that provides Flight Search, Flight Payment, Flight Booking and Loyalty points including end-to-end testing, GraphQL and CI/CD. This web application was the theme of Build on Serverless Season 2 on AWS Twitch running from April 24th until end of August in 2019.
Stars: ✭ 1,290 (+808.45%)
Mutual labels:  aws, serverless, aws-lambda
Serverless Layers
Serverless.js plugin that implements AWS Lambda Layers which reduces drastically lambda size, warm-up and deployment time.
Stars: ✭ 119 (-16.2%)
Mutual labels:  aws, serverless, aws-lambda

Serverless-postgres

semantic-release npm version GitHub

Serverless-postgres is a wrapper for node-pg Node.js module. It is heavily inspired by Jeremy Daly's serverless-mysql package.

Why I need this module?

In a serverless application a function can scale almost "infinitely" by creating separate container instances for each concurrent user. Each container can correspond to a database connection which, for performance purposes, is left opened for further re-utilization. If we have a sudden spike of concurrent traffic, the available connections can be quickly maxed out by other competing functions. If we reach the max connections limit, Postgres will automatically reject any frontend trying to connect to its backend. This can cause heavy disruption in your application.

What does it do?

Serverless-postgres adds a connection management component specifically for FaaS based applications. By calling the method .clean() at the end of your functions, the module will constantly monitor the status of all the processes running in the PostgreSQL backend and then, based on the configuration provided, will garbage collect the "zombie" connections. If the client fails to connect with "sorry, too many clients already" error, the module will retry using trusted backoff algorithms.

NOTE: This module should work with any PostgreSQL server. It has been tested with AWS's RDS Postgres, Aurora Postgres, and Aurora Serverless.

Feel free to request additional features and contribute =)

Install

npm i serverless-postgres

Usage

Declare the ServerlessClient outside the lambda handler

const ServerlessClient = require('serverless-postgres')

const client = new ServerlessClient({
    user: process.env.DB_USER,
    host: process.env.DB_HOST,
    database: process.env.DB_NAME,
    password: process.env.DB_PASSWORD,
    port: process.env.DB_PORT,
    debug: true,
    delayMs: 3000,
});

const handler = async(event, context) => {
    await client.connect();
    const result = await client.query(`SELECT 1+1 AS result`);
    await client.clean();
    return {
      body: JSON.stringify({message: result.rows[0]}),
      statusCode: 200
    }
}

You can set the configuration dynamically if your secret is stored in a vault

const ServerlessClient = require('serverless-postgres')

const client = new ServerlessClient({
    host: process.env.DB_HOST,
    database: process.env.DB_NAME,
    port: process.env.DB_PORT,
});

const handler = async(event, context) => {
    const {user, password} = await getCredentials('my-secret')
    client.setConfig({
      user, password
    })
    await client.connect();
    // ...rest of the code
}

Configuration Options

Property Type Description Default
config Object A node-pg configuration object as defined here {}
maxConnsFreqMs Integer The number of milliseconds to cache lookups of max_connections. 60000
manualMaxConnections Boolean if this parameters is set to true it will query to get the maxConnections values, to maximize performance you should set the maxConnections yourself false
maxConnections Integer Max connections of your PostgreSQL. I highly suggest to set this yourself 100
strategy String Name of your chosen strategy for cleaning up "zombie" connections, allowed values minimum_idle_time or ranked minimum_idle_time
minConnectionIdleTimeSec Integer The minimum number of seconds that a connection must be idle before the module will recycle it. 0.5
maxIdleConnectionsToKill Integer or null The amount of max connection that will get killed. Default is ALL null
connUtilization Number The percentage of total connections to use when connecting to your PostgreSQL server. A value of 0.80 would use 80% of your total available connections. 0.8
debug Boolean Enable/disable debugging logs. false
capMs Integer Maximum number of milliseconds between connection retries. 1000
baseMs Integer Number of milliseconds added to random backoff values. 2
delayMs Integer Additional delay to add to the exponential backoff. 1000
maxRetries Integer Maximum number of times to retry a connection before throwing an error. 3
processCountCacheEnabled Boolean Enable caching for get process count. False
processCountFreqMs Integer The number of milliseconds to cache lookups of process count. 6000

Note

  • Serverless-postgres depends on pg package and usually you do not need to install it on your own. As some users have observed, if you have installed it on your own, and it is a different version, this package might misbehave.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].