1

Here on Stack has the same question that I wanted to do but my question has one different aspect.

Today I have a rails application that's managing the web application and API but wanna separate it to manage the server resources more efficiently.

The question is: How App1 could know when App2 inserts a new record? Rpush? Sql Trigger? Or transform Api into a gem?

App1 - WebService Port 80 using DB1

class Post < ActiveRecord::Base
 belongs_to :user
 before_save :do_stuff_when_app2_insert_record #I know this doesn't work in this environment  

 def do_stuff_when_app2_insert_record
  ...
 end
end


App2 - API connection - Port 8080 using DB1

class Post < ActiveRecord::Base
 belongs_to :user
end

Post.create(name: 'post')
4
  • 1
    Why does App1 need to know when App2 inserted new records? Wouldn't it be enough when App1 will return those new records when reading or listing records? Commented Mar 23, 2023 at 20:13
  • Because Api on App2 receives the main information and App1 processes that. How reading? Do you mean using the SideKiq? Commented Mar 23, 2023 at 23:26
  • 2
    In my experience, two apps sharing a database is error-prone and should be avoided. It is too easy that a database migration from one of the apps takes down the other app. Instead, when App1 receives information that App2 should process, then App1 should forward the information directly to App2, for example via an API on App2. When you want to keep the one database, when you still need something like a callback in App2 that App1 pings after adding data. Commented Mar 24, 2023 at 5:33
  • @spickermann App2 would only be allowed to write in a single model table in the database and App1 would continue with de logic. In this way, I will have metrics to design betters servers for these two critical points Commented Mar 24, 2023 at 12:41

1 Answer 1

1

Don't Do This

First, it's generally not a good idea for two independent apps to share a database. Different threads/processes of a single app? Sure. But independent applications should communicate through structured processes instead of having their data changed underneath them.

... but if you have to

If you decide to go this route anyways, App1 could know about updates made by App2 through a number of mechanisms. You could write changes to a changes/notifications table App1 could poll. You could use custom triggers or stored procedures depending on the RDBMS. App2 could directly write to any job queue App1 is using (sidekiq's redis instance, delayed job table, etc). App2 could hit an API endpoint on App1. Updates could be pushed using a message queue (redis/rabbitmq/kafka). There isn't really a best practice here since you've already abandoned them to go this route.

Distributed App Approach

If what you really mean (or decide to implement) is App1 and App2 should have separate databases, but share data, then there are best practices available. You'll want some sort of flexible/scalable message broker in-between (kafka/rabbitmq/redis) and likely share data using a strongly typed schema amenable to future changes. Kafka has built in tools for this but tools like google's ProtoBufs also exist.

This means moving to a truly distributed architecture, which means a number of additional concerns and complexities. You will have to consider which system is the source of truth for any shared data. You'll need to have mechanisms to keep the systems in sync, detect when they are out of sync, and recover from issues. These last steps are often overlooked when architecting distributed systems and can grind organizations to a halt if they aren't considered from the beginning.

With this approach, updates would be published to the bus and App1 could listen for Post updates, record them locally, and trigger callbacks.

Probably the Right Way: Monolith

Yet another approach is App1 and App2 are really just different faces of the same rails app. Unless they absolutely positively need to be separate apps for some concrete reason: they should not be. Whatever your reasons for doing so should be met with extreme skepticism.

Rails is an extremely flexible framework and tools like rails engines already exist with widely used examples to draw from. Your rails app can be many apps and doing so eliminates vast amounts of complexity.

There are legitimate reasons you might need more apps, but if they need to share data to such a degree that a shared database is being considered, then you may be forcing a boundary between problems that can be solved in a single code base.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.