Microservices

Microservices: Design Principles Introduction icon-angle-right 

What is a Service?

A service is a piece of software that provides functionality to another pieces of software.

what-is-a-service

 

 

 

 

 

 

 

  • Provide reusability of functionality.
  • A service can provide functionality to any application.
    • Such a web server, mobile, or desktop application
  • A service can be use by another service
  • Service-oriented Architecture (SOA)
    • Instead of using package modules within each client application, we have a service which provide the same functionality to different client applications.
    • Allow for new services and application to use the same functionality, reusing it, in the future.
    • Allows for scale up our software when demand increasing.
      • i.e. Load balancer which have multiple copies of the same service on multiple servers. When demand increases, we increase the instance of the service running across servers.
  • As long as the signature of a service, contract and interface, doesn’t change when the service changes, we can upgrade our service without having to update our clients.
  • A service is stateless. When a request comes in, that instance of the service does not have to remember the previous request from that specific client.

Microservice Architecture

Traditional SOA resulted in monolithic services. You needed to know how to size a service. The micro sized services provide an efficiently, flexible, and high performance applications.

A microservice architecture is an application which is powered by multiple microservices, in which each microservice provides a set of functions (or related functions) to a specific part of the application.

A microservice have a single focus. It does one thing only and it does it well.

Microservice architecture are used in lightweight and quick communication mechanisms (such as REST) between client and services; as well as, service to service.

A microservice needs technology agnostic API which mean to use an open communication so it doesn’t dictate the technology that the client needs to use.

At difference to a monolithic service where there is a central database used to share data between applications and services, in microservices architecture, each microservice has its own data storage.

Monolithic Service Example
monolithic
Microservice Example
microservice

A microservice is independently changeable. This means that we can upgrade or fix a microservice without forcing any changes to the clients or services.

A microservice needs to be independently deployable. You should be able to deploy a microservice without having to deploy anything else.

dialog-warning-2 Note: You may need a centralized tooling for management of microservices.

 

The Monolithic System

Typical Enterprise Application:

  • Large website with all modules packaged in together into one package.
  • A service which talks to a website in which the service itself is a large service with all modules packaged together as one executable.
  • As you add features and stuff, your application keeps growing.
  • There is no restriction in size; as well as, there is no division.
  • There is always one package which basically contains everything.
  • Large code base which makes it harder and time consuming to add new functionality.
  • Code can become intertwined making difficult to make changes without creating side effects to other parts of the systems.
  • Testing can be challenging.
  • Features may be in deep in the system that they can be utilized for external use.
  • Commonly stuck with one technology stack which may bring restriction such as implementing some new technology that may be different to the current
  • technology stack.
  • The system is less competitive because it restrict the adoption of new technology.
  • Large package with high levels of coupling which means that changes may produce a ripple effect (side effects). This coupling happens in all levels such as modules, services, and objects.
  • A failure in one part of the system may affect the whole system.
  • Duplication of the whole may require scaling.
  • The system may requires a long time to compile. The larger the system gets, the longer the compilation time required.
  • Any changes, no matter how minor, may requires a complete rebuild.
Monolithic System Example

Emergence of Microservices

  • Need to respond to change quickly.
  • Can split a large system into parts that can be upgraded and enhanced individually
  • The entire system will not break if one part breaks.
  • It allows for business domain-driven design.
  • It takes advantage of automated test tools.
  • Since transactions are distributed, each transaction will be processed by multiple services before it’s completed.
    • The integration between those services requires to be tested.
    • Instead of testing these microservices manually, we can automated the test.
  • Release and deployment of microservices may become complex; however, there are tools available to easier the work.
  • We can host microservices using on-demand technology such using virtual machines to host our microservice.
    • Physical servers are no longer required in order to deploy our software.
    • On-demand hosting is simpler in these days with cloud services available.
    • We can clone these virtual machines.
  • We can move our microservice from one technology stack to another technology stack.
  • It allows for asynchronous communication technology.
    • The distributed transaction do not have to wait for other services to complete their task.
  • We have simpler server side and client side technology, as well as many open communication protocols available which allows communication between different technology stacks.

