Microservices &
API Gateways
Marco Palladino
#nginx #nginxconf2
I am Marco Palladino
CTO at mashape.com
Core committer at github.com/Mashape/kong
#nginx #nginxconf3
Monolitich vs Microservices
Pros and Cons
API Gateway Pattern
With real world use-cases
Kong + NGINX
For API Management
Topics
#nginx #nginxconf4
Monolithic Architecture
Client LB
Customers Orders Invoices
Customers Orders Invoices
Database Schema
#nginx #nginxconf5
Monolithic Application
Pros/Cons
Simplicity, for small
codebases
Faster early
development speed
Easy testing IDE support
Not ideal for growing
codebases
Slower iterations in
the long term
Harder to innovate
Steep code
learning curve
#nginx #nginxconf6
Microservice-oriented architecture
Client
LB Customers
Orders
Invoices
LB
LB
Customers
Orders
Invoices
DB Schema
DB Schema
DB Schema
Event Handler + Workers
#nginx #nginxconf7
Microservice-oriented Application
Pros/Cons
Better architecture for
large applications
Better agility in the
long term
Microservices: easy
to learn
Isolation for scalability
and damage control
More moving parts
Complex infrastructure
requirements
Consistency and
availability
Harder to test
#nginx #nginxconf8
Why an API Gateway?
API Gateway
Microservices
#nginx #nginxconf9
API Gateway Pattern
Client
LB Customers
Orders
Invoices
LB
LB
Customers
Orders
Invoices
DB Schema
DB Schema
DB Schema
API Gateway
• Optimized endpoints
• Request collapsing
• And more
#nginx #nginxconf10
Optimized Endpoints
Client
LB Customers
Orders
Invoices
LB
LB
Customers
Orders
Invoices
API Gateway
{
"id": "cus_123",
"customer_name": "Bob",
"address": "500 Montgomery St, SF"
}
{
"id": "order_123",
"customer_id": "cus_123",
"item_name": "Vacuum Cleaner"
}
{
"order_id": "order_123",
"price": "99.99"
}
GET /customers/{id}
{
"customer_id": "cus_123",
"name": "Bob",
"address": "500 Montgomery St, SF",
"orders": […],
"invoices": […],
}
+ extra transformations
#nginx #nginxconf11
Centralized Middleware Functionality
Client
Public APIs
Private APIs
Partner APIs
API Gateway
• Authentication
• Security
• Traffic Control
• Ops
• Logging
• Transformations
• Etc
Available to everybody
Only for internal usage
Only for specific partners
FaaS AWS Lambda, etc
#nginx #nginxconf12
Ops: Blue/Green deployments
customers.service
1.0.0
customer.service
1.0.1
API Gateway
ALL TRAFFIC
NO TRAFFIC
customers.service
1.0.0
customer.service
1.0.1
API Gateway
ALL TRAFFIC
NO TRAFFIC
#nginx #nginxconf13
Ops: Canary Releases
customers.service
1.0.0
customer.service
1.0.1
API Gateway
100% TRAFFIC
0% TRAFFIC
customers.service
1.0.0
customer.service
1.0.1
API Gateway
90% TRAFFIC
10% TRAFFIC
#nginx #nginxconf14
Ops: Load Balancing
Client OrdersLB
CustomersAPI Gateway
Client Orders
CustomersAPI Gateway
Client Orders
CustomersAPI Gateway
Service
Discovery
• etcd
• consul
1.
2.
3.
#nginx #nginxconf15
Ops: Circuit Breakers
Client Orders
OrdersAPI Gateway
Orders
Invoices
Orders
Customers
Too many 50x errors
Building a microservice
!=
Running a microservice
#nginx #nginxconf17
#nginx #nginxconf18
API Gateways, and Kong, can help
Microservice
MicroserviceMicroservice
Client
• Authentication
• Security
• Traffic Control
• Ops
• Logging
• Transformations
• Etc
• API for Automation
• On-boarding
• Developer Portal
#nginx #nginxconf
What is Kong?
Kong is an open-source management layer for APIs to secure, manage
and extend APIs and Microservices.
https://getkong.org
#nginx #nginxconf
What is Kong?
Built on top of NGINX, centralizes common middleware functionality:
#nginx #nginxconf
Kong Plugins
Can be created from scratch &
extended by the community.
#nginx #nginxconf
Kong: OpenResty + NGINX
NGINX
OpenResty
Clustering & Datastore
Plugins
RESTful Administration API
• JSON HTTP API
• Extendable by Plugins
• Can be integrated for automation
• Plugins created with LUA
• Intercept Request/Response lifecycle
• Can integrate with third-party services
• Either Cassandra or PostgreSQL
• Optionally Redis for some plugins
• Single or multi-DC clustering
• Underlying engine of Kong
• Provides hooks for Req/Res lifecycle
• Extends underlying NGINX
• The core dependency
• Handles low-level operations
• Solid foundation and known tech
#nginx #nginxconf
NGINX Configuration
worker_processes auto;
daemon on;
pid pids/nginx.pid;
error_log logs/error.log notice;
worker_rlimit_nofile 4864;
events {
worker_connections 4864;
multi_accept on;
}
http {
include 'nginx-kong.conf';
}
init_by_lua_block {
..
}
init_worker_by_lua_block {
..
}
server {
listen 0.0.0.0:8000;
location / {
access_by_lua_block {
..
}
header_filter_by_lua_block {
..
}
body_filter_by_lua_block {
..
}
log_by_lua_block {
..
}
}
}
..nginx.conf nginx-kong.conf
#nginx #nginxconf
Kong Entry-points
$ curl 127.0.0.1:8000
$ curl 127.0.0.1:8443
$ curl 127.0.0.1:8001
Proxy
Admin API
#nginx #nginxconf
Core Entities
$ curl 127.0.0.1:8001/apis
$ curl 127.0.0.1:8001/consumers
$ curl 127.0.0.1:8001/plugins
#nginx #nginxconf
Plugins Configuration Matrix
1. Per every API and every Consumer
2. Per every API and a specific Consumer
3. Per a specific API and every Consumer
4. Per a specific API and a specific Consumer
#nginx #nginxconf
Multi-DC deployment
DC1
KONG C*
API API API
API API API
KONG C*
DC2
KONGC*
API API API
API API API
KONGC*
• Horizontal Scalability
• Cassandra or PostgreSQL
• Clients can be both internal and external
Client Client
Invalidation events
Data
Demo Time
#nginx #nginxconf
Thank You
29
getkong.org
linkedin.com/marcopalladino
@thefosk
mashape.com

