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

How to build & support high load REST API

Avatar for Andrey.v Andrey.v
November 23, 2019

How to build & support high load REST API

I would like to tell the story of our experience of building the HighLoad REST API that has the following parameters:
* Intuitive
* Scalable
* Fault tolerant
* Protection from attack
* High bandwidth

Also I would like to describe:
* how we did LoadTesting
* how we survived a lots of DDoS attacks
* how we did deployment without downtime

The following list of technologies were used: .NET Core 2, ASP.NET Core 2, Consul, Fabio, Orleans.NET, Kafka, RabbitMQ, Serilog, xMetrics, Grafana, ELK

Avatar for Andrey.v

Andrey.v

November 23, 2019

Other Decks in Programming

Transcript

  1. Login API Authenticate Fast response High throughput Resistance to DDoS

    and Brute Force attacks High Availability (99.99 %)
  2. What we need Anti-robot defense Resistance to DDoS attack Quick

    response High throughput Scalable High availability
  3. Protection & Security HOW TO IDENTIFY POSSIBLE FRAUD? HOW TO

    PROTECT FROM ROBOTS? WHAT TO ANALYZE? WHERE TO STORE DATA FOR ANALYZING?
  4. Captcha Send a CAPTCHA challenge, expecting a human response to

    the challenge LoginAPI generates captcha value for current session and stores it inside Orleans Use a non-white background with interspersed or roughened
  5. Fraud detection Analyze IP addresses User-Agent Amount of failed login

    attempts per minute Amount of success login attempts per minute
  6. Blocking Block players from the login base on the analysis

    of failed and success attempts per period
  7. Quick response & High throughput Is captcha required? Whether login

    request is blocked? Quick search player by its login Login reports: success & failed Collect metrics
  8. Stateless architecture: Pros Services are easy to scale No state

    in services When service server is dead – a new one could be up without any crucial affect
  9. Stateless architecture: Cons Add latency Non efficient access to data

    Limits scalability • Master / slave replication • Sharding Concurrency
  10. Stateless architecture: Cons Add latency Non efficient access to data

    Limits scalability • Master / slave replication • Sharding Concurrency Cache invalidation
  11. What we want FAST ANALYZE OF INPUT REQUESTS IN-MEMORY STATISTICS

    OF SUCCESS / FAILED REQUESTS FAST AND SERIALIZABLE ACCESS TO THE CACHE DATA INVALIDATION EASY SCALING
  12. Actor model Actor can: • Receive messages • Make local

    decisions • Create more actors • Send more messages • Determine how to respond to the next message received
  13. Orleans .NET Framework used to create Scalable, Distributed, .NET Applications

    Focused on low response latency and high concurrency Usable in any .NET application (but frequently used with WebAPI Applications) Based on a system of VirtualActors
  14. Virtual actors Actor instances always exist, virtually • Application neither

    creates nor deletes them. They never fail. • Code can always call methods on an actor Activations are created on-demand • If there is no existing activation, a message sent to it triggers instantiation • Transparent recovery from server failures • Lifecycle is managed by the runtime • Runtime can create multiple activations of stateless actors (for performance) Location transparency • Actors can pass around references to one another or persist them • These are logical (virtual) references, always valid, not tied to a specific activation
  15. Orleans: Grain = Virtual actor • Grains are C# classes

    Derived from GrainBase Implement an Interface (e.g. IPlayer, ISession) • Messages passing = Calling Interface methods Ex: AddLoginSession (int playerId, Guid sessionId) • Can be Stateless or Stateful
  16. Orleans: Concurrency model for Grains Orleans uses a cooperative multithreading

    scheduler Scheduler schedules only one message at a time for a grain A message is processed completely before another message is scheduled A message is processed as a sequence of one or more turns (continuations)
  17. Orleans cluster Transparent scalability by default New Silos can be

    added at any time Location transparency Multiplexed communication Efficient scheduling
  18. Orleans grains Session Grain Player Grain Online Grain Operator Grain

    IP Grain Login Grain Cache layer Statistics layer Online layer Grain Cache Service Operator Cache Grain Player Cache Grain
  19. Orleans usage Cache pre-population on startup Update cache on a

    regular basis (Kafka loader) In-memory statistics of all login attempts Regulation specific data is being stored in DB All logged in players are in memory Time regulation features are based on Timer
  20. HA: Main points All requests should be signed with JWT

    All secrets are stored in Vault Consul as a LB for every API host
  21. HA: Communication schema Client Login API JWT Secret Credentials Anonymous

    JWT Credentials Anonymous JWT OK Auth JWT Availability status Health check 1 2 3
  22. Blue/Green deployment Login API-A Login API-B Client Credentials * Cluster

    (default=A) * Cluster A Cluster B Availability status Health check
  23. Metrics Based on Kibana • Success / Failed responses •

    Failed reasons • Blocks statistics Based on InfluxDB/Graphana • Response time for each MS • Total requests for each MS • Successful logins per min • Unsuccessful logins per min
  24. Fails Kafka consumer in the main thread Wrong configuration: connect

    App to Orleans Improper Captcha counters calculation
  25. Summary It’s impossible to handle all requests Quotes granting system

    Reject requests to save the system Load testing Know your common scenarios Identify performance degradation Feature toggles It’s better to disable some feature instead of broke the whole system Aggressive caching mechanism System health Alerts to prevent real issue on PROD Metrics to identify weak places