Key Benefits

  • Shorter development times
  • The split up of the system allows to work individually in one part or assign different parts to different people and/or teams.
  • Due the size and the concept of single focus, an individual or team has less to worry about in terms of scope.
  • They only need to focus on their scope and not the whole system
    • As long as the contracts (interfaces) between the services remain.
  • Developers can rework, change and deploy individual components without affecting the system (or need to redeploy everything) since the services are loosely coupled.
  • Deployment is more reliable and faster.
  • Allows for shorter development times, reliable and faster deployment; therefore, frequent updates.
    • Frequent updates provide a competitive edge.
  • It allows us to decouple changeable parts.
  • Increases security since each microservice has its own database and its own security mechanism.
    • Data is distributed which makes the data more secure.
    • The monolithic system may have one database. By hacking that one system, you can gain access to the data.
  • Quicker to identify which service is having the problem.
  • Highly scalable and better performance.
    • Scale part individually instead of the whole system.
  • Easier to change ownership of each microservice.
  • Each microservice have their own database and code base.
  • Enables distributed teams.

 

 

 

 

Share

Architectural Principles

Service Autonomy Principles

  • One service should not depend and/or rely on any other service to do its work.
    • Any service failing should not affect other services.
  • Any external service should be considered unstable and be expected to fail
    • If any external service fails then there should not be a cascade of these failures into our service, we should instead degrade the functionality so that such failure doesn’t become catastrophic to our service.
  • Each service update should be dependent without requiring any other service to coordinate updates as well. In other words, the update of any service should affect the rest of services.
  • Services should have the ability to be changes and deployed any time

No Coordinated Transactions Principles

  • One service should not be forced to enroll in a transaction which is owned by another service.
    • The service should not rely on other services
    • The service should not be allowed to do complex transactions, be involved with multiple services changing continuously of states, and/or interacting with multiple services and/or objects.

Microchanges Priciples

  • ACID 2.0 model.
    • Commutative. Idempotent. Distributed.
    • Achieving high throughput by altering our data model.
  • Create as small of database transactions as is logical.
  • Change as little as possible.
  • As events, capture user-intent rather than data objects

Change Tolerant Principles

  • Defensive coding.
  • How tolerant is your service in respect to other services changing?
    • How the service will react to failures?
    • How the service will react with changes in another service’s API?
    • How the service will react with changes in events and/or messages it receive?

Articles Related

  • ACID 2.0 Article:

    Jimmi Bogard. “ACID 2.0 in action.” Los Techies. . (2013): . . https://lostechies.com/jimmybogard/2013/06/06/acid-2-0-in-action/

Share

Quintus: Notes and Code Examples (in CodePen.io)

My Notes and Code Examples (in CodePen.io)

Recently, I started to use the website CodePen in order to share code examples; as well as, making notes of random things I am learning in my speared time.

Currently, I am experimenting with this game framework/library called Quintus

The order goes from the newest, down to the oldest.
In this way, you don’t have to scroll all the way down to the final result.

Quintus JS HTML5

dialog-warning-2Warning: For these notes/examples, I am using external url resources (such as Open Source sprites). If any of those resources are missing, then the code examples will not 

Resources

exl-br2-sizesArt Author: grenaderifle

Url: http://s23.photobucket.com/user/grenaderifle/media/EXL-BR2-sizes.gif

spaceship Art Author: thomaswp

Url: http://opengameart.org/sites/default/files/styles/medium/public/spaceship.png

m484bulletcollection1Art Author: Master484

Url: http://opengameart.org/sites/default/files/M484BulletCollection1.png

 

Art Author: Cuzco
Url: http://opengameart.org/sites/default/files/bg5.jpg

spaceship_tutArt Author: Skorpio
Url: http://opengameart.org/sites/default/files/styles/medium/public/Spaceship_tut.png

Player: Enemies: Image + Animation

See the Pen Quintus: Player: Enemies: Image + Animation on CodePen 1

 

Player: Shots: Throttling

See the Pen Quintus: Player: Shots: Throttling on CodePen 1

Player: Shots: Refactoring

See the Pen Quintus: Player: Shots: Refactoring on CodePen 1

 

Player: Gun: Refactoring

See the Pen Quintus: Player: Gun: Refactoring on CodePen 1

 

