Redis [2] redis configuration and distributed lock introduction

You and other students 2022-02-13 08:09:09 阅读数:120

redis redis configuration distributed lock

This article has been arranged since childhood d Class notes and java Advanced warehouse , any similarity , Most of them are written by others

java Advanced warehouse :

redis[2] redis Configuration and distributed lock introduction

Study xiao D Project notes in class

SpringDataRedis To configure RedisTemplate Introduce

  • RedisTemplate Introduce
    • ValueOperations: Simple K-V operation
    • SetOperations:set Type data operation
    • ZSetOperations:zset Type data operation
    • HashOperations: in the light of map Type of data operation
    • ListOperations:list Type of data operation
  • RedisTemplate and StringRedisTemplate The difference between
    • StringRedisTemplate Inherit RedisTemplate
    • The data of the two are different ( The default serialization mechanism leads to key Dissimilarity )
    • StringRedisTemplate The default is String The serialization strategy of
    • RedisTemplate The default is JDK The serialization strategy of , The data will be sequenced into a byte array and then stored in Redis database
    • summary
      • When redis When the original operation in the database is string data , That use StringRedisTemplate that will do
      • Data is a complex object type , So use RedisTemplate It's a better choice
  • operation
    • String structure
      • Store string
      • Store the object

Registration and login of case practice - Graphic verification code + Google open source Kaptcha introduce

  • background
    • register - Sign in - To change the password, you usually need to send a verification code , But vulnerable to attack, malicious calls
    • What is a text message - Mailbox bomber
      • SMS bombers are a batch of 􏰀、 Loop to the mobile phone to send unlimited registration information of various websites Method of card code short message .
    • The loss of the company
      • A text message 5 Cents , If it's big 􏰀 Steal the brush and calculate by yourself Email notification is free , But it was big 􏰀 Steal brush , bandwidth 、 Connections, etc. are occupied use , It can not be used normally
  • How to avoid your website becoming ” chicken “ Or be brushed
    • Add graphic captcha ( Developer )
    • single IP Limit the number of requests ( Developer )
    • Limit number sending ( Generally, SMS providers will do )
    • There is always attack and defense , It only increases the cost of attackers ,ROI Row no Naturally, I gave up
  • Kaptcha The framework is introduced
    • A highly configurable practical verification of Google open source Code generation tools
      • Verification code font / size / Color
      • Scope of verification code content ( Numbers , Letter , Chinese characters !)
      • Size of captcha picture , Frame , Border thickness , Border color
      • The interference line of the verification code Style of verification code (⻥ Eye style 、3D、 Ordinary blur )

High concurrency commodity home page hot data development practice

  • Hot data
    • It's often queried , But data that is not often modified or deleted
    • home page - Details page
  • Link logic
    • Check whether the cache has
    • If the cache does not exist, query the database
    • Put the query results into the cache , Set expiration time
    • The next access will hit the cache
  • Interface development
    • Simulating database queries takes time 200ms
    • Uncached logic :controller-service-dao layer
    • Add cache logic :controller-service-dao layer
  • Be careful
    • Cache breakdown
    • Cache penetration
    • Cache avalanche
    • Cache and database data consistency

Jmeter5.x Pressure test tools

  • LoadRunner

    • Stable performance , The results of pressure measurement and fine grain size are large , You can customize the script for pressure testing , But it's too big , There are many functions
  • Apache AB( Single interface pressure measurement is the most convenient )

    • Simulate multithreaded concurrent requests ,ab Commands require very little of the computer issuing the load , It doesn't take much CPU, And it won't take up too much memory , But it can put a huge load on the target server , Simple DDOS Attack, etc
  • Webbench

    • webbench First fork More than one subprocess , Each subprocess loops through web Access test . The subprocess passes the result of the visit through pipe Tell the parent process , The parent process makes the final statistics .
  • Jmeter

    • Free open source , Powerful , It is widely used in Internet companies
  • Local quick installation of pressure measuring tools Jmeter5.x

    • Need to install JDK8 above
    • Proposed installation JDK Environmental Science , although JRE It's fine too , But pressure measurement https need JDK Inside keytool Tools
    • Quick download
    • Document address :
  • Hot data interface pressure measurement

    • QPS: (Query Per Second): Requests per second , That is, how many requests the server processed in a second
  • Add aggregation report : Thread group -> add to -> Monitor -> Aggregation report (Aggregate Report)

 lable: sampler The name of
Samples: How many requests were sent out , for example 10 Users , loop 10 Time , It is 100
Average: Mean response time
Median: Median , That is to say 50% User's response time
90% Line : 90% The user's response will not exceed this time (90% of the samples took no more than this time. The remaining samples at least as long as this)
95% Line : 95% The user's response will not exceed this time
99% Line : 99% The user's response will not exceed this time
min : Minimum response time
max : Maximum response time
Error%: Number of wrong requests / Total number of requests
Throughput: throughput —— By default, this represents the number of requests completed per second (Request per Second) It can be compared to qps、tps
KB/Sec: The amount of data received per second
  • Pressure measurement based on the current machine configuration
    • Unused cache interface
    • Use cache interface
  • Problems with the current architecture
    • Distributed cache and application server network need intranet communication

    • You can deploy locally Redis To test

Introduction to the core knowledge of distributed lock and precautions

  • background

    • This is to ensure that only one client can operate on shared resources at the same time

    • Case study : The number of coupons is limited 、 The inventory of goods is oversold

    • The core

      • In order to prevent multiple processes from interfering with each other in a distributed system , We need a distributed coordination technology to schedule these processes
      • Use the mutual exclusion mechanism to control the access of shared resources , This is the problem of distributed lock
  • Avoid data problems caused by concurrent operation of shared resources

    • Lock
      • Local lock :synchronize、lock etc. , Locked in the current process , There are still problems under cluster deployment
      • Distributed lock :redis、zookeeper Such as implementation , Although it's still locked , But lock tags shared by multiple processes , It can be used Redis、Zookeeper、Mysql And so on.
  • What should be considered in designing distributed locks

    • exclusive

      • In distributed application clusters , The same method can only be executed by one thread on one machine at the same time
    • Fault tolerance

      • Distributed locks must be released , Such as client crash or network outage
    • Satisfy reentrant 、 High performance 、 High availability

    • Note the overhead of distributed locks 、 Lock granularity

copyright:author[You and other students],Please bring the original link to reprint, thank you.