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

Turbinando seus Microsserviços com Cache Distribuído via DevConf(Opensanca)

Turbinando seus Microsserviços com Cache Distribuído via DevConf(Opensanca)

Palestra feita por Jordi Henrique Silva, abordando sobre Cache Distribuído no Devconf 2023.

Sinopse:

Hoje é comum que boa parte das empresas estejam preocupadas em como aumentar o desempenho de seus serviços. Já que cada vez mais vivenciamos uma crescente demanda por features que processam grande volume de dados ou exigem integração com diversos sistemas. Uma das abordagens mais comuns para aumento de performance é armazenar temporariamente informações que são utilizadas frequentemente em um servidor de cache local a fim de acelerar o consumo das mesmas.

No entanto, o uso exclusivo de cache local pode levar a problemas como a inconsistência de dados quando trabalhamos em ambientes clusterizados, já que cada servidor não compartilham recursos entre-si. Então nesta talk iremos explorar como extrair o máximo das ferramentas de cachê distribuído e entender os tradeoffs de cada estratégia.

Opensanca

May 27, 2023
Tweet

More Decks by Opensanca

Other Decks in Programming

Transcript

  1. Turbinando Microsserviços com Distributed Cache

    View full-size slide

  2. Qual papel da Tecnologia no Negócio?

    View full-size slide

  3. Automatizar o processo operacional?

    View full-size slide

  4. Gerir os dados e oferecer inteligência no processo de decisão?

    View full-size slide

  5. Auxiliar o processo
    de crescimento do
    negócio

    View full-size slide

  6. Contextualizar

    View full-size slide

  7. RESTAURANTE
    MENU

    View full-size slide

  8. RESTAURANTE
    MENU
    R$ 30.00

    View full-size slide

  9. RESTAURANTE
    MENU
    R$ 30.00
    R$ 45.99

    View full-size slide

  10. RESTAURANTE
    MENU
    R$ 30.00
    R$ 45.99
    R$ 27.50

    View full-size slide

  11. RESTAURANTE
    MENU
    R$ 30.00
    R$ 45.99
    R$ 27.50
    R$ 65.98

    View full-size slide

  12. Formas de Pagamento

    View full-size slide

  13. Formas de Pagamento

    View full-size slide

  14. Formas de Pagamento

    View full-size slide

  15. Formas de Pagamento

    View full-size slide

  16. Como implementamos a
    consulta de formas de
    pagamento em comum com
    Spring Boot?

    View full-size slide

  17. @RestController
    public class ConsultaFormasDePagamentoController {
    @Autowired
    private UsuarioRepository usuarioRepository;
    @Autowired
    private RestauranteRepository restauranteRepository;
    @Autowired
    private FormaDePagamentoRepository formaDePagamentoRepository;
    @Transactional(readOnly = true)
    @GetMapping("/api/v1/formas-de-pagamento")
    public ResponseEntity> consultarFormasDePagamentoEmComum(
    @RequestParam(required = true) Long usuarioId,
    @RequestParam(required = true) Long restauranteId){
    if (usuarioRepository.existsById(usuarioId)) {
    throw new ResponseStatusException(UNPROCESSABLE_ENTITY, "Usuario não cadastrado");
    }
    if (restauranteRepository.existsById(restauranteId)) {
    throw new ResponseStatusException(UNPROCESSABLE_ENTITY, "Restaurante não cadastrado");
    }
    var formasPagamentoComum = formaDePagamentoRepository.findFormaDePagamentoEmComumEntre(usuarioId, restauranteId)
    .stream()
    .map(FormaDePagamentoResponse::new)
    .toList();
    return ResponseEntity.ok(formasPagamentoComum);
    }
    }

    View full-size slide

  18. @RestController
    public class ConsultaFormasDePagamentoController {
    @Autowired
    private UsuarioRepository usuarioRepository;
    @Autowired
    private RestauranteRepository restauranteRepository;
    @Autowired
    private FormaDePagamentoRepository formaDePagamentoRepository;
    @Transactional(readOnly = true)
    @GetMapping("/api/v1/formas-de-pagamento")
    public ResponseEntity> consultarFormasDePagamentoEmComum(
    @RequestParam(required = true) Long usuarioId,
    @RequestParam(required = true) Long restauranteId){
    if (usuarioRepository.existsById(usuarioId)) {
    throw new ResponseStatusException(UNPROCESSABLE_ENTITY, "Usuario não cadastrado");
    }
    if (restauranteRepository.existsById(restauranteId)) {
    throw new ResponseStatusException(UNPROCESSABLE_ENTITY, "Restaurante não cadastrado");
    }
    var formasPagamentoComum = formaDePagamentoRepository.findFormaDePagamentoEmComumEntre(usuarioId, restauranteId)
    .stream()
    .map(FormaDePagamentoResponse::new)
    .toList();
    return ResponseEntity.ok(formasPagamentoComum);
    }
    }

    View full-size slide

  19. @RestController
    public class ConsultaFormasDePagamentoController {
    @Autowired
    private UsuarioRepository usuarioRepository;
    @Autowired
    private RestauranteRepository restauranteRepository;
    @Autowired
    private FormaDePagamentoRepository formaDePagamentoRepository;
    @Transactional(readOnly = true)
    @GetMapping("/api/v1/formas-de-pagamento")
    public ResponseEntity> consultarFormasDePagamentoEmComum(
    @RequestParam(required = true) Long usuarioId,
    @RequestParam(required = true) Long restauranteId){
    if (usuarioRepository.existsById(usuarioId)) {
    throw new ResponseStatusException(UNPROCESSABLE_ENTITY, "Usuario não cadastrado");
    }
    if (restauranteRepository.existsById(restauranteId)) {
    throw new ResponseStatusException(UNPROCESSABLE_ENTITY, "Restaurante não cadastrado");
    }
    var formasPagamentoComum = formaDePagamentoRepository.findFormaDePagamentoEmComumEntre(usuarioId, restauranteId)
    .stream()
    .map(FormaDePagamentoResponse::new)
    .toList();
    return ResponseEntity.ok(formasPagamentoComum);
    }
    }

    View full-size slide

  20. @RestController
    public class ConsultaFormasDePagamentoController {
    @Autowired
    private UsuarioRepository usuarioRepository;
    @Autowired
    private RestauranteRepository restauranteRepository;
    @Autowired
    private FormaDePagamentoRepository formaDePagamentoRepository;
    @Transactional(readOnly = true)
    @GetMapping("/api/v1/formas-de-pagamento")
    public ResponseEntity> consultarFormasDePagamentoEmComum(
    @RequestParam(required = true) Long usuarioId,
    @RequestParam(required = true) Long restauranteId){
    if (usuarioRepository.existsById(usuarioId)) {
    throw new ResponseStatusException(UNPROCESSABLE_ENTITY, "Usuario não cadastrado");
    }
    if (restauranteRepository.existsById(restauranteId)) {
    throw new ResponseStatusException(UNPROCESSABLE_ENTITY, "Restaurante não cadastrado");
    }
    var formasPagamentoComum = formaDePagamentoRepository.findFormaDePagamentoEmComumEntre(usuarioId, restauranteId)
    .stream()
    .map(FormaDePagamentoResponse::new)
    .toList();
    return ResponseEntity.ok(formasPagamentoComum);
    }
    }

    View full-size slide

  21. @RestController
    public class ConsultaFormasDePagamentoController {
    @Autowired
    private UsuarioRepository usuarioRepository;
    @Autowired
    private RestauranteRepository restauranteRepository;
    @Autowired
    private FormaDePagamentoRepository formaDePagamentoRepository;
    @Transactional(readOnly = true)
    @GetMapping("/api/v1/formas-de-pagamento")
    public ResponseEntity> consultarFormasDePagamentoEmComum(
    @RequestParam(required = true) Long usuarioId,
    @RequestParam(required = true) Long restauranteId){
    if (usuarioRepository.existsById(usuarioId)) {
    throw new ResponseStatusException(UNPROCESSABLE_ENTITY, "Usuario não cadastrado");
    }
    if (restauranteRepository.existsById(restauranteId)) {
    throw new ResponseStatusException(UNPROCESSABLE_ENTITY, "Restaurante não cadastrado");
    }
    var formasPagamentoComum = formaDePagamentoRepository.findFormaDePagamentoEmComumEntre(usuarioId, restauranteId)
    .stream()
    .map(FormaDePagamentoResponse::new)
    .toList();
    return ResponseEntity.ok(formasPagamentoComum);
    }
    }

    View full-size slide

  22. @RestController
    public class ConsultaFormasDePagamentoController {
    @Autowired
    private UsuarioRepository usuarioRepository;
    @Autowired
    private RestauranteRepository restauranteRepository;
    @Autowired
    private FormaDePagamentoRepository formaDePagamentoRepository;
    @Transactional(readOnly = true)
    @GetMapping("/api/v1/formas-de-pagamento")
    public ResponseEntity> consultarFormasDePagamentoEmComum(
    @RequestParam(required = true) Long usuarioId,
    @RequestParam(required = true) Long restauranteId){
    if (usuarioRepository.existsById(usuarioId)) {
    throw new ResponseStatusException(UNPROCESSABLE_ENTITY, "Usuario não cadastrado");
    }
    if (restauranteRepository.existsById(restauranteId)) {
    throw new ResponseStatusException(UNPROCESSABLE_ENTITY, "Restaurante não cadastrado");
    }
    var formasPagamentoComum = formaDePagamentoRepository.findFormaDePagamentoEmComumEntre(usuarioId, restauranteId)
    .stream()
    .map(FormaDePagamentoResponse::new)
    .toList();
    return ResponseEntity.ok(formasPagamentoComum);
    }
    }

    View full-size slide

  23. @RestController
    public class ConsultaFormasDePagamentoController {
    @Autowired
    private UsuarioRepository usuarioRepository;
    @Autowired
    private RestauranteRepository restauranteRepository;
    @Autowired
    private FormaDePagamentoRepository formaDePagamentoRepository;
    @Transactional(readOnly = true)
    @GetMapping("/api/v1/formas-de-pagamento")
    public ResponseEntity> consultarFormasDePagamentoEmComum(
    @RequestParam(required = true) Long usuarioId,
    @RequestParam(required = true) Long restauranteId){
    if (usuarioRepository.existsById(usuarioId)) {
    throw new ResponseStatusException(UNPROCESSABLE_ENTITY, "Usuario não cadastrado");
    }
    if (restauranteRepository.existsById(restauranteId)) {
    throw new ResponseStatusException(UNPROCESSABLE_ENTITY, "Restaurante não cadastrado");
    }
    var formasPagamentoComum = formaDePagamentoRepository.findFormaDePagamentoEmComumEntre(usuarioId, restauranteId)
    .stream()
    .map(FormaDePagamentoResponse::new)
    .toList();
    return ResponseEntity.ok(formasPagamentoComum);
    }
    }

    View full-size slide

  24. @RestController
    public class ConsultaFormasDePagamentoController {
    @Autowired
    private UsuarioRepository usuarioRepository;
    @Autowired
    private RestauranteRepository restauranteRepository;
    @Autowired
    private FormaDePagamentoRepository formaDePagamentoRepository;
    @Transactional(readOnly = true)
    @GetMapping("/api/v1/formas-de-pagamento")
    public ResponseEntity> consultarFormasDePagamentoEmComum(
    @RequestParam(required = true) Long usuarioId,
    @RequestParam(required = true) Long restauranteId){
    if (usuarioRepository.existsById(usuarioId)) {
    throw new ResponseStatusException(UNPROCESSABLE_ENTITY, "Usuario não cadastrado");
    }
    if (restauranteRepository.existsById(restauranteId)) {
    throw new ResponseStatusException(UNPROCESSABLE_ENTITY, "Restaurante não cadastrado");
    }
    var formasPagamentoComum = formaDePagamentoRepository.findFormaDePagamentoEmComumEntre(usuarioId, restauranteId)
    .stream()
    .map(FormaDePagamentoResponse::new)
    .toList();
    return ResponseEntity.ok(formasPagamentoComum);
    }
    }

    View full-size slide

  25. @RestController
    public class ConsultaFormasDePagamentoController {
    @Autowired
    private UsuarioRepository usuarioRepository;
    @Autowired
    private RestauranteRepository restauranteRepository;
    @Autowired
    private FormaDePagamentoRepository formaDePagamentoRepository;
    @Transactional(readOnly = true)
    @GetMapping("/api/v1/formas-de-pagamento")
    public ResponseEntity> consultarFormasDePagamentoEmComum(
    @RequestParam(required = true) Long usuarioId,
    @RequestParam(required = true) Long restauranteId){
    if (usuarioRepository.existsById(usuarioId)) {
    throw new ResponseStatusException(UNPROCESSABLE_ENTITY, "Usuario não cadastrado");
    }
    if (restauranteRepository.existsById(restauranteId)) {
    throw new ResponseStatusException(UNPROCESSABLE_ENTITY, "Restaurante não cadastrado");
    }
    var formasPagamentoComum = formaDePagamentoRepository.findFormaDePagamentoEmComumEntre(usuarioId, restauranteId)
    .stream()
    .map(FormaDePagamentoResponse::new)
    .toList();
    return ResponseEntity.ok(formasPagamentoComum);
    }
    }

    View full-size slide

  26. @RestController
    public class ConsultaFormasDePagamentoController {
    @Autowired
    private UsuarioRepository usuarioRepository;
    @Autowired
    private RestauranteRepository restauranteRepository;
    @Autowired
    private FormaDePagamentoRepository formaDePagamentoRepository;
    @Transactional(readOnly = true)
    @GetMapping("/api/v1/formas-de-pagamento")
    public ResponseEntity> consultarFormasDePagamentoEmComum(
    @RequestParam(required = true) Long usuarioId,
    @RequestParam(required = true) Long restauranteId){
    if (usuarioRepository.existsById(usuarioId)) {
    throw new ResponseStatusException(UNPROCESSABLE_ENTITY, "Usuario não cadastrado");
    }
    if (restauranteRepository.existsById(restauranteId)) {
    throw new ResponseStatusException(UNPROCESSABLE_ENTITY, "Restaurante não cadastrado");
    }
    var formasPagamentoComum = formaDePagamentoRepository.findFormaDePagamentoEmComumEntre(usuarioId, restauranteId)
    .stream()
    .map(FormaDePagamentoResponse::new)
    .toList();
    return ResponseEntity.ok(formasPagamentoComum);
    }
    }

    View full-size slide

  27. @RestController
    public class ConsultaFormasDePagamentoController {
    @Autowired
    private UsuarioRepository usuarioRepository;
    @Autowired
    private RestauranteRepository restauranteRepository;
    @Autowired
    private FormaDePagamentoRepository formaDePagamentoRepository;
    @Transactional(readOnly = true)
    @GetMapping("/api/v1/formas-de-pagamento")
    public ResponseEntity> consultarFormasDePagamentoEmComum(
    @RequestParam(required = true) Long usuarioId,
    @RequestParam(required = true) Long restauranteId){
    if (usuarioRepository.existsById(usuarioId)) {
    throw new ResponseStatusException(UNPROCESSABLE_ENTITY, "Usuario não cadastrado");
    }
    if (restauranteRepository.existsById(restauranteId)) {
    throw new ResponseStatusException(UNPROCESSABLE_ENTITY, "Restaurante não cadastrado");
    }
    var formasPagamentoComum = formaDePagamentoRepository.findFormaDePagamentoEmComumEntre(usuarioId, restauranteId)
    .stream()
    .map(FormaDePagamentoResponse::new)
    .toList();
    return ResponseEntity.ok(formasPagamentoComum);
    }
    }

    View full-size slide

  28. @RestController
    public class ConsultaFormasDePagamentoController {
    @Autowired
    private UsuarioRepository usuarioRepository;
    @Autowired
    private RestauranteRepository restauranteRepository;
    @Autowired
    private FormaDePagamentoRepository formaDePagamentoRepository;
    @Transactional(readOnly = true)
    @GetMapping("/api/v1/formas-de-pagamento")
    public ResponseEntity> consultarFormasDePagamentoEmComum(
    @RequestParam(required = true) Long usuarioId,
    @RequestParam(required = true) Long restauranteId){
    if (usuarioRepository.existsById(usuarioId)) {
    throw new ResponseStatusException(UNPROCESSABLE_ENTITY, "Usuario não cadastrado");
    }
    if (restauranteRepository.existsById(restauranteId)) {
    throw new ResponseStatusException(UNPROCESSABLE_ENTITY, "Restaurante não cadastrado");
    }
    var formasPagamentoComum = formaDePagamentoRepository.findFormaDePagamentoEmComumEntre(usuarioId, restauranteId)
    .stream()
    .map(FormaDePagamentoResponse::new)
    .toList();
    return ResponseEntity.ok(formasPagamentoComum);
    }
    }

    View full-size slide

  29. @RestController
    public class ConsultaFormasDePagamentoController {
    @Autowired
    private UsuarioRepository usuarioRepository;
    @Autowired
    private RestauranteRepository restauranteRepository;
    @Autowired
    private FormaDePagamentoRepository formaDePagamentoRepository;
    @Transactional(readOnly = true)
    @GetMapping("/api/v1/formas-de-pagamento")
    public ResponseEntity> consultarFormasDePagamentoEmComum(
    @RequestParam(required = true) Long usuarioId,
    @RequestParam(required = true) Long restauranteId){
    if (usuarioRepository.existsById(usuarioId)) {
    throw new ResponseStatusException(UNPROCESSABLE_ENTITY, "Usuario não cadastrado");
    }
    if (restauranteRepository.existsById(restauranteId)) {
    throw new ResponseStatusException(UNPROCESSABLE_ENTITY, "Restaurante não cadastrado");
    }
    var formasPagamentoComum = formaDePagamentoRepository.findFormaDePagamentoEmComumEntre(usuarioId, restauranteId)
    .stream()
    .map(FormaDePagamentoResponse::new)
    .toList();
    return ResponseEntity.ok(formasPagamentoComum);
    }
    }

    View full-size slide

  30. @RestController
    public class ConsultaFormasDePagamentoController {
    @Autowired
    private UsuarioRepository usuarioRepository;
    @Autowired
    private RestauranteRepository restauranteRepository;
    @Autowired
    private FormaDePagamentoRepository formaDePagamentoRepository;
    @Transactional(readOnly = true)
    @GetMapping("/api/v1/formas-de-pagamento")
    public ResponseEntity> consultarFormasDePagamentoEmComum(
    @RequestParam(required = true) Long usuarioId,
    @RequestParam(required = true) Long restauranteId){
    if (usuarioRepository.existsById(usuarioId)) {
    throw new ResponseStatusException(UNPROCESSABLE_ENTITY, "Usuario não cadastrado");
    }
    if (restauranteRepository.existsById(restauranteId)) {
    throw new ResponseStatusException(UNPROCESSABLE_ENTITY, "Restaurante não cadastrado");
    }
    var formasPagamentoComum = formaDePagamentoRepository.findFormaDePagamentoEmComumEntre(usuarioId, restauranteId)
    .stream()
    .map(FormaDePagamentoResponse::new)
    .toList();
    return ResponseEntity.ok(formasPagamentoComum);
    }
    }

    View full-size slide

  31. @RestController
    public class ConsultaFormasDePagamentoController {
    @Autowired
    private UsuarioRepository usuarioRepository;
    @Autowired
    private RestauranteRepository restauranteRepository;
    @Autowired
    private FormaDePagamentoRepository formaDePagamentoRepository;
    @Transactional(readOnly = true)
    @GetMapping("/api/v1/formas-de-pagamento")
    public ResponseEntity> consultarFormasDePagamentoEmComum(
    @RequestParam(required = true) Long usuarioId,
    @RequestParam(required = true) Long restauranteId){
    if (usuarioRepository.existsById(usuarioId)) {
    throw new ResponseStatusException(UNPROCESSABLE_ENTITY, "Usuario não cadastrado");
    }
    if (restauranteRepository.existsById(restauranteId)) {
    throw new ResponseStatusException(UNPROCESSABLE_ENTITY, "Restaurante não cadastrado");
    }
    var formasPagamentoComum = formaDePagamentoRepository.findFormaDePagamentoEmComumEntre(usuarioId, restauranteId)
    .stream()
    .map(FormaDePagamentoResponse::new)
    .toList();
    return ResponseEntity.ok(formasPagamentoComum);
    }
    }

    View full-size slide

  32. @RestController
    public class ConsultaFormasDePagamentoController {
    @Autowired
    private UsuarioRepository usuarioRepository;
    @Autowired
    private RestauranteRepository restauranteRepository;
    @Autowired
    private FormaDePagamentoRepository formaDePagamentoRepository;
    @Transactional(readOnly = true)
    @GetMapping("/api/v1/formas-de-pagamento")
    public ResponseEntity> consultarFormasDePagamentoEmComum(
    @RequestParam(required = true) Long usuarioId,
    @RequestParam(required = true) Long restauranteId){
    if (usuarioRepository.existsById(usuarioId)) {
    throw new ResponseStatusException(UNPROCESSABLE_ENTITY, "Usuario não cadastrado");
    }
    if (restauranteRepository.existsById(restauranteId)) {
    throw new ResponseStatusException(UNPROCESSABLE_ENTITY, "Restaurante não cadastrado");
    }
    var formasPagamentoComum = formaDePagamentoRepository.findFormaDePagamentoEmComumEntre(usuarioId, restauranteId)
    .stream()
    .map(FormaDePagamentoResponse::new)
    .toList();
    return ResponseEntity.ok(formasPagamentoComum);
    }
    }

    View full-size slide

  33. @RestController
    public class ConsultaFormasDePagamentoController {
    @Autowired
    private UsuarioRepository usuarioRepository;
    @Autowired
    private RestauranteRepository restauranteRepository;
    @Autowired
    private FormaDePagamentoRepository formaDePagamentoRepository;
    @Transactional(readOnly = true)
    @GetMapping("/api/v1/formas-de-pagamento")
    public ResponseEntity> consultarFormasDePagamentoEmComum(
    @RequestParam(required = true) Long usuarioId,
    @RequestParam(required = true) Long restauranteId){
    if (usuarioRepository.existsById(usuarioId)) {
    throw new ResponseStatusException(UNPROCESSABLE_ENTITY, "Usuario não cadastrado");
    }
    if (restauranteRepository.existsById(restauranteId)) {
    throw new ResponseStatusException(UNPROCESSABLE_ENTITY, "Restaurante não cadastrado");
    }
    var formasPagamentoComum = formaDePagamentoRepository.findFormaDePagamentoEmComumEntre(usuarioId, restauranteId)
    .stream()
    .map(FormaDePagamentoResponse::new)
    .toList();
    return ResponseEntity.ok(formasPagamentoComum);
    }
    }

    View full-size slide

  34. @RestController
    public class ConsultaFormasDePagamentoController {
    @Autowired
    private UsuarioRepository usuarioRepository;
    @Autowired
    private RestauranteRepository restauranteRepository;
    @Autowired
    private FormaDePagamentoRepository formaDePagamentoRepository;
    @Transactional(readOnly = true)
    @GetMapping("/api/v1/formas-de-pagamento")
    public ResponseEntity> consultarFormasDePagamentoEmComum(
    @RequestParam(required = true) Long usuarioId,
    @RequestParam(required = true) Long restauranteId){
    if (usuarioRepository.existsById(usuarioId)) {
    throw new ResponseStatusException(UNPROCESSABLE_ENTITY, "Usuario não cadastrado");
    }
    if (restauranteRepository.existsById(restauranteId)) {
    throw new ResponseStatusException(UNPROCESSABLE_ENTITY, "Restaurante não cadastrado");
    }
    var formasPagamentoComum = formaDePagamentoRepository.findFormaDePagamentoEmComumEntre(usuarioId, restauranteId)
    .stream()
    .map(FormaDePagamentoResponse::new)
    .toList();
    return ResponseEntity.ok(formasPagamentoComum);
    }
    }

    View full-size slide

  35. @RestController
    public class ConsultaFormasDePagamentoController {
    @Autowired
    private UsuarioRepository usuarioRepository;
    @Autowired
    private RestauranteRepository restauranteRepository;
    @Autowired
    private FormaDePagamentoRepository formaDePagamentoRepository;
    @Transactional(readOnly = true)
    @GetMapping("/api/v1/formas-de-pagamento")
    public ResponseEntity> consultarFormasDePagamentoEmComum(
    @RequestParam(required = true) Long usuarioId,
    @RequestParam(required = true) Long restauranteId){
    if (usuarioRepository.existsById(usuarioId)) {
    throw new ResponseStatusException(UNPROCESSABLE_ENTITY, "Usuario não cadastrado");
    }
    if (restauranteRepository.existsById(restauranteId)) {
    throw new ResponseStatusException(UNPROCESSABLE_ENTITY, "Restaurante não cadastrado");
    }
    var formasPagamentoComum = formaDePagamentoRepository.findFormaDePagamentoEmComumEntre(usuarioId, restauranteId)
    .stream()
    .map(FormaDePagamentoResponse::new)
    .toList();
    return ResponseEntity.ok(formasPagamentoComum);
    }
    }

    View full-size slide

  36. @RestController
    public class ConsultaFormasDePagamentoController {
    @Autowired
    private UsuarioRepository usuarioRepository;
    @Autowired
    private RestauranteRepository restauranteRepository;
    @Autowired
    private FormaDePagamentoRepository formaDePagamentoRepository;
    @Transactional(readOnly = true)
    @GetMapping("/api/v1/formas-de-pagamento")
    public ResponseEntity> consultarFormasDePagamentoEmComum(
    @RequestParam(required = true) Long usuarioId,
    @RequestParam(required = true) Long restauranteId){
    if (usuarioRepository.existsById(usuarioId)) {
    throw new ResponseStatusException(UNPROCESSABLE_ENTITY, "Usuario não cadastrado");
    }
    if (restauranteRepository.existsById(restauranteId)) {
    throw new ResponseStatusException(UNPROCESSABLE_ENTITY, "Restaurante não cadastrado");
    }
    var formasPagamentoComum = formaDePagamentoRepository.findFormaDePagamentoEmComumEntre(usuarioId, restauranteId)
    .stream()
    .map(FormaDePagamentoResponse::new)
    .toList();
    return ResponseEntity.ok(formasPagamentoComum);
    }
    }

    View full-size slide

  37. @RestController
    public class ConsultaFormasDePagamentoController {
    @Autowired
    private UsuarioRepository usuarioRepository;
    @Autowired
    private RestauranteRepository restauranteRepository;
    @Autowired
    private FormaDePagamentoRepository formaDePagamentoRepository;
    @Transactional(readOnly = true)
    @GetMapping("/api/v1/formas-de-pagamento")
    public ResponseEntity> consultarFormasDePagamentoEmComum(
    @RequestParam(required = true) Long usuarioId,
    @RequestParam(required = true) Long restauranteId){
    if (usuarioRepository.existsById(usuarioId)) {
    throw new ResponseStatusException(UNPROCESSABLE_ENTITY, "Usuario não cadastrado");
    }
    if (restauranteRepository.existsById(restauranteId)) {
    throw new ResponseStatusException(UNPROCESSABLE_ENTITY, "Restaurante não cadastrado");
    }
    var formasPagamentoComum = formaDePagamentoRepository.findFormaDePagamentoEmComumEntre(usuarioId, restauranteId)
    .stream()
    .map(FormaDePagamentoResponse::new)
    .toList();
    return ResponseEntity.ok(formasPagamentoComum);
    }
    }

    View full-size slide

  38. SELECT f.*
    FROM usuario_forma_de_pagamento u
    INNER JOIN forma_de_pagamento f
    ON f.id = u.forma_de_pagamento_id
    INNER JOIN restaurante_forma_de_pagamento r
    ON f.id = r.forma_de_pagamento_id
    WHERE u.usuario_id = :usuarioId
    AND r.restaurante_id = :restauranteId

    View full-size slide

  39. Com a crescente dos dados
    e acessos simultâneos, é
    possível manter o tempo de
    resposta?

    View full-size slide

  40. O que é Caching?

    View full-size slide

  41. 980.999.810-49
    João Lucas
    Chave
    Valor

    View full-size slide

  42. 980.999.810-49
    João Lucas
    871.711.760-70
    Maria Fernanda
    Chave
    Valor

    View full-size slide

  43. 980.999.810-49
    João Lucas
    871.711.760-70
    Maria Fernanda
    549.254.550-79
    Arthur Souza
    Chave
    Valor

    View full-size slide

  44. 980.999.810-49
    João Lucas
    871.711.760-70
    Maria Fernanda
    549.254.550-79
    Arthur Souza
    682.248.740-95
    Antonella Alves
    Chave
    Valor

    View full-size slide

  45. Como Implementar a camada
    de cache em Aplicações
    Spring Boot?

    View full-size slide

  46. Criamos um CacheService?
    p/ gerenciar um HashMap

    View full-size slide

  47. E os acessos simultâneos?

    View full-size slide

  48. ConcurrentHashMap

    View full-size slide

  49. Servidor
    Heap Memory

    View full-size slide

  50. Database
    Servidor
    Heap Memory

    View full-size slide

  51. Database
    Servidor
    Heap Memory
    REQUEST

    View full-size slide

  52. Database
    Servidor
    Heap Memory
    REQUEST
    SELECT e.*
    FROM Entity e
    WHERE e.id=:id

    View full-size slide

  53. Database
    Servidor
    Heap Memory
    REQUEST
    SELECT e.*
    FROM Entity e
    WHERE e.id=:id
    RESPONSE

    View full-size slide

  54. Database
    Servidor
    Heap Memory
    REQUEST
    RESPONSE
    SELECT e.*
    FROM Entity e
    WHERE e.id=:id
    RESPONSE

    View full-size slide

  55. Database
    Servidor
    Heap Memory

    View full-size slide

  56. Database
    Servidor
    Heap Memory
    Local Caching

    View full-size slide

  57. Database
    Servidor
    Heap Memory
    REQUEST
    Local Caching

    View full-size slide

  58. Database
    Servidor
    Heap Memory
    REQUEST
    SELECT e.*
    FROM Entity e
    WHERE e.id=:id
    Local Caching

    View full-size slide

  59. Database
    Servidor
    Heap Memory
    REQUEST
    SELECT e.*
    FROM Entity e
    WHERE e.id=:id
    RESPONSE
    Local Caching

    View full-size slide

  60. Database
    Servidor
    Heap Memory
    REQUEST
    SELECT e.*
    FROM Entity e
    WHERE e.id=:id
    RESPONSE
    Local Caching

    View full-size slide

  61. Database
    Servidor
    Heap Memory
    REQUEST
    RESPONSE
    SELECT e.*
    FROM Entity e
    WHERE e.id=:id
    RESPONSE
    Local Caching

    View full-size slide

  62. Database
    Servidor
    Heap Memory
    Local Caching

    View full-size slide

  63. Database
    Servidor
    Heap Memory
    REQUEST
    Local Caching

    View full-size slide

  64. Database
    Servidor
    Heap Memory
    REQUEST
    Local Caching
    Cache.get(key)

    View full-size slide

  65. Database
    Servidor
    Heap Memory
    REQUEST
    RESPONSE
    Local Caching
    Cache.get(key)

    View full-size slide

  66. Existe uma forma mais
    elegante?

    View full-size slide

  67. Spring Caching

    View full-size slide

  68. @EnableCaching
    @SpringBootApplication
    public class Application {
    public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
    }
    }

    View full-size slide

  69. @EnableCaching
    @SpringBootApplication
    public class Application {
    public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
    }
    }

    View full-size slide

  70. @RestController
    public class ConsultaFormasDePagamentoController {
    @Autowired
    private UsuarioRepository usuarioRepository;
    @Autowired
    private RestauranteRepository restauranteRepository;
    @Autowired
    private FormaDePagamentoRepository formaDePagamentoRepository;
    @Transactional(readOnly = true)
    @GetMapping("/api/v1/formas-de-pagamento")
    public ResponseEntity> consultarFormasDePagamentoEmComum(
    @RequestParam(required = true) Long usuarioId,
    @RequestParam(required = true) Long restauranteId){
    if (usuarioRepository.existsById(usuarioId)) {
    throw new ResponseStatusException(UNPROCESSABLE_ENTITY, "Usuario não cadastrado");
    }
    if (restauranteRepository.existsById(restauranteId)) {
    throw new ResponseStatusException(UNPROCESSABLE_ENTITY, "Restaurante não cadastrado");
    }
    var formasPagamentoComum = formaDePagamentoRepository.findFormaDePagamentoEmComumEntre(usuarioId, restauranteId)
    .stream()
    .map(FormaDePagamentoResponse::new)
    .toList();
    return ResponseEntity.ok(formasPagamentoComum);
    }
    }

    View full-size slide

  71. @RestController
    public class ConsultaFormasDePagamentoController {
    @Autowired
    private UsuarioRepository usuarioRepository;
    @Autowired
    private RestauranteRepository restauranteRepository;
    @Autowired
    private FormaDePagamentoRepository formaDePagamentoRepository;
    @Transactional(readOnly = true)
    @GetMapping("/api/v1/formas-de-pagamento")
    public ResponseEntity> consultarFormasDePagamentoEmComum(
    @RequestParam(required = true) Long usuarioId,
    @RequestParam(required = true) Long restauranteId){
    if (usuarioRepository.existsById(usuarioId)) {
    throw new ResponseStatusException(UNPROCESSABLE_ENTITY, "Usuario não cadastrado");
    }
    if (restauranteRepository.existsById(restauranteId)) {
    throw new ResponseStatusException(UNPROCESSABLE_ENTITY, "Restaurante não cadastrado");
    }
    var formasPagamentoComum = formaDePagamentoRepository.findFormaDePagamentoEmComumEntre(usuarioId, restauranteId)
    .stream()
    .map(FormaDePagamentoResponse::new)
    .toList();
    return ResponseEntity.ok(formasPagamentoComum);
    }
    }

    View full-size slide

  72. public interface FormaDePagamentoRepository extends JpaRepository {
    @Cacheable(
    value = "forma_de_pagamento_entre",
    key = "T(java.util.Objects).hash(#usuarioId,#restauranteId)“
    )
    @Query(nativeQuery = true, value = """
    SELECT f.*
    FROM usuario_forma_de_pagamentos u
    INNER JOIN forma_de_pagamento f
    ON f.id = u.forma_de_pagamentos_id
    INNER JOIN restaurante_forma_de_pagamentos r
    ON f.id = r.forma_de_pagamentos_id
    WHERE u.usuario_id = :usuarioId
    AND r.restaurante_id = :restauranteId
    """)
    List findFormaDePagamentoEmComumEntre(Long usuarioId, Long restauranteId);
    }

    View full-size slide

  73. public interface FormaDePagamentoRepository extends JpaRepository {
    @Cacheable(
    value = "forma_de_pagamento_entre",
    key = "T(java.util.Objects).hash(#usuarioId,#restauranteId)"
    )
    @Query(nativeQuery = true, value = """
    SELECT f.*
    FROM usuario_forma_de_pagamentos u
    INNER JOIN forma_de_pagamento f
    ON f.id = u.forma_de_pagamentos_id
    INNER JOIN restaurante_forma_de_pagamentos r
    ON f.id = r.forma_de_pagamentos_id
    WHERE u.usuario_id = :usuarioId
    AND r.restaurante_id = :restauranteId
    """)
    List findFormaDePagamentoEmComumEntre(Long usuarioId, Long restauranteId);
    }

    View full-size slide

  74. public interface FormaDePagamentoRepository extends JpaRepository {
    @Cacheable(
    value = "forma_de_pagamento_entre",
    key = "T(java.util.Objects).hash(#usuarioId,#restauranteId)"
    )
    @Query(nativeQuery = true, value = """
    SELECT f.*
    FROM usuario_forma_de_pagamentos u
    INNER JOIN forma_de_pagamento f
    ON f.id = u.forma_de_pagamentos_id
    INNER JOIN restaurante_forma_de_pagamentos r
    ON f.id = r.forma_de_pagamentos_id
    WHERE u.usuario_id = :usuarioId
    AND r.restaurante_id = :restauranteId
    """)
    List findFormaDePagamentoEmComumEntre(Long usuarioId, Long restauranteId);
    }

    View full-size slide

  75. public interface FormaDePagamentoRepository extends JpaRepository {
    @Cacheable(
    value = "forma_de_pagamento_entre",
    key = "T(java.util.Objects).hash(#usuarioId,#restauranteId)"
    )
    @Query(nativeQuery = true, value = """
    SELECT f.*
    FROM usuario_forma_de_pagamentos u
    INNER JOIN forma_de_pagamento f
    ON f.id = u.forma_de_pagamentos_id
    INNER JOIN restaurante_forma_de_pagamentos r
    ON f.id = r.forma_de_pagamentos_id
    WHERE u.usuario_id = :usuarioId
    AND r.restaurante_id = :restauranteId
    """)
    List findFormaDePagamentoEmComumEntre(Long usuarioId, Long restauranteId);
    }

    View full-size slide

  76. Como o servidor se
    comporta com Caching
    Local?

    View full-size slide

  77. Cache
    João BD

    View full-size slide

  78. Cache
    João BD
    HTTP GET /formas-de-
    pagamento?usuarioId=1&restauranteId
    =1

    View full-size slide

  79. Cache
    João BD
    HTTP GET /formas-de-
    pagamento?usuarioId=1&restauranteId
    =1
    cache.get(formas-pagamento-1-
    1)

    View full-size slide

  80. Cache
    João
    null
    BD
    HTTP GET /formas-de-
    pagamento?usuarioId=1&restauranteId
    =1
    cache.get(formas-pagamento-1-
    1)

    View full-size slide

  81. Cache
    João
    repository.findFormasPagamentoComumEn
    tre(1,1);
    null
    BD
    HTTP GET /formas-de-
    pagamento?usuarioId=1&restauranteId
    =1
    cache.get(formas-pagamento-1-
    1)

    View full-size slide

  82. Cache
    João
    repository.findFormasPagamentoComumEn
    tre(1,1);
    null
    dados
    BD
    HTTP GET /formas-de-
    pagamento?usuarioId=1&restauranteId
    =1
    cache.get(formas-pagamento-1-
    1)

    View full-size slide

  83. Cache
    João
    repository.findFormasPagamentoComumEn
    tre(1,1);
    null
    dados
    BD
    HTTP GET /formas-de-
    pagamento?usuarioId=1&restauranteId
    =1
    cache.get(formas-pagamento-1-
    1)
    cache.put(formas-pagamento-1-
    1,dados)

    View full-size slide

  84. Cache
    João
    repository.findFormasPagamentoComumEn
    tre(1,1);
    null
    dados
    BD
    HTTP GET /formas-de-
    pagamento?usuarioId=1&restauranteId
    =1
    cache.get(formas-pagamento-1-
    1)
    cache.put(formas-pagamento-1-
    1,dados)
    dados

    View full-size slide

  85. Cache
    João
    repository.findFormasPagamentoComumEn
    tre(1,1);
    null
    dados
    BD
    HTTP GET /formas-de-
    pagamento?usuarioId=1&restauranteId
    =1
    cache.get(formas-pagamento-1-
    1)
    cache.put(formas-pagamento-1-
    1,dados)
    dados
    dados

    View full-size slide

  86. Cache Miss
    Cache
    João
    repository.findFormasPagamentoComumEn
    tre(1,1);
    null
    dados
    BD
    HTTP GET /formas-de-
    pagamento?usuarioId=1&restauranteId
    =1
    cache.get(formas-pagamento-1-
    1)
    cache.put(formas-pagamento-1-
    1,dados)
    dados
    dados

    View full-size slide

  87. Cache Miss
    Cache
    João
    repository.findFormasPagamentoComumEn
    tre(1,1);
    null
    dados
    BD
    HTTP GET /formas-de-
    pagamento?usuarioId=1&restauranteId
    =1
    cache.get(formas-pagamento-1-
    1)
    cache.put(formas-pagamento-1-
    1,dados)
    dados
    HTTP GET /formas-de-
    pagamento?usuarioId=1&restauranteId
    =1
    dados

    View full-size slide

  88. Cache Miss
    Cache
    João
    repository.findFormasPagamentoComumEn
    tre(1,1);
    null
    dados
    BD
    HTTP GET /formas-de-
    pagamento?usuarioId=1&restauranteId
    =1
    cache.get(formas-pagamento-1-
    1)
    cache.put(formas-pagamento-1-
    1,dados)
    dados
    HTTP GET /formas-de-
    pagamento?usuarioId=1&restauranteId
    =1 cache.get(formas-pagamento-1-
    1)
    dados

    View full-size slide

  89. Cache Miss
    Cache
    João
    repository.findFormasPagamentoComumEn
    tre(1,1);
    null
    dados
    BD
    HTTP GET /formas-de-
    pagamento?usuarioId=1&restauranteId
    =1
    cache.get(formas-pagamento-1-
    1)
    cache.put(formas-pagamento-1-
    1,dados)
    dados
    HTTP GET /formas-de-
    pagamento?usuarioId=1&restauranteId
    =1 cache.get(formas-pagamento-1-
    1)
    dados
    dados

    View full-size slide

  90. Cache Miss
    Cache
    João
    repository.findFormasPagamentoComumEn
    tre(1,1);
    null
    dados
    BD
    HTTP GET /formas-de-
    pagamento?usuarioId=1&restauranteId
    =1
    cache.get(formas-pagamento-1-
    1)
    cache.put(formas-pagamento-1-
    1,dados)
    dados
    HTTP GET /formas-de-
    pagamento?usuarioId=1&restauranteId
    =1 cache.get(formas-pagamento-1-
    1)
    dados
    dados
    dados

    View full-size slide

  91. Cache Hit
    Cache Miss
    Cache
    João
    repository.findFormasPagamentoComumEn
    tre(1,1);
    null
    dados
    BD
    HTTP GET /formas-de-
    pagamento?usuarioId=1&restauranteId
    =1
    cache.get(formas-pagamento-1-
    1)
    cache.put(formas-pagamento-1-
    1,dados)
    dados
    HTTP GET /formas-de-
    pagamento?usuarioId=1&restauranteId
    =1 cache.get(formas-pagamento-1-
    1)
    dados
    dados
    dados

    View full-size slide

  92. Como o servidor se
    comporta com aumento de
    acessos simultâneos?

    View full-size slide

  93. Servidor
    Local Caching
    Heap Memory
    Database
    REQUEST
    RESPONSE

    View full-size slide

  94. Servidor
    Local Caching
    Heap Memory
    Database
    REQUEST
    RESPONSE

    View full-size slide

  95. Servidor
    Local Caching
    Heap Memory
    Database
    REQUEST
    RESPONSE

    View full-size slide

  96. Servidor
    Local Caching
    Heap Memory
    Database
    REQUEST
    RESPONSE

    View full-size slide

  97. Servidor
    Local Caching
    Heap Memory
    Database
    REQUEST
    RESPONSE

    View full-size slide

  98. Database
    REQUEST
    RESPONSE
    Servidor
    Local Caching
    Heap Memory

    View full-size slide

  99. Servidor
    Local Caching
    Heap Memory
    Database
    REQUEST
    RESPONSE

    View full-size slide

  100. Turbinando Microsserviços com Distributed Cache

    View full-size slide

  101. NÃO POSSUI POLÍTICA DE
    INVALIDAÇÃO

    View full-size slide

  102. EHCache, Guava e Caffeine

    View full-size slide

  103. spring:
    ##
    # Spring Caching
    ##
    cache:
    cache-names: forma_de_pagamento_entre
    caffeine.spec: maximumSize = 100000, expireAfterWrite = 4h
    application.yaml

    View full-size slide

  104. spring:
    ##
    # Spring Caching
    ##
    cache:
    cache-names: forma_de_pagamento_entre
    caffeine.spec: maximumSize = 100000, expireAfterWrite = 4h
    application.yaml

    View full-size slide

  105. spring:
    ##
    # Spring Caching
    ##
    cache:
    cache-names: forma_de_pagamento_entre
    caffeine.spec: maximumSize = 100000, expireAfterWrite = 4h
    application.yaml

    View full-size slide

  106. spring:
    ##
    # Spring Caching
    ##
    cache:
    cache-names: forma_de_pagamento_entre
    caffeine.spec: maximumSize = 100000, expireAfterWrite = 4h
    application.yaml

    View full-size slide

  107. spring:
    ##
    # Spring Caching
    ##
    cache:
    cache-names: forma_de_pagamento_entre
    caffeine.spec: maximumSize = 100000, expireAfterWrite = 4h
    application.yaml

    View full-size slide

  108. Time To Live (TTL)

    View full-size slide

  109. Servidor
    Heap Memory
    Local Caching
    Time to live (TTL)
    0 min

    View full-size slide

  110. Servidor
    Heap Memory
    Local Caching
    Time to live (TTL)
    20 min

    View full-size slide

  111. Servidor
    Heap Memory
    Local Caching
    Time to live (TTL)
    40 min

    View full-size slide

  112. Servidor
    Heap Memory
    Local Caching
    Time to live (TTL)
    1h 00 min

    View full-size slide

  113. Servidor
    Heap Memory
    Local Caching
    Time to live (TTL)
    1h 20 min

    View full-size slide

  114. Servidor
    Heap Memory
    Local Caching
    Time to live (TTL)
    1h 40 min

    View full-size slide

  115. Servidor
    Heap Memory
    Local Caching
    Time to live (TTL)
    2h 00 min

    View full-size slide

  116. Servidor
    Heap Memory
    Local Caching
    Time to live (TTL)
    2h 20 min

    View full-size slide

  117. Servidor
    Heap Memory
    Local Caching
    Time to live (TTL)
    2h 40 min

    View full-size slide

  118. Servidor
    Heap Memory
    Local Caching
    Time to live (TTL)
    3h 00 min

    View full-size slide

  119. Servidor
    Heap Memory
    Local Caching
    Time to live (TTL)
    3h 20 min

    View full-size slide

  120. Servidor
    Heap Memory
    Local Caching
    Time to live (TTL)
    3h 40 min

    View full-size slide

  121. Servidor
    Heap Memory
    Local Caching
    Time to live (TTL)
    4h 00 min

    View full-size slide

  122. Servidor
    Heap Memory
    Local Caching
    Time to live (TTL)

    View full-size slide

  123. como funciona em Ambientes
    Clusterizados?

    View full-size slide

  124. Servidor B
    Servidor A

    View full-size slide

  125. Servidor B
    Servidor A
    repository.findFormasPagamentoComumEntre(1,1)

    View full-size slide

  126. Servidor B
    Servidor A
    repository.findFormasPagamentoComumEntre(1,1)
    dados

    View full-size slide

  127. Servidor B
    Servidor A
    repository.findFormasPagamentoComumEntre(1,1)
    dados
    Cache.put(key,dados)

    View full-size slide

  128. Servidor B
    Servidor A
    repository.findFormasPagamentoComumEntre(1,1)
    dados
    repository.findFormasPagamentoComumEntre(1,1)
    Cache.put(key,dados)

    View full-size slide

  129. Servidor B
    Servidor A
    repository.findFormasPagamentoComumEntre(1,1)
    dados
    repository.findFormasPagamentoComumEntre(1,1)
    dados
    Cache.put(key,dados)

    View full-size slide

  130. Servidor B
    Servidor A
    repository.findFormasPagamentoComumEntre(1,1)
    dados
    repository.findFormasPagamentoComumEntre(1,1)
    dados
    repository.addFormaDePagamentoParaUsuadrio(1,6)
    Cache.put(key,dados)

    View full-size slide

  131. Servidor B
    Servidor A
    repository.findFormasPagamentoComumEntre(1,1)
    dados
    repository.findFormasPagamentoComumEntre(1,1)
    dados
    repository.addFormaDePagamentoParaUsuadrio(1,6)
    1 ROW Insert
    Cache.put(key,dados)

    View full-size slide

  132. Servidor B
    Servidor A
    repository.findFormasPagamentoComumEntre(1,1)
    dados
    repository.findFormasPagamentoComumEntre(1,1)
    dados
    repository.addFormaDePagamentoParaUsuadrio(1,6)
    1 ROW Insert
    Cache.put(key,dados)
    Cache.put(key,dados)

    View full-size slide

  133. Cache Distribuído

    View full-size slide

  134. spring:
    ##
    # Spring Caching
    ##
    cache:
    cache-names: forma_de_pagamento_entre
    caffeine.spec: maximumSize = 100000, expireAfterWrite = 4h
    application.yaml

    View full-size slide

  135. spring:
    ##
    # Spring Caching
    ##
    cache:
    cache-names: forma_de_pagamento_entre
    type: redis
    application.yaml

    View full-size slide

  136. Configurar as Politicas do
    Redis

    View full-size slide

  137. @Configuration
    public class RedisCacheConfig {
    @Bean
    public RedisCacheConfiguration defaultCacheConfiguration() {
    return RedisCacheConfiguration.defaultCacheConfig()
    .entryTtl(Duration.ofHours(4))// 4 horas
    .disableCachingNullValues()
    .serializeValuesWith(fromSerializer(new GenericJackson2JsonRedisSerializer()));
    }
    }

    View full-size slide

  138. @Configuration
    public class RedisCacheConfig {
    @Bean
    public RedisCacheConfiguration defaultCacheConfiguration() {
    return RedisCacheConfiguration.defaultCacheConfig()
    .entryTtl(Duration.ofHours(4))// 4 horas
    .disableCachingNullValues()
    .serializeValuesWith(fromSerializer(new GenericJackson2JsonRedisSerializer()));
    }
    }

    View full-size slide

  139. @Configuration
    public class RedisCacheConfig {
    @Bean
    public RedisCacheConfiguration defaultCacheConfiguration() {
    return RedisCacheConfiguration.defaultCacheConfig()
    .entryTtl(Duration.ofHours(4))// 4 horas
    .disableCachingNullValues()
    .serializeValuesWith(fromSerializer(new GenericJackson2JsonRedisSerializer()));
    }
    }

    View full-size slide

  140. @Configuration
    public class RedisCacheConfig {
    @Bean
    public RedisCacheConfiguration defaultCacheConfiguration() {
    return RedisCacheConfiguration.defaultCacheConfig()
    .entryTtl(Duration.ofHours(4))// 4 horas
    .disableCachingNullValues()
    .serializeValuesWith(fromSerializer(new GenericJackson2JsonRedisSerializer()));
    }
    }

    View full-size slide

  141. @Configuration
    public class RedisCacheConfig {
    @Bean
    public RedisCacheConfiguration defaultCacheConfiguration() {
    return RedisCacheConfiguration.defaultCacheConfig()
    .entryTtl(Duration.ofHours(4))// 4 horas
    .disableCachingNullValues()
    .serializeValuesWith(fromSerializer(new GenericJackson2JsonRedisSerializer()));
    }
    }

    View full-size slide

  142. @Configuration
    public class RedisCacheConfig {
    @Bean
    public RedisCacheConfiguration defaultCacheConfiguration() {
    return RedisCacheConfiguration.defaultCacheConfig()
    .entryTtl(Duration.ofHours(4))// 4 horas
    .disableCachingNullValues()
    .serializeValuesWith(fromSerializer(new GenericJackson2JsonRedisSerializer()));
    }
    }

    View full-size slide

  143. @Configuration
    public class RedisCacheConfig {
    @Bean
    public RedisCacheConfiguration defaultCacheConfiguration() {
    return RedisCacheConfiguration.defaultCacheConfig()
    .entryTtl(Duration.ofHours(4))// 4 horas
    .disableCachingNullValues()
    .serializeValuesWith(fromSerializer(new GenericJackson2JsonRedisSerializer()));
    }
    }

    View full-size slide

  144. @Configuration
    public class RedisCacheConfig {
    @Bean
    public RedisCacheConfiguration defaultCacheConfiguration() {
    return RedisCacheConfiguration.defaultCacheConfig()
    .entryTtl(Duration.ofHours(4))// 4 horas
    .disableCachingNullValues()
    .serializeValuesWith(fromSerializer(new GenericJackson2JsonRedisSerializer()));
    }
    }

    View full-size slide

  145. BD
    Servidor A Servidor B Cache

    View full-size slide

  146. BD
    cache.get(formas-pagamento-1-1)
    Servidor A Servidor B Cache

    View full-size slide

  147. null
    BD
    cache.get(formas-pagamento-1-1)
    Servidor A Servidor B Cache

    View full-size slide

  148. null
    BD
    cache.get(formas-pagamento-1-1)
    Servidor A Servidor B
    repository.findFormasPagamentoComum
    Entre(1,1)
    Cache

    View full-size slide

  149. null
    BD
    dados
    cache.get(formas-pagamento-1-1)
    Servidor A Servidor B
    repository.findFormasPagamentoComum
    Entre(1,1)
    Cache

    View full-size slide

  150. null
    BD
    cache.put(formas-pagamento-
    1-1,dados)
    dados
    cache.get(formas-pagamento-1-1)
    Servidor A Servidor B
    repository.findFormasPagamentoComum
    Entre(1,1)
    Cache

    View full-size slide

  151. Cache Miss
    null
    BD
    cache.put(formas-pagamento-
    1-1,dados)
    dados
    cache.get(formas-pagamento-1-1)
    Servidor A Servidor B
    repository.findFormasPagamentoComum
    Entre(1,1)
    Cache

    View full-size slide

  152. Cache Miss
    null
    BD
    cache.put(formas-pagamento-
    1-1,dados)
    dados
    cache.get(formas-pagamento-1-1)
    Servidor A Servidor B
    repository.findFormasPagamentoComum
    Entre(1,1)
    cache.get(formas-pagamento-1-1)
    Cache

    View full-size slide

  153. Cache Miss
    null
    BD
    cache.put(formas-pagamento-
    1-1,dados)
    dados
    cache.get(formas-pagamento-1-1)
    dados
    Servidor A Servidor B
    repository.findFormasPagamentoComum
    Entre(1,1)
    cache.get(formas-pagamento-1-1)
    Cache

    View full-size slide

  154. Cache Hit
    Cache Miss
    null
    BD
    cache.put(formas-pagamento-
    1-1,dados)
    dados
    cache.get(formas-pagamento-1-1)
    dados
    Servidor A Servidor B
    repository.findFormasPagamentoComum
    Entre(1,1)
    cache.get(formas-pagamento-1-1)
    Cache

    View full-size slide

  155. Como continuar
    escalando?🚀

    View full-size slide

  156. Servidor A Servidor B
    Servidor E
    Servidor C
    Servidor F
    Servidor D
    Cluster
    Database

    View full-size slide

  157. Servidor A Servidor B
    Servidor E
    Servidor C
    Servidor F
    Servidor D
    Cluster
    Database

    View full-size slide

  158. Servidor A Servidor B
    Servidor E
    Servidor C
    Servidor F
    Servidor D
    Cluster
    Database

    View full-size slide

  159. Servidor A Servidor B
    Servidor E
    Servidor C
    Servidor F
    Servidor D
    Cluster
    Database

    View full-size slide

  160. Servidor A Servidor B
    Servidor E
    Servidor C
    Servidor F
    Servidor D
    Cluster
    Database

    View full-size slide

  161. Servidor A Servidor B
    Servidor E
    Servidor C
    Servidor F
    Servidor D
    Cluster
    Database

    View full-size slide

  162. Se for possível cachear
    pequena parte dos dados
    quentes?

    View full-size slide

  163. Database
    Servidor
    Heap Memory
    Servidor
    Heap Memory
    Servidor
    Heap Memory
    CLUSTER SERVER

    View full-size slide

  164. Database
    Servidor
    Heap Memory
    Servidor
    Heap Memory
    Servidor
    Heap Memory
    CLUSTER SERVER

    View full-size slide

  165. Database
    Servidor
    Heap Memory
    Local Hazel
    Cache
    Servidor
    Heap Memory
    Local Hazel
    Cache
    Servidor
    Heap Memory
    Local Hazel
    Cache
    CLUSTER SERVER

    View full-size slide

  166. cache:
    name: forma_de_pagamento_entre
    spring:
    ##
    # Spring Caching
    ##
    cache:
    cache-names: forma_de_pagamento_entre
    type: redis
    application.yaml

    View full-size slide

  167. cache:
    name: forma_de_pagamento_entre
    spring:
    ##
    # demais configurações omitidas
    ##
    cache:
    cache-names: forma_de_pagamento_entre
    type: redis
    application.yaml

    View full-size slide

  168. cache:
    name: forma_de_pagamento_entre
    spring:
    ##
    # demais configurações omitidas
    ##
    cache:
    cache-names: forma_de_pagamento_entre
    type: redis
    application.yaml

    View full-size slide

  169. Habilitando o Near Cache

    View full-size slide

  170. @Configuration
    public class HazelCastCacheConfig {
    @Value("${cache.name}")
    private String cacheName;
    private final int FOUR_HOURS = 14400;// 4 horas
    @Bean
    ClientConfig config() {
    ClientConfig clientConfig = new ClientConfig();
    clientConfig.addNearCacheConfig(nearCacheConfig());
    return clientConfig;
    }
    private NearCacheConfig nearCacheConfig() {
    NearCacheConfig nearCacheConfig = new NearCacheConfig();
    nearCacheConfig.setName(cacheName);
    nearCacheConfig.setTimeToLiveSeconds(FOUR_HOURS); // 4 horas
    return nearCacheConfig;
    }
    }

    View full-size slide

  171. @Configuration
    public class HazelCastCacheConfig {
    @Value("${cache.name}")
    private String cacheName;
    private final int FOUR_HOURS = 14400;// 4 horas
    @Bean
    ClientConfig config() {
    ClientConfig clientConfig = new ClientConfig();
    clientConfig.addNearCacheConfig(nearCacheConfig());
    return clientConfig;
    }
    private NearCacheConfig nearCacheConfig() {
    NearCacheConfig nearCacheConfig = new NearCacheConfig();
    nearCacheConfig.setName(cacheName);
    nearCacheConfig.setTimeToLiveSeconds(FOUR_HOURS); // 4 horas
    return nearCacheConfig;
    }
    }

    View full-size slide

  172. @Configuration
    public class HazelCastCacheConfig {
    @Value("${cache.name}")
    private String cacheName;
    private final int FOUR_HOURS = 14400;// 4 horas
    @Bean
    ClientConfig config() {
    ClientConfig clientConfig = new ClientConfig();
    clientConfig.addNearCacheConfig(nearCacheConfig());
    return clientConfig;
    }
    private NearCacheConfig nearCacheConfig() {
    NearCacheConfig nearCacheConfig = new NearCacheConfig();
    nearCacheConfig.setName(cacheName);
    nearCacheConfig.setTimeToLiveSeconds(FOUR_HOURS); // 4 horas
    return nearCacheConfig;
    }
    }

    View full-size slide

  173. @Configuration
    public class HazelCastCacheConfig {
    @Value("${cache.name}")
    private String cacheName;
    private final int FOUR_HOURS = 14400;// 4 horas
    @Bean
    ClientConfig config() {
    ClientConfig clientConfig = new ClientConfig();
    clientConfig.addNearCacheConfig(nearCacheConfig());
    return clientConfig;
    }
    private NearCacheConfig nearCacheConfig() {
    NearCacheConfig nearCacheConfig = new NearCacheConfig();
    nearCacheConfig.setName(cacheName);
    nearCacheConfig.setTimeToLiveSeconds(FOUR_HOURS); // 4 horas
    return nearCacheConfig;
    }
    }

    View full-size slide

  174. @Configuration
    public class HazelCastCacheConfig {
    @Value("${cache.name}")
    private String cacheName;
    private final int FOUR_HOURS = 14400;// 4 horas
    @Bean
    ClientConfig config() {
    ClientConfig clientConfig = new ClientConfig();
    clientConfig.addNearCacheConfig(nearCacheConfig());
    return clientConfig;
    }
    private NearCacheConfig nearCacheConfig() {
    NearCacheConfig nearCacheConfig = new NearCacheConfig();
    nearCacheConfig.setName(cacheName);
    nearCacheConfig.setTimeToLiveSeconds(FOUR_HOURS); // 4 horas
    return nearCacheConfig;
    }
    }

    View full-size slide

  175. @Configuration
    public class HazelCastCacheConfig {
    @Value("${cache.name}")
    private String cacheName;
    private final int FOUR_HOURS = 14400;// 4 horas
    @Bean
    ClientConfig config() {
    ClientConfig clientConfig = new ClientConfig();
    clientConfig.addNearCacheConfig(nearCacheConfig());
    return clientConfig;
    }
    private NearCacheConfig nearCacheConfig() {
    NearCacheConfig nearCacheConfig = new NearCacheConfig();
    nearCacheConfig.setName(cacheName);
    nearCacheConfig.setTimeToLiveSeconds(FOUR_HOURS); // 4 horas
    return nearCacheConfig;
    }
    }

    View full-size slide

  176. @Configuration
    public class HazelCastCacheConfig {
    @Value("${cache.name}")
    private String cacheName;
    private final int FOUR_HOURS = 14400;// 4 horas
    @Bean
    ClientConfig config() {
    ClientConfig clientConfig = new ClientConfig();
    clientConfig.addNearCacheConfig(nearCacheConfig());
    return clientConfig;
    }
    private NearCacheConfig nearCacheConfig() {
    NearCacheConfig nearCacheConfig = new NearCacheConfig();
    nearCacheConfig.setName(cacheName);
    nearCacheConfig.setTimeToLiveSeconds(FOUR_HOURS); // 4 horas
    return nearCacheConfig;
    }
    }

    View full-size slide

  177. @Configuration
    public class HazelCastCacheConfig {
    @Value("${cache.name}")
    private String cacheName;
    private final int FOUR_HOURS = 14400;// 4 horas
    @Bean
    ClientConfig config() {
    ClientConfig clientConfig = new ClientConfig();
    clientConfig.addNearCacheConfig(nearCacheConfig());
    return clientConfig;
    }
    private NearCacheConfig nearCacheConfig() {
    NearCacheConfig nearCacheConfig = new NearCacheConfig();
    nearCacheConfig.setName(cacheName);
    nearCacheConfig.setTimeToLiveSeconds(FOUR_HOURS); // 4 horas
    return nearCacheConfig;
    }
    }

    View full-size slide

  178. @Configuration
    public class HazelCastCacheConfig {
    @Value("${cache.name}")
    private String cacheName;
    private final int FOUR_HOURS = 14400;// 4 horas
    @Bean
    ClientConfig config() {
    ClientConfig clientConfig = new ClientConfig();
    clientConfig.addNearCacheConfig(nearCacheConfig());
    return clientConfig;
    }
    private NearCacheConfig nearCacheConfig() {
    NearCacheConfig nearCacheConfig = new NearCacheConfig();
    nearCacheConfig.setName(cacheName);
    nearCacheConfig.setTimeToLiveSeconds(FOUR_HOURS); // 4 horas
    return nearCacheConfig;
    }
    }

    View full-size slide

  179. @Configuration
    public class HazelCastCacheConfig {
    @Value("${cache.name}")
    private String cacheName;
    private final int FOUR_HOURS = 14400;// 4 horas
    @Bean
    ClientConfig config() {
    ClientConfig clientConfig = new ClientConfig();
    clientConfig.addNearCacheConfig(nearCacheConfig());
    return clientConfig;
    }
    private NearCacheConfig nearCacheConfig() {
    NearCacheConfig nearCacheConfig = new NearCacheConfig();
    nearCacheConfig.setName(cacheName);
    nearCacheConfig.setTimeToLiveSeconds(FOUR_HOURS); // 4 horas
    return nearCacheConfig;
    }
    }

    View full-size slide

  180. @Configuration
    public class HazelCastCacheConfig {
    @Value("${cache.name}")
    private String cacheName;
    private final int FOUR_HOURS = 14400;// 4 horas
    @Bean
    ClientConfig config() {
    ClientConfig clientConfig = new ClientConfig();
    clientConfig.addNearCacheConfig(nearCacheConfig());
    return clientConfig;
    }
    private NearCacheConfig nearCacheConfig() {
    NearCacheConfig nearCacheConfig = new NearCacheConfig();
    nearCacheConfig.setName(cacheName);
    nearCacheConfig.setTimeToLiveSeconds(FOUR_HOURS); // 4 horas
    return nearCacheConfig;
    }
    }

    View full-size slide

  181. @Configuration
    public class HazelCastCacheConfig {
    @Value("${cache.name}")
    private String cacheName;
    private final int FOUR_HOURS = 14400;// 4 horas
    @Bean
    ClientConfig config() {
    ClientConfig clientConfig = new ClientConfig();
    clientConfig.addNearCacheConfig(nearCacheConfig());
    return clientConfig;
    }
    private NearCacheConfig nearCacheConfig() {
    NearCacheConfig nearCacheConfig = new NearCacheConfig();
    nearCacheConfig.setName(cacheName);
    nearCacheConfig.setTimeToLiveSeconds(FOUR_HOURS); // 4 horas
    return nearCacheConfig;
    }
    }

    View full-size slide

  182. @Configuration
    public class HazelCastCacheConfig {
    @Value("${cache.name}")
    private String cacheName;
    private final int FOUR_HOURS = 14400;// 4 horas
    @Bean
    ClientConfig config() {
    ClientConfig clientConfig = new ClientConfig();
    clientConfig.addNearCacheConfig(nearCacheConfig());
    return clientConfig;
    }
    private NearCacheConfig nearCacheConfig() {
    NearCacheConfig nearCacheConfig = new NearCacheConfig();
    nearCacheConfig.setName(cacheName);
    nearCacheConfig.setTimeToLiveSeconds(FOUR_HOURS); // 4 horas
    return nearCacheConfig;
    }
    }

    View full-size slide

  183. @Configuration
    public class HazelCastCacheConfig {
    @Value("${cache.name}")
    private String cacheName;
    private final int FOUR_HOURS = 14400;// 4 horas
    @Bean
    ClientConfig config() {
    ClientConfig clientConfig = new ClientConfig();
    clientConfig.addNearCacheConfig(nearCacheConfig());
    return clientConfig;
    }
    private NearCacheConfig nearCacheConfig() {
    NearCacheConfig nearCacheConfig = new NearCacheConfig();
    nearCacheConfig.setName(cacheName);
    nearCacheConfig.setTimeToLiveSeconds(FOUR_HOURS); // 4 horas
    return nearCacheConfig;
    }
    }

    View full-size slide

  184. @Configuration
    public class HazelCastCacheConfig {
    @Value("${cache.name}")
    private String cacheName;
    private final int FOUR_HOURS = 14400;// 4 horas
    @Bean
    ClientConfig config() {
    ClientConfig clientConfig = new ClientConfig();
    clientConfig.addNearCacheConfig(nearCacheConfig());
    return clientConfig;
    }
    private NearCacheConfig nearCacheConfig() {
    NearCacheConfig nearCacheConfig = new NearCacheConfig();
    nearCacheConfig.setName(cacheName);
    nearCacheConfig.setTimeToLiveSeconds(FOUR_HOURS); // 4 horas
    return nearCacheConfig;
    }
    }

    View full-size slide

  185. BD
    Servidor
    Local HazelCache Cache

    View full-size slide

  186. BD
    Servidor
    Local HazelCache Cache
    cache.get(formas-pagamento-1-1)

    View full-size slide

  187. BD
    Servidor
    Local HazelCache Cache
    cache.get(formas-pagamento-1-1)
    null

    View full-size slide

  188. BD
    cache.get(formas-pagamento-1-1)
    Servidor
    Local HazelCache Cache
    cache.get(formas-pagamento-1-1)
    null

    View full-size slide

  189. null
    BD
    cache.get(formas-pagamento-1-1)
    Servidor
    Local HazelCache Cache
    cache.get(formas-pagamento-1-1)
    null

    View full-size slide

  190. null
    BD
    cache.get(formas-pagamento-1-1)
    Servidor
    Local HazelCache
    repository.findFormasPagamentoComum
    Entre(1,1)
    Cache
    cache.get(formas-pagamento-1-1)
    null

    View full-size slide

  191. null
    BD
    dados
    cache.get(formas-pagamento-1-1)
    Servidor
    Local HazelCache
    repository.findFormasPagamentoComum
    Entre(1,1)
    Cache
    cache.get(formas-pagamento-1-1)
    null

    View full-size slide

  192. null
    BD
    cache.put(formas-pagamento-
    1-1,dados)
    dados
    cache.get(formas-pagamento-1-1)
    Servidor
    Local HazelCache
    repository.findFormasPagamentoComum
    Entre(1,1)
    Cache
    cache.get(formas-pagamento-1-1)
    null

    View full-size slide

  193. null
    BD
    cache.put(formas-pagamento-
    1-1,dados)
    dados
    cache.get(formas-pagamento-1-1)
    Servidor
    Local HazelCache
    repository.findFormasPagamentoComum
    Entre(1,1)
    Cache
    cache.get(formas-pagamento-1-1)
    null
    cache.put(formas-pagamento-1-
    1,dados)

    View full-size slide

  194. Servidor A Servidor B
    Servidor E
    Servidor C
    Servidor F
    Servidor D
    Cluster
    Database

    View full-size slide

  195. Servidor A Servidor B
    Servidor E
    Servidor C
    Servidor F
    Servidor D
    Cluster
    Database

    View full-size slide

  196. OBRIGADO!
    @JORDIHOFC

    View full-size slide