- Urls
/home,/products- doesn't need authentication/my-cart/get,/my-cart/add- needs authentication/product/add,/product/delete- needs authorization
- SSL Certificate - for https
- Change certificate every 30 or 60 days
- The above components can be implemented in API gateway
- It is an entry point for API
- First request goes to API gateway
- API gateway ensures https, authentication and authorization
- It is a guard for any requests
- It is an entry point for API
- Features
- Feature #1: Separate out cross cutting concerns
- Authentication
- Authorization
- SSL Termination
- DDoS protection/ Throttling
- Feature #2: Suppose we use microservices as follows which are consilidated into single component
- service 1:
/home,/my-cart - service 2:
/product
- service 1:
- Feature #3: Replacing multiple client calls with single API call, also has features of reverse proxy (serving static content, caching responses)
- An adapter (in gateway) can make the following calls
- Static files (HTML, JS, CSS) - A component can exist in the gateway for this (instead of having a service) called Static Content
- Personalized Recommendations
- Trending Products - This does not change so frequently so this can be cached in Cache of the API gateway (we can give threshold time for the Cache)
- Discount Offers
- An adapter (in gateway) can make the following calls
- Feature #4: Routing based on headers, paths and params etc, load balancing, A/B testing, canary release
- Router:
- If one service give low quality video (for mobile) this is chosen for mobile which is fast
- If another service gives high quality video (for desktop/laptop) this is chosen for other computers which is slow
- Load Balancer:
- If a service is in multiple copies (because the load is high) - round robin can be used (or some algorithm like consistent hashing)
- Router:
- Feature #5:
- If backend services are not ready for latest protocols like http-2 and Websocket, API gateway can convert from new protocol to existing protocol
- All requests can be monitored for performance and latencies
- Dashboards exist
- We can expose to market an API service
- Based on service usage, we can charge
- Feature #1: Separate out cross cutting concerns
- Gateway Configuration
- In House
- Apache
- HAProxy
- Nginx
- Spring Cloud Gateway
- Cloud Services
- AWS API Gateway
- Azure API Gateway
- Google Cloud Endpoints
- Apigee
- In House
- Parallelism
-
Example:
public static void main(String[] args) { new Thread(new Runnable() { // task 1 - first thread @Override public void run() { processTax(user1); } }).start(); new Thread(new Runnable() { // task 2 - second thread @Override public void run() { processTax(user2); } }).start(); heavyCalculations(); // task 3 - run by main thread }- If we have a 4 core processor, we can run the three tasks in parallel
- The tasks are not dependent on each other
- If we have a 4 core processor, we can run the three tasks in parallel
-
Parallelism is about doing lot of things at once - Rob Pike
-
Example:
ExecutorServicepublic static void main(String[] args) { ExecutorService es = Executors.newFixedThreadPool(4); // thread pool es.submit(() -> processTax(user1)); es.submit(() -> processTax(user2)); heavyCalculations(); } -
Ways of enabling parallelism: Java
- Threads - raw threads
- ThreadPool
- ExecutorService
- ForkJoinPool (newer)
- Custom ThreadPools (eg: Web Servers)
- Note: Requires > 1 CPU cores (pre-requisite for parallelism)
-
- Concurrency:
-
Example:
public static void main(String[] args) throws InterruptedException { new Thread(() -> { if (ticketsAvailable > 0) { // task accessing shared variable bookTicket(); ticketsAvailable--; } }).start(); new Thread(() -> { // task accessing shared variable if (ticketsAvaialable > 0) { bookTicket(); ticketsAvailable--; } }).start(); Thread.sleep(5000); }
-
- What is service mesh?
- Concept in microservices