Player: Gun: Shots Movement

See the Pen Quintus: Player: Gun: Shots Movement on CodePen 1

 

Player: Gun

See the Pen Quintus: Player: Gun on CodePen 1

 

Player: Shot

See the Pen Quintus: Player: Shot on CodePen 1

 

Control: Customization

See the Pen Quintus: Control: Customization on CodePen 1

Scene Module

See the Pen Quintus: Scene Module on CodePen 1

 

Sprites: Player: Movement: Boundaries

See the Pen Quintus: Sprites: Player: Movement: Boundaries on CodePen 1

 

Sprites: Player: Movement

See the Pen Quintus: Sprites: Player: Movement on CodePen 1

 

Sprites: Player: Input

See the Pen Quintus: Sprites: Player: Input on CodePen 1

 

Sprite: Player: Animation

See the Pen Quintus: Sprites: Player: Animation on CodePen 1

 

Sprite: Player

See the Pen Quintus: Sprites: Player on CodePen 1

 

Background

See the Pen Quintus: Background on CodePen 1

Basic

See the Pen Quintus: Basic on CodePen 1

 

Share

[IN CONSTRUCTION] Notes: Python: Part 2: Modularity

[THIS POST IS IN CONSTRUCTION]

In my spare time, I will be learning about Machine Learning, Artificial Intelligence, Data Science and other topics where Python is the interpreted language of choice.
Below are some fast notes I cleaned up to share to however is interested. Disclaimer: I am not responsible of the content or how you use the content. The responsibility fall on you.

We can import modules using the import keyword.

Note: Remember to use 4 spaces for indentation and use UTF-8 encoding with your editor when saving py files.

Functions

Fetch words. Assume this is in the words.py file:

from urllib.request import urlopen

def fetchWordsFromUrl(url): 
    with urlopen(url) as content:
        words = []
        for each_line in content:
            array = each_line.split()
            for each_word in array:
                words.append(each_word)
        return words

Using words.py file with python:

>>> import words
>>> words.fetchWordsFromUrl('https://wordpress.org/plugins/about/readme.txt')

Running as Script or Repo

import sys
from urllib.request import urlopen

def fetchWordsFromUrl(url): 
    with urlopen(url) as content:
        words = []
        for each_line in content:
            array = each_line.split()
            for each_word in array:
                words.append(each_word)
        return words

if __name__ == '__main__'
    fetchWordsFromUrl(sys.argv[1])

 

Other Functions

Bytes to Int 32:

def _bytes_to_int32(b):
    return b[0] |
              b([1] << 8) |
              b([2] << 16) |
              b([3] << 24)

Int 32 to Bytes:

def _int32_to_bytes(i):
    return bytes( (i & 0xff,
                         i >> 8 & 0xff,
                         i >> 16 & 0xff,
                         i >> 24 & 0xff) )

Even or Odd:

def isEvenOrOdd(n):
    return n % 2 == 0

Square:

def square(n):
    return n * n

Note: Code Execution.  

$ python3 code.py 99

 

Share

Notes: Python: Part 1

In my spare time, I will be learning about Machine Learning, Artificial Intelligence, Data Science and other topics where Python is the interpreted language of choice.
Below are some fast notes I cleaned up to share to however is interested. Disclaimer: I am not responsible of the content or how you use the content. The responsibility fall on you.

Exiting REPL: Ctrl-Z (Windows), Ctrl-D (Linux, OSX)

The Zen of Python, by Tim Peters: https://www.python.org/dev/peps/pep-0020/

Import

Here are some example of import plus a code so you can get a general idea of how thing are going to look:

>>> import math
>>> from urllib.request import urlopen
>>> with urlopen('https://wordpress.org/plugins/about/readme.txt') as content:
...        words = []
...        for each_line in content:
...            array = each_line.split()
...            for each_word in array:
...               words.append(each_word)
...
>>> 

 

Each element in the words array, is a byte therefore it will be in [b’word’, b’another’…] format; therefore, we can decode the bytes into strings:

>>> import math
>>> from urllib.request import urlopen
>>> with urlopen('https://wordpress.org/plugins/about/readme.txt') as content:
...        words = []
...        for each_line in content:
...            array = each_line.decode('utf-8').split()
...            for each_word in array:
...               words.append(each_word)
...
>>> 

 

