Slide 1

Slide 1 text

Breno Oliveira @brenoholiveira [email protected] B R E N O O L I V E I R A Tech Leader at Moip ElasticSearch Turbinando sua aplicação PHP

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

+ 86k de contas vendendo

Slide 4

Slide 4 text

+ 100K de pedidos por dia

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

VENDAS AUMENTAM VENDEDOR QUER SABER

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

Ordenação FullText Search Paginação Agregação Filtros

Slide 10

Slide 10 text

REALTIME

Slide 11

Slide 11 text

E O SEU SERVER DEPOIS DE VÁRIOS F5 …

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

NOSSA VONTADE É DE …

Slide 15

Slide 15 text

SEU CLIENTA VAI…

Slide 16

Slide 16 text

PHP + MySQL PHP + postgresql PHP + nosql PHP + ????

Slide 17

Slide 17 text

CASO DE USO

Slide 18

Slide 18 text

+

Slide 19

Slide 19 text

502 200 304

Slide 20

Slide 20 text

+

Slide 21

Slide 21 text

Aplicação MOIP Base de Dados Master

Slide 22

Slide 22 text

Single Term SELECT * FROM pedidos p INNER JOIN cliente ON p.cliente_id = c.id WHERE p.descricao LIKE "%superman%"

Slide 23

Slide 23 text

multi Term SELECT * FROM pedidos p INNER JOIN cliente ON p.cliente_id = c.id WHERE p.descricao LIKE "%superman%" OR p.descricao LIKE "%batman%"

Slide 24

Slide 24 text

ORDER ARRGH!!!! SELECT * FROM pedidos p INNER JOIN cliente ON p.cliente_id = c.id WHERE p.descricao LIKE "%superman%" OR p.descricao LIKE "%batman%" ORDER BY ??

Slide 25

Slide 25 text

PROBLEMAS • Difícil manter • Muitos JOINS • Lento

Slide 26

Slide 26 text

.Primeiro REFACTOR

Slide 27

Slide 27 text

MICRO SERVIÇO + BANCO denormalizado

Slide 28

Slide 28 text

APLICAÇÃO MOIP Base de Dados Master REPORTS API Base de Dados denormalizada

Slide 29

Slide 29 text

MELHORIAS • Micro serviço • Concorrência no banco de dados • No more JOINS • Full-Text Search • Melhorias de desempenho

Slide 30

Slide 30 text

PROBLEMAS • Periodos longos • Filtros • Lentidão

Slide 31

Slide 31 text

.SEgundo REFACTOR

Slide 32

Slide 32 text

MICRO SERVIÇO + Banco colunar

Slide 33

Slide 33 text

APLICAÇÃO MOIP Base de Dados Master REPORTS API

Slide 34

Slide 34 text

MELHORIAS • Consultas significantemente mais rápidas

Slide 35

Slide 35 text

PROBLEMAS • BULK INSERTs Too Slow

Slide 36

Slide 36 text

.terceiro REFACTOR

Slide 37

Slide 37 text

MICRO SERVIÇO + Elasticseach

Slide 38

Slide 38 text

APLICAÇÃO MOIP Base de Dados Master REPORTS API Elasticsearch

Slide 39

Slide 39 text

MELHORIAS • Bulk INSERT • Desempenho • Aggregations • Scalable • Redundancy

Slide 40

Slide 40 text

.Atualmente

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

Last Black Friday

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

.não é banco da dados

Slide 47

Slide 47 text

.nem nosql I T ' ’ N O T K E Y- VA L U E S T O R A G E

Slide 48

Slide 48 text

.Escalável

Slide 49

Slide 49 text

.Alta resiliencia

Slide 50

Slide 50 text

.restful api

Slide 51

Slide 51 text

.orientado a documentos

Slide 52

Slide 52 text

.full-text search

Slide 53

Slide 53 text

.free schema

Slide 54

Slide 54 text

.Eventualmente consistente

Slide 55

Slide 55 text

.apache lucene

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

.Conceitos básicos Elasticsearch

Slide 58

Slide 58 text

MySQL Elasticsearch Database Index Table Type Column Field Schema Mapping Partition Shard

Slide 59

Slide 59 text

curl -XPUT http://localhost:9200/tips/tip/1 -d '{"tip": "Using Elasticsearch in Production", "tags": ["protip","success"]} ' Endpoint Index Type Document ID Document { "_index": "tips", "_type": "tip", "_id": "1", "_version": 1, "created": true } Response

Slide 60

Slide 60 text

$ curl http://localhost:9200/tips/tip/1?pretty { "_index": "tips", "_type": "tip", "_id": "1", "_version": 1, "found": true, "_source": { "tip": "Using Elasticsearch in Production", "tags": [ "protip", "success" ] } }

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