Microservices & API Gateways

  • 1.
  • 2.
    #nginx #nginxconf2 I amMarco Palladino CTO at mashape.com Core committer at github.com/Mashape/kong
  • 3.
    #nginx #nginxconf3 Monolitich vsMicroservices Pros and Cons API Gateway Pattern With real world use-cases Kong + NGINX For API Management Topics
  • 4.
    #nginx #nginxconf4 Monolithic Architecture ClientLB Customers Orders Invoices Customers Orders Invoices Database Schema
  • 5.
    #nginx #nginxconf5 Monolithic Application Pros/Cons Simplicity,for small codebases Faster early development speed Easy testing IDE support Not ideal for growing codebases Slower iterations in the long term Harder to innovate Steep code learning curve
  • 6.
    #nginx #nginxconf6 Microservice-oriented architecture Client LBCustomers Orders Invoices LB LB Customers Orders Invoices DB Schema DB Schema DB Schema Event Handler + Workers
  • 7.
    #nginx #nginxconf7 Microservice-oriented Application Pros/Cons Betterarchitecture for large applications Better agility in the long term Microservices: easy to learn Isolation for scalability and damage control More moving parts Complex infrastructure requirements Consistency and availability Harder to test
  • 8.
    #nginx #nginxconf8 Why anAPI Gateway? API Gateway Microservices
  • 9.
    #nginx #nginxconf9 API GatewayPattern Client LB Customers Orders Invoices LB LB Customers Orders Invoices DB Schema DB Schema DB Schema API Gateway • Optimized endpoints • Request collapsing • And more
  • 10.
    #nginx #nginxconf10 Optimized Endpoints Client LBCustomers Orders Invoices LB LB Customers Orders Invoices API Gateway { "id": "cus_123", "customer_name": "Bob", "address": "500 Montgomery St, SF" } { "id": "order_123", "customer_id": "cus_123", "item_name": "Vacuum Cleaner" } { "order_id": "order_123", "price": "99.99" } GET /customers/{id} { "customer_id": "cus_123", "name": "Bob", "address": "500 Montgomery St, SF", "orders": […], "invoices": […], } + extra transformations
  • 11.
    #nginx #nginxconf11 Centralized MiddlewareFunctionality Client Public APIs Private APIs Partner APIs API Gateway • Authentication • Security • Traffic Control • Ops • Logging • Transformations • Etc Available to everybody Only for internal usage Only for specific partners FaaS AWS Lambda, etc
  • 12.
    #nginx #nginxconf12 Ops: Blue/Greendeployments customers.service 1.0.0 customer.service 1.0.1 API Gateway ALL TRAFFIC NO TRAFFIC customers.service 1.0.0 customer.service 1.0.1 API Gateway ALL TRAFFIC NO TRAFFIC
  • 13.
    #nginx #nginxconf13 Ops: CanaryReleases customers.service 1.0.0 customer.service 1.0.1 API Gateway 100% TRAFFIC 0% TRAFFIC customers.service 1.0.0 customer.service 1.0.1 API Gateway 90% TRAFFIC 10% TRAFFIC
  • 14.
    #nginx #nginxconf14 Ops: LoadBalancing Client OrdersLB CustomersAPI Gateway Client Orders CustomersAPI Gateway Client Orders CustomersAPI Gateway Service Discovery • etcd • consul 1. 2. 3.
  • 15.
    #nginx #nginxconf15 Ops: CircuitBreakers Client Orders OrdersAPI Gateway Orders Invoices Orders Customers Too many 50x errors
  • 16.
  • 17.
  • 18.
    #nginx #nginxconf18 API Gateways,and Kong, can help Microservice MicroserviceMicroservice Client • Authentication • Security • Traffic Control • Ops • Logging • Transformations • Etc • API for Automation • On-boarding • Developer Portal
  • 19.
    #nginx #nginxconf What isKong? Kong is an open-source management layer for APIs to secure, manage and extend APIs and Microservices. https://getkong.org
  • 20.
    #nginx #nginxconf What isKong? Built on top of NGINX, centralizes common middleware functionality:
  • 21.
    #nginx #nginxconf Kong Plugins Canbe created from scratch & extended by the community.
  • 22.
    #nginx #nginxconf Kong: OpenResty+ NGINX NGINX OpenResty Clustering & Datastore Plugins RESTful Administration API • JSON HTTP API • Extendable by Plugins • Can be integrated for automation • Plugins created with LUA • Intercept Request/Response lifecycle • Can integrate with third-party services • Either Cassandra or PostgreSQL • Optionally Redis for some plugins • Single or multi-DC clustering • Underlying engine of Kong • Provides hooks for Req/Res lifecycle • Extends underlying NGINX • The core dependency • Handles low-level operations • Solid foundation and known tech
  • 23.
    #nginx #nginxconf NGINX Configuration worker_processesauto; daemon on; pid pids/nginx.pid; error_log logs/error.log notice; worker_rlimit_nofile 4864; events { worker_connections 4864; multi_accept on; } http { include 'nginx-kong.conf'; } init_by_lua_block { .. } init_worker_by_lua_block { .. } server { listen 0.0.0.0:8000; location / { access_by_lua_block { .. } header_filter_by_lua_block { .. } body_filter_by_lua_block { .. } log_by_lua_block { .. } } } ..nginx.conf nginx-kong.conf
  • 24.
    #nginx #nginxconf Kong Entry-points $curl 127.0.0.1:8000 $ curl 127.0.0.1:8443 $ curl 127.0.0.1:8001 Proxy Admin API
  • 25.
    #nginx #nginxconf Core Entities $curl 127.0.0.1:8001/apis $ curl 127.0.0.1:8001/consumers $ curl 127.0.0.1:8001/plugins
  • 26.
    #nginx #nginxconf Plugins ConfigurationMatrix 1. Per every API and every Consumer 2. Per every API and a specific Consumer 3. Per a specific API and every Consumer 4. Per a specific API and a specific Consumer
  • 27.
    #nginx #nginxconf Multi-DC deployment DC1 KONGC* API API API API API API KONG C* DC2 KONGC* API API API API API API KONGC* • Horizontal Scalability • Cassandra or PostgreSQL • Clients can be both internal and external Client Client Invalidation events Data
  • 28.
  • 29.