“//” Integer division:

>>> import math
>>> n=5
>>> n=3
>>> fac(n) / (fac(k) * fac(n-k))
10.0
>> fac(n) // (fac(k) * fac(n-k))
10

(*) You can use math.factorial(n) if you don’t import. Using fac is the short of factorial.

The limit of results are not based on the type of variable but based on the limit of memory so a huge factorial such as factorial(13) it would give us a result without troubles.

 

Number of Characters. Example:

>>> len(str(fact(n)))

 

“**” Exponential:

 

>>> 2**31 – 1
2147483647

 

Scalar Type and Type Conversions:

 

>>> 10
10
>>> 0b10     # base 2
2
>>> 0o10    # base 8
8
>>> 0x10    #base 16
16
>>> int(3.5)  
3
>>> int(“500”)
500
>>> int(“10000”, 3)     # 0, 1, 2 of base 3?
81
>>> 3e8
300000000.0
>>> float(8)
8.0
>>> float(“nan”)
Nan
>>> Float(“-inf”)
-inf
>>> 2.0 + 1
3.0
>>> a = None
>>> a is None
True
>>> True
True
>>> bool(0)
False
>>> bool(99)
True
>>> bool(0.0)
False
>>> bool (0.2)
True
>>> bool(-1.1)
True
>>> bool([])              # [] Empty Array
False
>>> bool([1, 2, 3])
True
>>> bool(“”)
False
>>> bool(“Hello”)
True
>>> bool(“False”)    #You cannot convert from String to Boolean in this way
True

Float Type: IEEE-754 double precision (64-bit). 53 bits of binary precision. 15 to 16 bits of decimal precision.

None: Value of NoneType which is used to represent the absence of a value.

 

Conditional Statement

The expression, expr, is converted to bool as if by the bool() constructor

Note: Remember to indent 4 spaces

 

>>> if True
…       print(“Hello World!”)
Hello World!
>>> if bool(“eggs”):
…       print(“2 please”);
2 Please
>>> if “eggs”:
…        print(“3 please”)
3 please
>>> n = 10
>>> if n > 20:
…       print(“Yes”)
…    else:
…       print(“No”)
No
>>> if n > 20:
…       print(“Yes”)
…    elseif n == 10:
…       print(“Same”)
…    else:
…       print(“No”)
Same

If you are planning to do nested if statement, perhaps you considered to use “elif” instead of “else: [return + 4 spaces] if”

Reminder: Flat is better than nested

 

Loops:

White loop:

>>> n = 2
>>> while n != 0:
…         print (n)
…         n -= 1
2
1
>>> n = 2
>>> while n:  # While there is a number is true, when reaching 0 is false. Previous form is preferred
…        print(n)
…        n -= 1
2
1
>>> while True:
…         response = input()
…         if int(response) % 10 == 0:
…              break
…
13
67
100
>>>

Exiting Loop: Pressing Ctrl+C which is an exception

Keyword “break”: Terminates the innermost loop. Transferring execution to the first statement after the loop.

 

For Loops:

 

>>> names = [“Alejandro”, “Diego”, “Dado”]
>>> for name in names:
…         print(name)
…
Alejandro
Diego
Dado
>>> colors = { ‘red’: ‘0xFF0000’, ‘blue’:’0x0000FF’, ‘green’:’0x00FF00’}
>>> for color in colors:
…        print (color, colors[color])
…
red 16711680
blue 255
green 65280
>>> 

Strings

Str: Immutable sequence of Unicode code-points. Once you construct cannot be modified

Since quotes (‘) and double-quotes (“) are allowed but must be consistence

 

>>> “One” “Two”
‘OneTwo’

Note: Practicality beats purity “Beautiful text strings. Rendered in literal form. Simple elegance.”

String with New Lines:

  • Option 1: Multilines strings using triple double-quotes or triple single quotes (like in grails)

 

>>> “”” This is
…    a multiline string.
…    Deal with it!”””
‘This is\na multiline string\nDeal with it!’
>>> ‘’’ This is
…    a multiline string.
…    Deal with it!’’’
‘This is\na multiline string\nDeal with it!’

 

  • Option 2: Escape sequences
