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

Turbinando Microsserviços com Distributed Cache

Turbinando Microsserviços com Distributed Cache

Talk apresentada na #DevConf2023 em 27/05/2023

Talk apresentada no #SouJavaNaCampus em 27/07/2023 disponivel em: https://www.youtube.com/live/8qJqbrwtZr8?feature=share&t=61

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 featues 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 afim 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ê distribuido e entender os tradeoffs de cada estratégia.

Jordi Henrique Silva

May 29, 2023
Tweet

More Decks by Jordi Henrique Silva

Other Decks in Technology

Transcript

  1. Turbinando Microsserviços com Distributed Cache

    View Slide

  2. Qual papel da Tecnologia no Negócio?

    View Slide

  3. Automatizar o processo operacional?

    View Slide

  4. View Slide

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

    View Slide

  6. Auxiliar o processo
    de crescimento do
    negócio

    View Slide

  7. O meu pedido
    já chegou?

    View Slide

  8. RESTAURANTE
    MENU

    View Slide

  9. RESTAURANTE
    MENU
    R$ 30.00

    View Slide

  10. RESTAURANTE
    MENU
    R$ 30.00
    R$ 45.99

    View Slide

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

    View Slide

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

    View Slide

  13. View Slide

  14. View Slide

  15. View Slide

  16. View Slide

  17. View Slide

  18. View Slide

  19. Formas de Pagamento

    View Slide

  20. Formas de Pagamento

    View Slide

  21. Formas de Pagamento

    View Slide

  22. Formas de Pagamento

    View Slide

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

    View 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 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 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 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 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 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 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 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 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 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 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 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 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 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 Slide

  38. @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 Slide

  39. @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 Slide

  40. @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 Slide

  41. @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 Slide

  42. @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 Slide

  43. @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 Slide

  44. @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 Slide

  45. 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 Slide

  46. View Slide

  47. View Slide

  48. View Slide

  49. View Slide

  50. View Slide

  51. Database

    View Slide

  52. Database

    View Slide

  53. Database

    View Slide

  54. Aumento no tempo de
    resposta

    View Slide

  55. O que é Caching?

    View Slide

  56. 980.999.810-49
    João Lucas
    Chave
    Valor

    View Slide

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

    View Slide

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

    View Slide

  59. 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 Slide

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

    View Slide

  61. Criamos um CacheService?
    p/ gerenciar um HashMap

    View Slide

  62. E os acessos de
    simultâneos?

    View Slide

  63. ConcurrentHashMap

    View Slide

  64. View Slide

  65. Servidor
    Heap Memory

    View Slide

  66. Database
    Servidor
    Heap Memory

    View Slide

  67. Database
    Servidor
    Heap Memory
    REQUEST

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  71. Depois...

    View Slide

  72. Database
    Servidor
    Heap Memory

    View Slide

  73. Database
    Servidor
    Heap Memory
    Local Caching

    View Slide

  74. Database
    Servidor
    Heap Memory
    REQUEST
    Local Caching

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  79. Database
    Servidor
    Heap Memory
    Local Caching

    View Slide

  80. Database
    Servidor
    Heap Memory
    REQUEST
    Local Caching

    View Slide

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

    View Slide

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

    View Slide

  83. Existe uma forma mais
    elegante?

    View Slide

  84. Spring Caching

    View Slide

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

    View Slide

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

    View Slide

  87. @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 Slide

  88. @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 Slide

  89. 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 Slide

  90. 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 Slide

  91. 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 Slide

  92. 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 Slide

  93. Como o servidor se
    comporta com Caching
    Local?

    View Slide

  94. Cache
    João BD

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  98. 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 Slide

  99. 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 Slide

  100. 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 Slide

  101. 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 Slide

  102. 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 Slide

  103. 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 Slide

  104. 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 Slide

  105. 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 Slide

  106. 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 Slide

  107. 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 Slide

  108. 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 Slide

  109. E...

    View Slide

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

    View Slide

  111. Servidor
    Local Caching
    Heap Memory
    Database
    REQUEST
    RESPONSE

    View Slide

  112. Servidor
    Local Caching
    Heap Memory
    Database
    REQUEST
    RESPONSE

    View Slide

  113. Servidor
    Local Caching
    Heap Memory
    Database
    REQUEST
    RESPONSE

    View Slide

  114. Servidor
    Local Caching
    Heap Memory
    Database
    REQUEST
    RESPONSE

    View Slide

  115. Servidor
    Local Caching
    Heap Memory
    Database
    REQUEST
    RESPONSE

    View Slide

  116. Database
    REQUEST
    RESPONSE
    Servidor
    Local Caching
    Heap Memory

    View Slide

  117. Servidor
    Local Caching
    Heap Memory
    Database
    REQUEST
    RESPONSE

    View Slide

  118. View Slide

  119. View Slide

  120. Turbinando Microsserviços com Distributed Cache

    View Slide

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

    View Slide

  122. EHCache, Guava e Caffeine

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  128. Time To Live (TTL)

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  143. E Se?

    View Slide

  144. View Slide

  145. View Slide

  146. Como funciona em Ambientes
    Clusterizados?

    View Slide

  147. Servidor B
    Servidor A

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  154. 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 Slide

  155. 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 Slide

  156. 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 Slide

  157. 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 Slide

  158. Cache Distribuído

    View Slide

  159. View Slide

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

    View Slide

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

    View Slide

  162. Configurar as Politicas do
    Redis

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  171. BD
    Servidor A Servidor B Cache

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  176. 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 Slide

  177. 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 Slide

  178. 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 Slide

  179. 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 Slide

  180. 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 Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  187. View Slide

  188. E...

    View Slide

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

    View Slide

  190. Como continuar escalando?

    View Slide

  191. Near Cache

    View Slide

  192. Database
    Servidor
    Heap Memory
    Servidor
    Heap Memory
    Servidor
    Heap Memory
    CLUSTER SERVER

    View Slide

  193. Database
    Servidor
    Heap Memory
    Servidor
    Heap Memory
    Servidor
    Heap Memory
    CLUSTER SERVER

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  198. Habilitando o Near Cache

    View Slide

  199. @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 Slide

  200. @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 Slide

  201. @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 Slide

  202. @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 Slide

  203. @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 Slide

  204. @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 Slide

  205. @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 Slide

  206. @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 Slide

  207. @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 Slide

  208. @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 Slide

  209. @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 Slide

  210. @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 Slide

  211. @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 Slide

  212. @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 Slide

  213. @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 Slide

  214. BD
    Servidor
    Local HazelCache Cache

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  219. 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 Slide

  220. 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 Slide

  221. 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 Slide

  222. 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 Slide

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

    View Slide

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

    View Slide

  225. View Slide

  226. CONCLUINDO

    View Slide

  227. Cache Local

    View Slide

  228. Cache Distribuído

    View Slide

  229. Near Cache

    View Slide

  230. OBRIGADO!
    @JORDIHOFC

    View Slide