VAGRANT

Slide 63

Slide 63 text

$ git clone [email protected]:brenooliveira/elastic-php-demo.git $ cd elastic-php-demo $ vagrant up —provision $ vagrant ssh vagrant@default:~/$

Slide 64

Slide 64 text

vagrant@default:$ curl http://localhost:9200 { "status" : 200, "name" : "Corruptor", "cluster_name" : "elasticsearch", "version" : { "number" : "1.5.2", "build_hash" : "62ff9868b4c8a0c45860bebb259e21980778ab1c", "build_timestamp" : "2015-04-27T09:21:06Z", "build_snapshot" : false, "lucene_version" : "4.10.4" }, "tagline" : "You Know, for Search" }

Slide 65

Slide 65 text

{ "require": { "elasticsearch/elasticsearch": "~2.0@beta" } } composer.json $ curl -s http://getcomposer.org/installer | php $ php composer.phar install --no-dev

Slide 66

Slide 66 text

build(); $params = array(); $params['index'] = 'tips'; $params['type'] = 'tip'; $params['id'] = '1'; $result = $client->get($params) ?>

exemplo1.php

              

Slide 67

Slide 67 text

Array ( [_index] => tips [_type] => tip [_id] => 1 [_version] => 1 [found] => 1 [_source] => Array ( [tip] => Using Elasticsearch in Production [tags] => Array ( [0] => protip [1] => success ) ) )

Slide 68

Slide 68 text

exemplo2.php build(); $params = [ 'index' => 'tips', 'type' => 'tip', 'id' => '2', 'body' => [ 'tip' => 'Using Marvel to get metrics from Elasticsearch', 'tags' => ['protip', 'metrics'] ] ]; // Documento indexado em tips/tip/2 $response = $client->index($params); ?>


              

Slide 69

Slide 69 text

exemplo3.php build(); for($i = 0; $i < 100; $i++) { $params['body'][] = [ 'index' => [ '_index' => 'jobs', '_type' => 'job', ] ]; $params['body'][] = [ 'title' => 'Job position '. $i, 'email' => 'email-'. $i . '@gmail.com' ]; } $responses = $client->bulk($params); ?>

Slide 70

Slide 70 text

exemplo4.php build(); $json = '{ "query" : { "match" : { "tags" : "protip" } } }'; $params = [ 'index'=> 'tips', 'type' => 'tip', 'body' => $json ]; $responses = $client->search($params); ?>


              

Slide 71

Slide 71 text

exemplo5.php build(); $params = [ 'index' => 'tips', 'type' => 'tip', 'id' => '3', 'body' => [ 'tip' => 'Wrong insert', 'tags' => ['missing', 'error', 'no_logs'] ] ]; $response_insert = $client->index($params); $params = [ 'index' => 'tips', 'type' => 'tip', 'id' => '3' ]; $responde_delete = $client->delete($params); ?>

Slide 72

Slide 72 text

exemplo6.php build(); $params = [ 'index' => 'tips', 'type' => 'tip', 'id' => '4', 'body' => [ 'tip' => 'Loren Ipsum', 'tags' => ['lorem'] ] ]; $response_insert = $client->index($params); $params = [ 'index' => 'tips', 'type' => 'tip', 'id' => '4', 'body' => [ 'doc' => [ 'text' => 'lipsum generator' ] ] ]; $response_update = $client->update($params); $params = [ 'index' => 'tips', 'type' => 'tip', 'id' => '4' ]; $response_search = $client->get($params); ?>

Slide 73

Slide 73 text

exemplo7.php build(); $json = '{ "my-suggestion" : { "text" : "Elastiscearch", "term" : { "field" : "tip" } } }'; $params = [ 'index' => 'tips', 'body' => $json ]; $response = $client->suggest($params); ?>

Slide 74

Slide 74 text

exemplo8.php build(); $json = '{ "query" : { "more_like_this" : { "like_text" : "In production", "min_term_freq" : 1, "min_doc_freq" : 1 } } }'; $params = [ 'index' => 'tips', 'type' => 'tip', 'body' => $json ]; $response = $client->search($params); ?>

Slide 75

Slide 75 text

.Vamos falar de arquitetura

Slide 76

Slide 76 text

.Como devo sincronizar mysql & Elasticsearch

Slide 77

Slide 77 text

E-commerce app Base de Dados Elasticsearch Cadastrar novo produto

Slide 78

Slide 78 text

E-commerce app Base de Dados Elasticsearch rabbitmq Publish Message php worker Consume Message & Sync Cadastrar novo produto

Slide 79

Slide 79 text

.Lessons learned

Slide 80

Slide 80 text

Utilize mais mappings

Slide 81

Slide 81 text

Em produção use alias

Slide 82

Slide 82 text

No content