Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Database Implementation in Microservices

Database Implementation in Microservices

Today, 91%(of Global Companies) are using or have plans to use microservices.
Microservices motivates 82%(of agility) & 78%(of scalability) in digital business impact.
There are many people say that microservices is too hard to be implemented especially related to the database. Is that true? Let the code answer it because The Code never Lies ! You or I? Sometimes, We could Lie.

ronysetyawan

May 07, 2020
Tweet

Other Decks in Programming

Transcript

  1. Description Today, 91%(of Global Companies) are using or have plans

    to use microservices. Microservices motivates 82%(of agility) & 78%(of scalability). There are many people say that microservices is too hard to be implemented especially related to the database. Is that true? Let the code answer it because The Code never Lies! You or I ? Sometimes, we could lie.
  2. Bibliography Newman, Sam. (2015). Building Microservices. O’Reilly Media. Bas, Len.,

    Clements, Paul., Kazman, Rick. (2013). Software Architecture in Practice. Pearson Education, Inc. Martin, Robert C. (2009). Clean Code : A Handbook of Agile Software Craftsmanship.Pearson Education, Inc. LightStep. (2018). New Research Reveals Record Growth in Microservices Is Disrupting the Operational Landscape. globenewswire.com/news-release/2018/05/02/1494613/0/en/New-Research-Reveals-Record-G rowth-in-Microservices-Is-Disrupting-the-Operational-Landscape.html
  3. What is Microservice ? User experience Physical Computing architectural style

    that structures an application as a collection of services that are : • Small as Services • Focused on Doing One Thing Well • Autonomous • Support Technology Heterogeneity • Independently Deployable • Loosely Coupled • Decentralized
  4. What is Database ? User experience Physical Computing • Organized

    collection of data • Electronically from a computer system • Using formal design and modeling techniques • Easy to be Accessed, Managed, Updated, Deleted
  5. Database - 1 CREATE DATABASE BLANJACOM; CREATE TABLE `Customers` (

    `id` int PRIMARY KEY AUTO_INCREMENT, `name` varchar(255), `phone_number` varchar(255), `email` varchar(255), `balance` int, `wallet` ENUM ('linkaja', 'tmoney', 'ovo') ); CREATE TABLE `Orders` ( `id` int PRIMARY KEY AUTO_INCREMENT, `customer_id` int, `status` ENUM ('pending', 'refund', 'approved', 'completed'), `created_at` datetime DEFAULT (now()), `updated_at` datetime DEFAULT (now()) ); ALTER TABLE `Orders` ADD FOREIGN KEY (`customer_id`) REFERENCES `Customers` (`id`);
  6. Database - 2 INSERT INTO `Customers` (`id` ,`name`,`phone_number`,`email`,`balance`,`wallet`) VALUES (1,

    "Hepta Yuniarita", "08123456789", "[email protected]", 500000, "linkaja"); INSERT INTO `Customers` (`id` ,`name`,`phone_number`,`email`,`balance`,`wallet`) VALUES (2, "Komang Budi Aryasa", "08198765432", "[email protected]", 1000000, "linkaja"); INSERT INTO `Customers` (`id` ,`name`,`phone_number`,`email`,`balance`,`wallet`) VALUES (3, "Andri Qiantori", "08123459876", "[email protected]", 100000, "ovo"); INSERT INTO `Orders` (`id` ,`customer_id`,`status`) VALUES (1, 1, "pending"); INSERT INTO `Orders` (`id` ,`customer_id`,`status`) VALUES (2, 2, "completed"); INSERT INTO `Orders` (`id` ,`customer_id`,`status`) VALUES (3, 3, "pending");
  7. Database - 3 var es = require('eventstore')({ type: 'mongodb', host:

    'localhost', port: 27017, dbName: 'eventstore', eventsCollectionName: 'events', snapshotsCollectionName: 'snapshots', transactionsCollectionName: 'transactions', timeout: 10000 maxSnapshotsCount: 3 authSource: 'authedicationDatabase', username: 'blanjacom', password: 'secret' url: 'mongodb://user:pass@host:port/db?opts positionsCollectionName: 'positions' }); es.useEventPublisher(function(evt) { // bus.emit('event', evt); });
  8. Database - 4 es.getFromSnapshot({ aggregateId: 'myAggregateId', aggregate: 'person', // optional

    context: 'hr' // optional }, function(err, snapshot, stream) { var snap = snapshot.data; var history = stream.events; // events history from given snapshot // myAggregate.loadSnapshot(snap); // myAggregate.loadFromHistory(history); }); es.useEventPublisher(function(evt, callback) { bus.sendAndWaitForAck('event', evt, callback); }); es.defineEventMappings({ id: 'id', commitId: 'commitId', commitSequence: 'commitSequence', commitStamp: 'commitStamp', streamRevision: 'streamRevision' });
  9. Database - 5 es.getEventStream('streamId', function(err, stream) { stream.addEvent({ my: 'event'

    }); stream.addEvents([{ my: 'event2' }]); stream.commit(); stream.commit(function(err, stream) { console.log(stream.eventsToDispatch); // this is an array containing all added events in this commit. }); }); es.store.getLastEvent({ aggregateId: txs[0].aggregateId, aggregate: txs[0].aggregate, // optional context: txs[0].context // optional }, function (err, lastEvent) { if(err) { console.log('ohhh :-('); return; } es.store.repairFailedTransaction(lastEvent, function (err) { if(err) { console.log('ohhh :-('); return; } console.log('event proceed'); }); }); });
  10. Shared Database BLANJA ORDER API BLANJA ORDER mSERVICE BLANJA CUSTOMER

    API BLANJA CUSTOMER mSERVICE BLANJA SINGLE LARGE DATABASE BLANJA LEGACY SYSTEM BLANJA LEGACY API
  11. Database Per Service User experience Physical Computin g BLANJA ORDER

    API BLANJA ORDER microSERVICE User experience BLANJA CUSTOME R API BLANJA CUSTOMER microSERVICE BLANJA ORDER DATABASE BLANJA CUSTOMER DATABASE
  12. SAGA - Choreography User experience Physical Computing BLANJA ORDER API

    BLANJA ORDER mSERVICE User experience BLANJA CUSTOMER API BLANJA CUSTOMER mSERVICE BLANJA ORDER DATABASE BLANJA CUSTOMER DATABASE Order Channel Customer Channel Publish order event Subscribe order event Publish customer event Subscribe customer event
  13. CQRS User experience Physical Computin g BLANJA ORDER API BLANJA

    ORDER mSERVICE User experience BLANJA CUSTOMER API BLANJA CUSTOMER mSERVICE BLANJA ORDER DATABASE BLANJA CUSTOMER VIEW DATABASE Order Channel Customer Channel BLANJA ORDER VIEW DATABASE BLANJA CUSTOMER DATABASE Publish order event Subscribe customer event Subscribe order event Publish customer event
  14. EVENT SOURCING User experienc e Physica l Computi ng BLANJA

    ORDER API BLANJA ORDER microSERVICE User experienc e BLANJA CUSTOMER API BLANJA CUSTOMER microSERVICE BLANJA ORDER VIEW DATABASE BLANJA CUSTOMER VIEW DATABASE Order Channel Customer Channel BLANJA ORDER SNAPSHOT DATABASE BLANJA CUSTOMER DATABASE BLANJA ORDER EVENT DATABASE BLANJA ORDER DATABASE BLANJA CUSTOMER SNAPSHOT DATABASE BLANJA CUSTOMER EVENT DATABASE Publish order event Subscribe order event Publish customer event Subscribe customer event