>>> m = ‘This is\na multiline string\nDeal with it!’
>>> m
‘This is\na multiline string\nDeal with it!’
>>> print(m)   #Print what are we representing
This is
a multiline string
Deal with it!
>>>

 

Note: Python has Universal Newlines which means you only need to use ‘\n’ and not worry of do ‘\r\n’ for windows or ‘\n’ for linux/osX. For more information: http://www.python.org/dev/peps/pep-0278/

Escape Strings:

>>> “This is a \”text\””
‘This is a “ text“’
>>> ‘This is a \’text\’’
“This is a ‘text’”
>>> ‘This is a \” crazy \’ text’
‘This is a “ crazy \’ text’
>>> s = “A \\ in the string to use”
>>> s
‘A \\ in the string to use’
>>> print (s)
A \ in the string to use
>>>

Note: More information about Escape string in http://docs.python.org/3/reference/lexical_analysis.html#Strings

 

Dealing with Windows Double BackSlashes using Raw Strings:

>>> file_path = r’C:\Windows\text.rft’
>>> file_path
‘C:\\Windows\\text.rft’
>>> print (file_path)
C:\Windows\text.rft
>>>

 

Create Strings from Other Types:

 

>>> str(500)
‘500’
>>> str(7.03e23)
‘7.03e+23’

 

String Common Operations

 

>>> s = “Alejandro”
>>> s[3]
‘j’
>>> type(s[3])
<class ‘str’>
>>> c = “alex”
>>> c.capitalize()
‘Alex’
>>> c
‘alex’

Note: There is no separate character type. The “characters” are simply one element strings.

Note: Use help for more information.

International Use: Python strings are Unicode. Default encoding is UTF-8 literals. Using ‘\u00e5’ for example, you can print the letter å. Also you can use ‘\xe5’ and ‘\345’ to get the same character.

 

Bytes

They are immutable sequence of bytes.

Byte literals: Prefix by letter b

b’data’

b”data”

Note: They support almost most operations of string

 

>>> d = b’One Two Bytes’
>>> d.split()
[b’One’, b’Two’, b’Bytes’]
>>> 

 

Converting Between Strings and Bytes:

Str => encode => bytes

Bytes => decode => Str

Bytes support a long range of encodings: http://docs.python.org/3/library/codecs.html#standard-encodings

 

>>> text = “å fotære”
>>> data = text.encode(“utf-8”)
>>> data
\xc3\xa5 fort\xc3\xa6re
>>> text2 = data.decode(“utf-8”)
>>> text2
å fotære
>>> text == text2
True
>>> 

(*) å is \xc3\xa5 and æ is \xc3\xa6

Note: Important to understand since information is transmitted in byte streams when dealing with files, network resources, and HTTP responses.

List

They are mutable sequences of objects

[a, b, c, d]

 

>>> [1, 2, 3]
[1, 2, 3]
>>> a = [“Alejandro”, “Godofredo”, “Carlstein]
>>> a[1]
‘Godofredo’
>>> a[1] = 10
>>> a
[‘Alejandro’, 10, ‘Carlstein’]
>>> b = []
>>> b.append(2.1)
>>> b.append(3.0)
>>> b
[2.1, 3.0]
>>> list (“Alex”)
[‘A’, ‘l’, ‘e’, ‘x’]
>>> c = [‘A’,
…            ‘b’,
…            ‘c’,] #We are allowed to add a coma and it would not create an issue

 

Dictionaries

Dict: Mutable mappings of keys to values

{key1 : value1, key2 : value2}

 

>>> m = { ‘firstname’ : ‘Alejandro, ‘lastname’ : ‘Carlstein’}
>>> m[‘firstname’]
‘Alejandro’
>>> m[‘firstname’] = ‘Godofredo’
>>> m
{ ‘firstname’ : ‘Godofredo, ‘lastname’ : ‘Carlstein’}
>>> m[‘middlename’] = ‘Alex’
>>> m
{ ‘firstname’ : ‘Godofredo, ‘lastname’ : ‘Carlstein’, ‘middlename’ : ‘Alex’}
>>> empty_map = {}

 

 

 

 

Share