Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Share data between microservices

I work on a microservices architecture and I want to solve a small data sharing problem (I don't know if is the right word).

Example :

I have one user service and is DB that stores email, username, password...

I have another service and his database that work with the user data for generate documents with the user informations.

Which is the best way for the second service for accessing to the user data ? Replicate user data (Just if is required for his job) for her in is database ?

like image 451
PaulP Avatar asked Apr 09 '16 16:04

PaulP


People also ask

Can microservices share the same database?

In the shared-database-per-service pattern, the same database is shared by several microservices. You need to carefully assess the application architecture before adopting this pattern, and make sure that you avoid hot tables (single tables that are shared among multiple microservices).

How do you communicate between microservices?

The most common type is single-receiver communication with a synchronous protocol like HTTP/HTTPS when invoking a regular Web API HTTP service. Microservices also typically use messaging protocols for asynchronous communication between microservices.

Should two microservices share a database?

Microservices with shared databases can't easily scale. What is more, the database will be a single point of failure. Changes related to the database could impact multiple services. Besides, microservices won't be independent in terms of development and deployment as they connect to and operate on the same database.

Should you share code between microservices?

The general guideline in microservices is to not share code. Things that can be shared are libraries that don't changed very often like US States, color, etc. To answer your question what about the code for the communication between the microservices , I would say not to share this code.


2 Answers

I don't know the design of the whole system, but I would let the user microservice send an event when a user is created, updated or deleted. All other microservices which are interested in this event can extract the necessary user data they need.

By using microservices a redundant data cannot be avoided, but each microservice has different view on the data.

Your user microservice may see the user more as identity with complete user data (e.g id, password, first name, last name, email, address, organization, etc.) On the other hand, your document generation microservice may see the user as receiver of the document and do not need all user data.

like image 59
S.Stavreva Avatar answered Sep 23 '22 02:09

S.Stavreva


There is the caveat consultants answer of 'it depends'. Everyone has a different description of what microservices are, but for me they are small independently deployable services that do one thing well (i generally subscribe to fowlers definition of microsevices).

Actually I don't have a problem with microservices sharing storage (be it databases, blob storage etc.) as long as the microservices fall within the same 'service' boundary. What i mean by service boundary is a logical grouping of small services all collaborating to achieve some goal or business capability. For me its this logical grouping which forms the 'service' as viewed from outside the team. One service 'the user service' is often made up of many small microservices.

For example you might have a 'User' service that consists of a number of microservices. One to capture interest from a website and store it in a DB, one used by the website to process address changes, one to run a timed job and issue events to other services in the system.

But you would not want the Document Generation (reporting service?) reading and writing from the User DB, in this case (like Stavreva said) event communication between services would be the way to go.

Only you with your business domain knowledge can decide if two microservices belong within this same service, one tip is to look at the business capability that the microservice is contributing towards.

like image 6
Damo Avatar answered Sep 23 '22 02:09

Damo