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

Caching in AWS Lambda

Caching in AWS Lambda

You want to optimize your performance in your AWS Lambda functions and reduce infrastructure costs by avoiding expensive API calls? You should think about caching data in your function! In this talk, I'll show you three different options how to cache data in Lambda: simple caching, custom caching and managed caching.

sebastianhesse

December 06, 2018
Tweet

More Decks by sebastianhesse

Other Decks in Programming

Transcript

  1. View Slide

  2. Available options to cache data in AWS Lambda and how to use them.
    CACHING IN AWS LAMBDA

    View Slide

  3. We´re

    View Slide

  4. Quick recap what AWS Lambda is and how it looks like.
    AWS LAMBDA

    View Slide

  5. SIMPLE AWS LAMBDA FUNCTION: NODEJS

    View Slide

  6. SIMPLE AWS LAMBDA FUNCTION: JAVA

    View Slide

  7. DEPLOYMENT PROCESS
    Lambda
    Function
    Lambda
    Function
    >
    Code Artifact
    AWS Lambda

    View Slide

  8. REASONS FOR CACHING

    View Slide

  9. BETTER PERFORMANCE
    Avoid slow API calls or
    computations.
    REDUCE COSTS
    Avoid calls which cost you
    money, e.g. to your database.
    REASONS

    View Slide

  10. PAY FOR WHAT
    YOU USE

    View Slide

  11. LAMBDA SCALING
    GET Name for X
    GET Name for X
    GET Name for X
    GET Name for X
    DynamoDB

    View Slide

  12. TYPICAL CACHING SETUP
    CACHE
    Your app
    DynamoDB

    View Slide

  13. "Tha)'s e-./. I've 3456 t8a9 - h:;9=m6.."
    You

    View Slide

  14. START CODING?!

    View Slide

  15. INFRASTRUCTURE UNCERTAINTIES
    Functions are only instantiated when needed.
    Functions might be shut down at any time.

    View Slide

  16. CUSTOM
    Setup a caching instance by
    your own.
    CACHING OPTIONS
    MANAGED
    Use a managed caching
    service.
    SIMPLE
    Cache data only inside
    Lambda functions.

    View Slide

  17. Understanding the process how a Lambda function is called.
    LAMBDA EXECUTION PROCESS

    View Slide

  18. LAMBDA EXECUTION PROCESS
    Response Time (ms)
    0
    250
    500
    750
    1000
    # Number Of Request
    1 2 3 4 5 6 7 8 9 10
    Cold start

    View Slide

  19. Called by AWS Lambda
    LAMBDA EXECUTION PROCESS

    View Slide

  20. LAMBDA EXECUTION PROCESS
    1.) Only executed at cold start
    2.) Executed each time

    View Slide

  21. Cache data without using a caching service or library.
    SIMPLE CACHING

    View Slide

  22. SIMPLE CACHING

    View Slide

  23. SIMPLE CACHING

    View Slide

  24. SIMPLE CACHING

    View Slide

  25. SIMPLE CACHING

    View Slide

  26. SIMILAR IN JAVA
    Field initialization, constructor, etc.

    View Slide

  27. PRO TIP
    TIME FOR INITIALIZATION
    IS NOT CHARGED
    BY AWS.

    View Slide

  28. PRO TIP

    View Slide

  29. PRO TIP

    View Slide

  30. PRO TIP

    View Slide

  31. PRO TIP

    View Slide

  32. PRO TIP

    View Slide

  33. SIMPLE CACHING

    View Slide

  34. AFTER
    No data synchronization between
    instances of your Lambda functions.
    BEFORE
    Low cost caching without performance
    overhead.

    View Slide

  35. Use a bullet-proof caching library and host it by yourself.
    CUSTOM CACHING SERVICE

    View Slide

  36. CUSTOM CACHING SETUP
    1.) GET Name for X
    CACHE
    2.) GET Name for X
    3.) SET Name for X
    DynamoDB

    View Slide

  37. "Whe; /4: la@;Ah B; EC2 in.9-nH6
    Iit84:9 sJeA=fLM5g a .@On6), it's
    a@)QRat=HBSlL Sa@nHT6d i;94 B deV-:St
    .uOnX) M5 yo@[ <6fa@\9 V^_."
    https://docs.aws.amazon.com/vpc/latest/userguide/default-vpc.html

    View Slide

  38. "By 3ea-:l), re.4:bce. I=t8M5 a V^_ -r6 ;o9
    -cH6dsieSX f[QR wi)T=n B LahOd- V:5c)i45"
    https://docs.aws.amazon.com/lambda/latest/dg/vpc.html

    View Slide

  39. LAMBDA IN VPC
    ACCESS POSSIBLE, BUT
    LONGER STARTUP TIMES.

    View Slide

  40. LAMBDA VPC STARTUP
    VPC
    Network Hardware Image: https://www.flaticon.com/free-icon/network-interface-card_969356
    "A Serverless Journey: AWS Lambda Under the Hood"
    https://www.youtube.com/watch?v=QdzV04T_kec&t=2400

    View Slide

  41. CUSTOM CACHING SETUP
    VPC
    Lambda Subnet Cache Subnet
    Public Subnet

    View Slide

  42. Demo

    View Slide

  43. Use a managed caching service instead of maintaining your own.
    MANAGED CACHING SERVICES

    View Slide

  44. AWS ELASTICACHE
    REDIS + MEMCACHED

    View Slide

  45. MAINTENANCE
    AWS is regularly taking care
    of updates.
    ADVANTAGES
    COMPATIBILITY
    Redis and Memcached well
    supported caching solutions.
    GET STARTED
    Easy setup and ready to go.

    View Slide

  46. SIMILAR SETUP
    LIKE CUSTOM
    CACHING

    View Slide

  47. Demo

    View Slide

  48. SEBASTIAN HESSE
    Software Engineer | www.k15t.com | @seeebiii
    CHARLIE IS MY HOMEBOY
    CHARLIE IS MY HOMEBOY
    K15t Software
    Example code @ GitHub:
    https://github.com/seeebiii/caching-in-aws-lambda

    View Slide

  49. NEW MEETUP SERIES: K15T TEAM TALKS
    TOPIC: GIT TOGETHER
    More information:
    https://www.meetup.com/de-DE/K15tTeamTalks/events/256232999/

    View Slide