19

Imagine we have 2 services: Product and Order. Based on my understanding of SOA, I know that each service can have its own data store (a separate database, or a group of tables in the same database). But no Service is allowed to touch the data store of another Service directly.

Now, imagine we have stored the product and order data independently inside Product and Order Services. In the Order Service, we can identify products by their ID.

My question is: With this architecture, how can I display the list of orders and product details on the "same" page?

My understanding is that I should get the list of OrderItems from OrderService. Each OrderItem has a ProductID. Now, if I make a separate call to ProductService to retrieve the details about each Product, that would be very inefficient.

How would you approach this problem?

Cheers, Mosh

Mosh
  • 5,944
  • 4
  • 39
  • 44

6 Answers6

14

I did some research and found 2 different solutions for this.

1- Services can cache data of other Services locally. But this requires a pub/sub mechanism, so any changes in the source of the data should be published so the subscribing Services can update their local cache. This is costly to implement, but is the fastest solution because the Service has the required data locally. It also increases the availability of a Service by preventing it from being dependent to the data of other Services. In other words, if the other Service is not available, it can still do its job by its cache data.

2- Alternatively, a Service can query a "list" of objects from another Service by supplying a list of identifiers. This prevents a separate call to be made to the target service to get the details of a given object. This is easier to implement but performance-wise, is not as fast as solution 1. Also, in case the target Service is not available, the source Service cannot do its job.

Hope this helps others who have come across this issue.

Mosh

Mosh
  • 5,944
  • 4
  • 39
  • 44
  • 1
    I really hope no one else follows this coding paradigm as you have misapplied a LOT of information. There is absolutely NO "requirement" stating that two services YOU are in control of cannot be tied to the same database/tables/backing store. These "solutions" are imposing restrictions that simply do not exist and are considered really bad design. – NotMe Nov 03 '10 at 22:14
  • 11
    I'd like to disagree with Chris on this one. These "restrictions" are constraints that can be very useful. For a trite example, these constraints may help you to evolve your system in order to scale. Software development is about choosing your constraints wisely. In a little demo app, this constraint may seem silly, but in a large application that needs to be maintained over time, it makes a lot of sense. – Dave Nichol May 30 '13 at 14:49
3

DB integration (which is really what you are talking about when two services share a table in a DB) is wrong at so many levels! It completely breaks some of the major principles of software engineering

loose coupling, encapsulation separation of concerns

A service should be (to earn that name) completely independent, namely:

  1. it must not rely on others to ensure the consistency and coherence of its data
  2. it must not rely on others to guaranty the security of its data
  3. it must not depend on external implementations (only interfaces)

Two services that share data at the DB level are unable to guarantee any of the former.

The fact that you "control" both services is completely irrelevant. Today you control... tomorrow you might want to outsource or replace one of the services. That should be as simple as ensuring the proper interfaces are in place.

Imagine both services that share a table with some field (varchar) in it. Now one service needs to change that field to numeric... bang the other service stops functioning - loose coupling goes down the drain.

Most of the time the trick lies in properly defining the service scope and clearly stipulating what a service do and what it doesn't do. You should also avoid turning everything into a service. Set your service granularity to high and services will start popping everywhere and integration headaches will escalate.

That being said, there are some situations where data integration between services poses some challenges. The main premiss do, should always be - data can belong only to one service. Data is intrinsically tied to business logic that affects data consistency and coherence and as such there should never be more than one service controlling any given data.

lucas
  • 39
  • 1
2

Another approach would be to have some sort of data source that lives outside of the SOA services. This data source could be considered your cache of the data, your operational data source or even a data warehouse. Extraction packages can export the data from the services (and/or some sort of real time mechanism). You can query this data source how you want.

The advantage of this approach is that the SOA black box is maintained and you can swap out a service knowing how you have coupled it.

Disadvantage is the added complexity and maintenance overhead.

Lee Simpson
  • 179
  • 3
  • 11
0

It is unfortunate to see this whole discussion being deteriorating in a "can I use a shared database or not in SOA" statement quest, which is totally irrelevant and does not help answering the original question at all.

More then often in a real world situation the data is already stored in different systems to start with. Customer data for example is coming from CRM, product data from SAP, contract data from yet another different source.

It is not a quest for getting this data technically together, rather than an understanding there is only one source of the data. To differently put it, there is only one owner of the data within your enterprise, who is solely responsible for maintaining it and ensuring the correct data quality.

Storing data locally for performance reasons means replicating data, which is more than often a dangerous venture, unless you have a solid caching strategy in place. I think Mosh has given some sensible answers when faced with an existing application landscape.

Eric
  • 1
  • 1
0

SOA is just a buzz-phrase for deploying components behind web services. How many data stores you have is entirely up to you. In some cases it makes sense to have partitioned data behind individual components, and in other cases all the data lives behind one service, and in other cases many components that expose service interfaces connect to the same database via the database's connection protocol. Approach the problem by approaching the problem, not be imposing artificial constraints.

bmargulies
  • 97,814
  • 39
  • 186
  • 310
0

I don't think there is any principle in SOA that services should have separate data store. In general it is actually impractical. Yes you can have product and order service and the client can do the join using web service call as you said and this may be acceptable in some scenario. But that doesn't mean that you cannot have a specific service for a client if you already know the client's behaviour and performance requirements.
What I mean is that you should have a search service that returns orders and products with the join done in database. This is practical and would solve your business problem.

softveda
  • 10,858
  • 6
  • 42
  • 50
  • 6
    I'm not an SOA expert, but I believe the idea of each Service having its own data store is a core principle in SOA. Services are not allowed to touch the data store each other directly. If they need data, they should ask the Service that encapsulates the data. This is to prevent changes to the schema of that data store breaking any other Services directly dependent on that. – Mosh Oct 27 '10 at 00:27
  • 3
    I have doing SOA for few years now. Services are autonomous but that doesn't mean they cannot share data store. In this particular case where products and orders are related then you will end up with a service with a fat surface area managing both entities (to satisfy your definition). In many enterprise core systems there is one data source (source of truth) and there can be multiple services that manages various entities of your data model. Consistency can be achieved by having a common business logic layer implementation for the various service interfaces. – softveda Oct 27 '10 at 08:59