Microservice Patterns
Microservice Patterns
1. Shared Database
Use a (single) database that is shared by multiple services. Each service
freely accesses data owned by other services using local ACID transactions.
Advantages:
Issues:
Runtime coupling - because all services access the same database they
can potentially interfere with one another
Single database might not satisfy the data storage and access
requirements of all services.
There are a few different ways to keep a service’s persistent data private.
You do not need to provision a database server for each service. For example,
if you are using a relational database then the options are:
It is a good idea to create barriers that enforce this modularity. You could, for
example, assign a different database user id to each service and use a
database access control mechanism such as grants. Without some kind of
barrier to enforce encapsulation, developers will always be tempted to bypass
a service’s API and access it’s data directly.
Example: The FTGO application is an example of an application that uses
this approach. Each service has database credentials that only grant it
access its own (logical) database on a shared MySQL server.
Advantages:
Helps ensure that the services are loosely coupled. Changes to one
service’s database does not impact any other services.
Each service can use the type of database that is best suited to its
needs. For example, a service that does text searches could use
Elasticsearch. A service that manipulates a social graph could use
Neo4j.
Issues:
Implementing business transactions that span multiple services is not
straightforward.
Solutions:
Implementing transactions that span services - use the Saga pattern.
Domain events are events generated on updating and creating certain data.
These events are consumed by other services.
Advantages:
Issues:
Increased complexity
Potential code duplication
Replication lag/eventually consistent views
4. Saga
How to implement transactions that span services?
Advantages:
Issues:
5. Circuit Breaker
When one service synchronously invokes another there is always the
possibility that the other service is unavailable or is exhibiting such high
latency it is essentially unusable.
A service client should invoke a remote service via a proxy that functions in a
similar fashion to an electrical circuit breaker.
After the timeout expires the circuit breaker allows a limited number of test
requests to pass through. If those requests succeed the circuit breaker
resumes normal operation. Otherwise, if there is a failure the timeout period
begins again
Advantages:
Issues:
How does the API gateway discover the location of a service instance?
Lambda Architecture
Aim: To satisfy the needs for a robust system that is fault-tolerant, both
against hardware failures and human mistakes, being able to serve a wide
range of workloads and use cases, and in which low-latency reads and
updates are required. The resulting system should be linearly scalable, and it
should scale out rather than up.
1. All data entering the system is dispatched to both the batch layer and
the speed layer for processing.
2. The batch layer has two functions:
a) Managing the master dataset (an immutable, append-only set of
raw data)
b) To pre-compute the batch views.
3. The serving layer indexes the batch views so that they can be queried
in low-latency, ad-hoc way.
4. The speed layer compensates for the high latency of updates to the
serving layer and deals with recent data only.
5. Any incoming query can be answered by merging results from batch
views and real-time views.
Twitter tweets analysis using Lambda architecture, article explains
Lambda architecture as follows:
Batch Layer: The batch layer precomputes the master dataset (it is core components of
lambda architecture and it contains all data) into batch views so that queries can be resolved
with low latency.
Speed Layer: In speed layer we are basically do two things, storing the real-time views and
processing the incoming data stream so as to update those views. It fills the delta gap that is
left by batch layer. that means combine speed layer view and batch view give us capability
fire any adhoc query over all data that is query=function(over all data).
Serving Layer: It provides low-latency access to the results of calculations performed on the
master dataset. It combines batch view and real-time view to give result in real-time for any
adhoc query over all data.