Lightning Talk: Multiple Backend DBs In A Rails Application

Summarized using AI

Lightning Talk: Multiple Backend DBs In A Rails Application

Anton Lvov • October 13, 2017 • Selangor, Malaysia • Lightning Talk

In the "Lightning Talk: Multiple Backend DBs In A Rails Application," speaker Anton Lvov shares his journey and insights on implementing a Rails application that utilizes multiple backend databases. Coming from a Python background, Anton emphasizes the collaborative environment at Rapid River Software, where he learned to tackle complex challenges with his team.

Key points covered in the talk include:

- Introduction and Background: Anton introduces himself as a relative newcomer to Ruby and shares his professional journey from Python and enterprise environments.

- Project Challenge: The team faced a significant challenge in organizing ETL data flows from four source databases containing web events and related information, culminating in the need for a web statistics database.

- Choice of Framework: After deliberation, the team opted for a Rails application due to the expertise of their developers.

- Handling Multiple Databases in Rails:

- Configuration Management: Each database connection should have its own configuration file in the config/database directory.

- Initialization: Database connections need to be initialized during the Rails application's startup process.

- Model Organization: Each database should have its own folder for models, with a base abstract model for establishing database connections.

- Migration Generation: Custom generators for migrations allow easier management of database-specific activities, simplifying development workflows.

- Rake Tasks: Creation of rake tasks specific to each database facilitates common operations such as migrations and resets.

- Custom Solutions: The team created tailored rake tasks and used PostgreSQL table partitions to optimize performance for large datasets.

- Future Development: Anton mentions plans to develop a gem that would aid in setting up multi-database configurations in Rails projects, aiming to share this solution with the wider community.

In conclusion, Anton appreciates the Ruby community and expresses a desire to contribute further by sharing knowledge and tools that help optimize multi-database management in Rails applications. He encourages attendees to connect and learn more about advanced topics he can cover beyond this talk.

Lightning Talk: Multiple Backend DBs In A Rails Application
Anton Lvov • October 13, 2017 • Selangor, Malaysia • Lightning Talk

Speaker: Anton Lvov

Website: http://rubyconf.my

Produced by Engineers.SG

RubyConf MY 2017

