Memcached Notes--(iv) responding to high concurrent attacks

Source: Internet
Author: User
Tags getmessage memcached log4j

Nearly half a month has been very painful, mainly products on the line, attracted countless machine users malicious attacks, and constantly refresh the products of various service portals, the production of garbage data, consumption of resources. Their best results, 1 seconds can be concurrent 6 times, in the database before storage, cache missing loading before the seizure of this more than 10 milliseconds, a malicious attack.

RELATED links:
Memcached notes-(i) Installation & General error & Monitoring
Memcached Notes--(ii) xmemcached&spring integration
Memcached Notes--(iii) Summary
of the use of memcached

memcached Notes--(iv) responding to high concurrent attacks

In response to this situation, the following adjustments have been made:

When you update the data, write the cache first, then write the database, and if you can, write to the queue for subsequent completion. Limit the unified account, the same action, the same second number of concurrent times, more than 1 times do not do the action, return operation failed. Limit unified user, daily action times, overrun return operation failed.

To complete the above operation, colleagues give me a weapon. With the memcached Add method, you can quickly solve the problem. No need for cumbersome development, no need to rely on database records, full memory operations.

The following methods are used to implement a conflict determination:

Java Code/** * Conflict delay 1 seconds/public static final int mutex_exp = 1;/** * Conflict key */public static final String Mutex_key_prefi X = "Mutex_"; /** * Conflict Determination * @param key/public boolean Ismutex (String key) {return Ismutex (key, Mutex_exp);}/** * Conflict determination * @param Key * @param exp * @return true conflict */public boolean Ismutex (String key, int exp) {Boolean status = true; try {if MEMCA Chedclient.add (Mutex_key_prefix + KEY, exp, True)) {status = false;}} catch (Exception e) {logger.error (E.getmessage (), e);} return status; }

	/**
	 * Conflict delay 1 seconds * * Public
	static final int mutex_exp = 1;
	/**
	 * Conflict Key
	 *
	/public static final String Mutex_key_prefix = "Mutex_";

	/**
	 * Conflict Determination
	 * 
	 * @param key
	/public boolean Ismutex (String key) {return
		Ismutex key, MUTEX_EXP);
	}

	/**
	 * Conflict Determination
	 * 
	 @param key
	 * @param exp
	 * @return true conflict *
	 /Public
	Boolean Ismutex ( String key, int exp) {
		Boolean status = True;
		try {
			if (Memcachedclient.add (Mutex_key_prefix + KEY, exp, True)) {
				status = false;
			}
		catch (exce Ption e) {
			logger.error (E.getmessage (), E);
		}
		return status;
	}

Make a note:

Options Description
Add Save only if no key data exists in the storage space
Replace Save only if there is data in the same key in the storage space
Set Unlike add and replace, save at any time

That is, if the add operation returns True, it is not currently conflicting.

Return to the scene, malicious users 1 seconds to operate 6 times, encountered this method, only obediently 1 seconds later. Don't underestimate this 1 seconds, a database operation is only a few milliseconds. 1-second delay is enough to reduce system load and increase the cost of malicious users.

Attached to my application based on xmemcached implementation:

Java code Import net.rubyeye.xmemcached.MemcachedClient; Import Org.apache.log4j.Logger; Import org.springframework.beans.factory.annotation.Autowired; Import org.springframework.stereotype.Component; /** * * @author Snowolf * @version 1.0 * @since 1.0/@Component public class Memcachedmanager {/** * cache aging 1 days/public static final int cache_exp_day = 3600 * 24; /** * Cache Aging 1 weeks/public static final int cache_exp_week = 3600 * 24 * 7; /** * Cache Aging January/public static final int cache_exp_month = 3600 * 24 * 30; /** * Cache Aging Permanent/public static final int cache_exp_forever = 0; /** * Conflict delay 1 seconds * * public static final int mutex_exp = 1; /** * Conflict key */public static final String Mutex_key_prefix = "Mutex_"; /** * Logger for this class/private static final Logger Logger = Logger. GetLogger (Memcachedmanager.class); /** * Memcached Client * * @Autowired private memcachedclient memcachedclient; /** * Cache * @param key * @param value * @param exp * Failure time/public void CacheObject (String key, Object ValuE, int exp) {try {memcachedclient.set (key, exp, value);} catch (Exception e) {logger.error (E.getmessage (), e);} Logge R.info ("Cache Object: [" + key + "]");} /** * Shut down the Memcached cilent. */public void Finalize () {if (memcachedclient!= null) {try {if (!memcachedclient.isshutdown ()) {Memcachedclient.shut Down (); Logger.debug ("Shutdown memcachedmanager ...");} catch (Exception e) {logger.error (E.getmessage (), E);}} /** * Clean Object * * @param key */public void Flushobject (String key) {try {memcachedclient.deletewithnoreply (key)} catch (Exception e) {Logger.error (E.getmessage (), e);} Logger.info ("Flush Object: [" + key + "]"); /** * Conflict Determination * @param key/public boolean Ismutex (String key) {return Ismutex (key, Mutex_exp);}/** * Conflict determination * @param Key * @param exp * @return true conflict */public boolean Ismutex (String key, int exp) {Boolean status = true; try {if MEMCA Chedclient.add (Mutex_key_prefix + KEY, exp, True)) {status = false;}} catch (Exception e) { Logger.error (E.getmessage (), E); } return status; /** * Load Cache Object * * @param key * @return/public <T> T loadobject (String key) {T object = null; try {object = Mem Cachedclient.<t> get (key); catch (Exception e) {logger.error (E.getmessage (), e);} logger.info ("Load Object: [" + key + "]"); return object; } }

Import net.rubyeye.xmemcached.MemcachedClient;
Import Org.apache.log4j.Logger;
Import org.springframework.beans.factory.annotation.Autowired;

Import org.springframework.stereotype.Component;
	 /** * * @author Snowolf * @version 1.0 * @since 1.0/@Component public class Memcachedmanager {/** * cache aging 1 days

	* * public static final int cache_exp_day = 3600 * 24;

	/** * Cache Aging 1 weeks/public static final int cache_exp_week = 3600 * 24 * 7;

	/** * Cache Aging January/public static final int cache_exp_month = 3600 * 24 * 30;

	/** * Cache Aging Permanent/public static final int cache_exp_forever = 0;
	/** * Conflict delay 1 seconds * * public static final int mutex_exp = 1;
	/** * Conflict key */public static final String Mutex_key_prefix = "Mutex_";

	/** * Logger for this class/private static final Logger Logger = Logger. GetLogger (Memcachedmanager.class);

	/** * Memcached Client * * @Autowired private memcachedclient memcachedclient;
	/** * Cache * @param key * @param value * @param exp * Expiration time */public void CacheObject (String key, Object value, int exp) {try {Memcachedc
		Lient.set (Key, exp, value);
		catch (Exception e) {logger.error (E.getmessage (), E);
	} logger.info ("Cache Object: [" + key + "]");
	 }/** * Shut down the Memcached cilent. */public void Finalize () {if (memcachedclient!= null) {try {if (!memcachedclient.isshutdown ()) {MEMCA
					Chedclient.shutdown ();
				Logger.debug ("Shutdown memcachedmanager ...");
			The catch (Exception e) {logger.error (E.getmessage (), E); }}/** * Cleanup Object * * @param key/public void Flushobject (String key) {try {Memcachedclient.deletew
		Ithnoreply (key);
		catch (Exception e) {logger.error (E.getmessage (), E);
	} logger.info ("Flush Object: [" + key + "]");
	/** * Conflict Determination * * @param key/public boolean Ismutex (String key) {return Ismutex (key, Mutex_exp); /** * Conflict Determination * @param key * @param exp * @return true Conflict */public boolean Ismutex (String key, int exp) {Boolean status = True;
			try {if (Memcachedclient.add (Mutex_key_prefix + KEY, exp, True)) {status = FALSE;
		The catch (Exception e) {logger.error (E.getmessage (), E);
	} return status;
		/** * Load Cache Object * * @param key * @return/public <T> T loadobject (String key) {T object = null;
		try {object = memcachedclient.<t> get (key);
		catch (Exception e) {logger.error (E.getmessage (), E);
		} logger.info ("Load Object: [" + key + "]");
	return object; }

}

RELATED links:
Memcached notes-(i) Installation & General error & Monitoring
Memcached Notes--(ii) xmemcached&spring integration
Memcached Notes--(iii) Summary
of the use of memcached

Memcached Notes--(iv) responding to high concurrent attacks

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.