00:00:06.080 so hi again my friends I want to begin
00:00:10.070 with a saying that I am relatively new
00:00:13.730 guy in the Ruby world so my first Ruby
00:00:16.700 code appeared less than two years ago I
00:00:19.849 came here from pythonic world and I came
00:00:22.880 to pythonic world from polluted blood
00:00:25.279 enterprise world and I still survived so
00:00:29.540 you guys have really comfortable place
00:00:31.250 to live and work in which I appreciate
00:00:34.040 and it co it could be sound pathetic but
00:00:36.470 I really you know like to stand here in
00:00:39.590 front of you and talking and it's a big
00:00:42.410 honour for me so thanks for the
00:00:44.359 opportunity to do this thank you so okay
00:00:50.960 let's start with a short introduction
00:00:52.160 Who am I
00:00:53.179 first of all yeah I am a weird-sounding
00:00:55.370 Russian guy which is which is obviously
00:00:58.249 true
00:00:59.059 I'm also our guitar player guitar player
00:01:02.570 and although I do not perform publicly I
00:01:05.810 really like to do that thing
00:01:08.630 so I'm also a son a husband and a father
00:01:11.540 and mostly I'm a yak shaver at Rapid
00:01:15.200 River software for the last one and a
00:01:16.610 half year so well I was learning to
00:01:20.840 shave yaks with a team of experienced
00:01:24.020 developers I really thankful to all the
00:01:27.500 guys from Rapid River software because
00:01:29.180 they really taught me to be cool and act
00:01:32.210 cool in every critical situation so here
00:01:36.380 I am
00:01:36.680 acting cool as hell while shooting
00:01:38.630 dangerous Rapids actually I just fell
00:01:41.810 asleep so on one of our project we had a
00:01:47.990 challenge so which can be basically
00:01:52.160 explained by the words that we had about
00:01:54.800 for source databases which contains web
00:01:58.490 events from the front end of different
00:02:00.680 sites and some related information and
00:02:03.350 we had to organize the ETL data flows
00:02:06.980 and processes and post calculation
00:02:10.039 processes and put over data to the
00:02:12.170 source database which called here is a
00:02:14.510 web statistics database so we had to
00:02:17.360 find the solution
00:02:18.400 we had to implement some magic driven
00:02:20.230 thing with all the unicorns and rainbows
00:02:22.239 inside and we came to the Ipoh of our
00:02:26.829 decision because we had no idea about
00:02:29.860 the you know underneath platform yeah
00:02:34.680 what should I say about hard decisions
00:02:37.769 so after all the hesitation we decided
00:02:42.909 to go with a yeah rails application wire
00:02:46.689 rails well because we wolf it definitely
00:02:50.560 right so I'm just kidding rails because
00:02:54.030 there are a lot of guys in Rapid River
00:02:56.260 software it's not me but a lot of them
00:02:59.980 can really cook rails in a perfect way
00:03:03.609 so we decided to go with that and yeah I
00:03:07.120 have this slide version for Malaysia
00:03:09.129 thanks team for the idea so it's a rails
00:03:12.010 app can LA because we're in Malaysia and
00:03:16.170 okay everyone knows how do you know
00:03:20.709 usual race application or in a race
00:03:22.930 implication for the single database
00:03:24.190 looks like we have the models in app
00:03:26.680 folder we have a database yeah mold file
00:03:28.959 in config folder we have a DB folder
00:03:31.690 with all the things set it in like all
00:03:33.940 the migration stuff seeds file and
00:03:36.280 schema right so you all should know that
00:03:40.900 right but what about rails application
00:03:43.810 with multiple it is we really felt
00:03:46.840 ourselves as a tumbleweed over the road
00:03:49.569 because no one has no idea of what
00:03:52.810 should we do so as a team of experienced
00:03:55.569 developers we decided to go with a most
00:03:57.970 obvious way to solve this problem and
00:04:00.540 use mystics box so we call the mr.
00:04:04.000 mystics and he said hey mr. musics look
00:04:06.519 at me and we asked him hey mr. physics
00:04:09.310 could you please explain us how should
00:04:11.919 we do how should we do handle in
00:04:14.919 multiple databases in our rails
00:04:16.630 application and he say ok as long as
00:04:19.449 it's not to teach you play golf it works
00:04:23.199 for me so here advices of mr. musics so
00:04:27.639 first of all he advised us to manage
00:04:29.349 that the bass connection
00:04:30.669 and organize them the way that each
00:04:33.250 database connection information should
00:04:34.719 be stored in a dedicated config in the
00:04:37.569 config database DB name llamo file each
00:04:41.139 database should have its own config so
00:04:44.979 the next point is we should initialize
00:04:48.039 DB conflicts at the in rails application
00:04:51.430 in the initialization step so basically
00:04:54.340 here is the piece of code from our
00:04:57.879 databases initializer which simply
00:05:00.969 contains the you know reading all the
00:05:04.509 context for all databases we are
00:05:06.639 interested at and check if they has a
00:05:09.840 conflicts for a given environment I'm
00:05:13.000 sorry for the bunch of code the next
00:05:17.229 thing we should choose a default
00:05:19.389 database for our rails application
00:05:21.340 because although we have a different
00:05:23.349 databases there should be one of them
00:05:25.240 for holding over you know rails app
00:05:27.969 related things so for that purpose we
00:05:31.779 have a module in our app which name is
00:05:35.529 DB which has an added select current
00:05:38.289 database so this is its short
00:05:40.930 implementation what it does is simply
00:05:44.379 changes rails application conflict paths
00:05:46.659 so this is it next thing we should
00:05:50.860 organize the be files so each database
00:05:54.250 related stuff should take place in its
00:05:57.460 own folder inside DB folder so we have
00:06:01.449 organized it the way I just shown into
00:06:04.180 this slide so we have a code folder with
00:06:06.550 functions and views all the things that
00:06:09.159 store it into the database we have a
00:06:11.500 seats folder for all the seed files and
00:06:13.750 seats dot RB just includes all of them
00:06:17.439 and migrate folders and structure
00:06:21.009 folders so this should be clear next
00:06:24.370 thing we should organize our models
00:06:26.229 properly so each database should has its
00:06:29.229 own folder for its models and also we
00:06:32.770 should have a base abstract model for
00:06:35.259 each database so this is how it looks
00:06:38.800 like in model for database foodie B we
00:06:42.219 have a class AB
00:06:43.650 class which simply establish connection
00:06:46.860 to the proper database and we should you
00:06:50.639 know inherit all the models for that
00:06:52.650 database from this class next for our
00:06:57.090 comfortable usage of application we
00:06:59.190 should create a migration generators for
00:07:00.930 each database so we could simply use
00:07:04.460 them through a I shown at the bottom of
00:07:07.380 the slide like rails generate food DB
00:07:09.510 mail migration blah blah blah and it
00:07:11.400 will put the migration file to the right
00:07:13.110 folder inside the DB structure so also
00:07:17.729 we should set create a set of rake tasks
00:07:20.820 for each database like the standart once
00:07:23.280 dropped readme migrate reset so all the
00:07:26.940 things that we supposed to do with
00:07:28.919 databases right so here we did the trick
00:07:32.370 we simply changed we simply change the
00:07:37.350 database for our rails application and
00:07:40.380 invoke all the rail tasks for for the
00:07:43.560 default database from the you know
00:07:45.419 single DB approach well you can
00:07:48.210 customize each method for each database
00:07:50.729 in the way that will work for you so
00:07:55.590 yeah and the last thing to do is just to
00:07:59.160 begin to use that rake tasks and be
00:08:02.580 quite happy because this is the final
00:08:04.650 step and from this point of time
00:08:06.800 everything should works at least
00:08:09.030 everything works for us so this is the
00:08:14.280 things that remains out of scope of my
00:08:16.320 today talk because it should be a
00:08:17.760 blazing fast first of all it's a DB
00:08:20.310 specific things which means that we
00:08:24.000 probably have to custom customize some
00:08:26.639 rake tasks for Oracle case for my ul
00:08:29.849 case and so on and so forth with all of
00:08:32.400 them you know with all of the specific
00:08:35.130 stuff for each database we also have the
00:08:39.300 rake tasks for managing with a table
00:08:41.339 partitions because we heavily use
00:08:45.529 partition since PostgreSQL because we
00:08:48.360 have a really huge databases and we also
00:08:51.839 have implemented wrapper
00:08:53.730 / PG copy - which implemented as a bot
00:08:59.010 loader which works blazing fast because
00:09:01.590 copy itself works blazing fast and also
00:09:04.610 we have implemented the concepts of DB
00:09:08.250 code versioning so we have a migrations
00:09:11.220 for each stored procedure for each sole
00:09:14.490 function and for each view and it's just
00:09:18.600 the thing about you know code
00:09:21.360 organization so if you will be
00:09:22.980 interested in some of these topics just
00:09:24.750 fired me say hi and I will try to
00:09:27.240 explain you all the things that I know
00:09:29.210 so this is it and yeah the thing that I
00:09:33.180 should mention that is not in this
00:09:35.250 presentation while working on this
00:09:37.470 project we with another developers
00:09:40.680 decided to make a gem
00:09:42.570 which could help you to set up multiple
00:09:44.970 databases with your rails project so
00:09:47.490 currently we're on the early stages of
00:09:49.770 making this gem but be sure when it's
00:09:53.460 done we will advertise it as much as
00:09:55.830 possible because it really worked well
00:09:58.170 for us and if you if it will help at
00:10:01.590 least one other guy it will be just
00:10:03.690 perfect and we will be happy so this is
00:10:08.760 my credentials viniq history is you know
00:10:12.090 is like an elder one so don't ask me I
00:10:15.450 know that 404 stands for not found thank
00:10:18.510 you guys and it was a lightning talk it
00:10:22.080 was a nice to be here and after every
00:10:26.520 lightning it comes the founder so let's
00:10:28.680 make some thunder guys for all of you
00:10:30.720 for the Ruby community because you're
00:10:33.210 really awesome and I really happy to be
00:10:36.180 a part of it thank you
Explore all talks recorded at RubyConf MY 2017
+16